Skip to content

Commit

Permalink
Simplify packing of frame lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
jakirkham committed Nov 20, 2020
1 parent 51bbaf1 commit e9577c2
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions distributed/comm/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,12 @@ 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", nframes)] + [
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
b = b"".join([length_bytes, *frames]) # small enough, send in one go
stream.write(b)
else:
stream.write(b"".join(length_bytes)) # avoid large memcpy, send in many
stream.write(length_bytes) # avoid large memcpy, send in many

for frame, frame_bytes in zip(frames, lengths):
# Can't wait for the write() Future as it may be lost
Expand Down

0 comments on commit e9577c2

Please sign in to comment.