-
Notifications
You must be signed in to change notification settings - Fork 92
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
Compute the expected body length to reduce heap allocations #1773
Compute the expected body length to reduce heap allocations #1773
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR!
This change removes a Vec allocation for every peer message. But the exact body length calculation adds an extra serialization. (I'm not sure if the functions called by our serializations do any extra allocations internally. And I don't know how much of the redundant FakeWriter serializations will be removed by the optimiser. But our serialization code does try to write directly to the Writer.)
Overall, allocations are more expensive than serialization, particularly if the allocation incurs a page fault. So this should provide a small performance improvement. (And a bit of code cleanup.)
We should follow up this PR with a size estimator and a benchmark, but that's on us :-)
Co-authored-by: teor <teor@riseup.net>
@teor2345 Yeah, I was originally writing out size estimators for each message, but eventually due to all the nested structures it started to look like... the serialization code. So I figured this would be as efficient modulo some pointer arithmetic (which probably won't get optimized out, but meh) |
@ebfull yeah it's possible there's no good design for size estimation - or we'd have to rewrite all out serialisation code using a visitor pattern. So we might never end up doing that ticket. Doubling the size of all our serialisation code wouldn't be great for performance either. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The latest change just applies my comment suggestion.
We should re-run CI after #1789 merges, then merge this PR once it passes. |
Motivation
Unnecessary allocation(s) in the external protocol codec implementation.
Solution
Let's precompute the expected body length of a network message and reserve this capacity in the provided buffer. Also compute the checksum in-place.
Review
This review is a low priority.
Follow Up
Add a size estimator #1774
Proptest our message implementation #27
Add benchmarks for network messages #1775