Skip to content

Commit

Permalink
Simplify frame length packing in TCP write (#4257)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakirkham authored Nov 20, 2020
1 parent 2ba2731 commit 48648fb
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions distributed/comm/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,15 @@ async def write(self, msg, serializers=None, on_error="message"):
)

try:
nframes = len(frames)
lengths = [nbytes(frame) for frame in frames]
length_bytes = [struct.pack("Q", len(frames))] + [
struct.pack("Q", x) for x in lengths
]
length_bytes = struct.pack(f"Q{nframes}Q", nframes, *lengths)
if sum(lengths) < 2 ** 17: # 128kiB
b = b"".join(length_bytes + frames) # small enough, send in one go
stream.write(b)
# small enough, send in one go
stream.write(b"".join([length_bytes, *frames]))
else:
stream.write(b"".join(length_bytes)) # avoid large memcpy, send in many
# avoid large memcpy, send in many
stream.write(length_bytes)

for frame, frame_bytes in zip(frames, lengths):
# Can't wait for the write() Future as it may be lost
Expand Down Expand Up @@ -398,7 +398,7 @@ def __init__(
deserialize=True,
allow_offload=True,
default_port=0,
**connection_args
**connection_args,
):
self._check_encryption(address, connection_args)
self.ip, self.port = parse_host_port(address, default_port)
Expand Down

0 comments on commit 48648fb

Please sign in to comment.