Skip coercing to bytes in merge_frames#3960
Merged
mrocklin merged 1 commit intodask:masterfrom Jul 14, 2020
Merged
Conversation
As the frames we receive are typically mutable, non-`bytes` objects like `bytearray`s or NumPy `ndarray`s, coercing to `bytes` at this stage triggers a copy of all frames. As we are going to toss those copied versions anyways when joining them into a larger `bytes` object, this ends up being wasteful with memory. Fortunately `bytes.join(...)` accepts any and all `bytes`-like objects. So instead just pass them all through as-is to `bytes.join(...)`, which is free and doesn't require a copy. Should cutdown on the memory usage in this part of the code.
06612e0 to
a3671e4
Compare
Member
Author
|
FWIW here's a little example showing this works. In [1]: import numpy as np
In [2]: b"".join([
...: b"abc",
...: bytearray(b"def"),
...: memoryview(b"ghi"),
...: np.arange(106, 109, dtype="u1")
...: ])
Out[2]: b'abcdefghijkl'This was raised in Python issue ( https://bugs.python.org/issue15958 ) and implemented in commit ( python/cpython@cfc22b4 ) as part of Python 3.4+. |
Member
|
I kicked the CI here so test should be passing now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As the frames we receive are typically mutable, non-
bytesobjects likebytearrays or NumPyndarrays, coercing tobytesat this stage triggers a copy of all frames. As we are going to toss those copied versions anyways when joining them into a largerbytesobject, this ends up being wasteful with memory. Fortunatelybytes.join(...)accepts any and allbytes-like objects. So instead just pass them all through as-is tobytes.join(...), which is free and doesn't require a copy. Should cutdown on the memory usage in this part of the code.