Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve bytes and bytearray serialization #4009

Merged
merged 5 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions distributed/protocol/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,17 +564,36 @@ def normalize_Serialized(o):
return [o.header] + o.frames # for dask.base.tokenize


# Teach serialize how to handle bytestrings
@dask_serialize.register((bytes, bytearray))
# Teach serialize how to handle bytes
@dask_serialize.register(bytes)
def _serialize_bytes(obj):
header = {} # no special metadata
frames = [obj]
return header, frames


@dask_deserialize.register((bytes, bytearray))
# Teach serialize how to handle bytestrings
@dask_serialize.register(bytearray)
def _serialize_bytearray(obj):
header = {} # no special metadata
frames = [obj]
return header, frames


@dask_deserialize.register(bytes)
def _deserialize_bytes(header, frames):
return b"".join(frames)
if len(frames) == 1 and isinstance(frames[0], bytes):
return frames[0]
else:
return bytes().join(frames)


@dask_deserialize.register(bytearray)
def _deserialize_bytearray(header, frames):
if len(frames) == 1 and isinstance(frames[0], bytearray):
return frames[0]
else:
return bytearray().join(frames)


@dask_serialize.register(memoryview)
Expand Down
7 changes: 7 additions & 0 deletions distributed/protocol/tests/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ def test_serialize_bytestrings():
header, frames = serialize(b)
assert frames[0] is b
bb = deserialize(header, frames)
assert type(bb) == type(b)
assert bb == b
bb = deserialize(header, list(map(memoryview, frames)))
assert type(bb) == type(b)
assert bb == b
bb = deserialize(header, [b"", *frames])
assert type(bb) == type(b)
assert bb == b


Expand Down