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

Simplify frame length packing in TCP write #4257

Merged
merged 5 commits into from
Nov 20, 2020

Conversation

jakirkham
Copy link
Member

Noticed that TCP write was taking a while in the process of other benchmarking work. These struct.pack calls stuck out. Combined these into a single struct.pack call for simplicity. This results in a nearly 5x speed improvement.

Here's a simple benchmark to illustrate the effect of this change:

In [1]: import struct

In [2]: %%timeit lengths = list(range(10))
   ...: 
   ...: length_bytes = [struct.pack("Q", len(lengths))] + [
   ...:     struct.pack("Q", x) for x in lengths
   ...: ]
   ...: length_bytes = b"".join(length_bytes)
   ...: 
   ...: 
3.45 µs ± 88.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [3]: %%timeit lengths = list(range(10))
   ...: 
   ...: nframes = len(lengths)
   ...: length_bytes = struct.pack(f"Q{nframes}Q", nframes, *lengths)
   ...: 
   ...: 
711 ns ± 2.05 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants