-
Notifications
You must be signed in to change notification settings - Fork 171
[stream] BufferedSender
#2512
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
[stream] BufferedSender
#2512
Conversation
| if self.send_buf.capacity() < target_capacity { | ||
| self.send_buf | ||
| .reserve(target_capacity - self.send_buf.capacity()); | ||
| } |
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.
Bug: Reserve calculation incorrectly computes additional capacity needed
The reserve() call uses target_capacity - self.send_buf.capacity() but BytesMut::reserve(n) ensures capacity >= len + n. After clear(), len is 0, so reserve(n) guarantees capacity >= n. When capacity is between half the target and the target (e.g., capacity=150, target=200), calling reserve(50) only guarantees capacity ≥ 50, which is already satisfied, so nothing happens. The intended 2x buffer optimization fails to work, causing the same memory fragmentation the BufferedSender was designed to prevent.
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.
This is incorrect; BytesMut::reserve documents:
Reserves capacity for at least additional more bytes to be inserted into the given BytesMut.
More than additional bytes may be reserved in order to avoid frequent reallocations. A call to reserve may result in an allocation.
|
#2518 potential alternative |
|
It's worth approaching vectored writes instead of this IMO. Yields a performance benefit for sure, but is more of a band-aid when a larger API change is justified. |
Codecov Report❌ Patch coverage is
@@ Coverage Diff @@
## main #2512 +/- ##
==========================================
- Coverage 92.51% 92.50% -0.02%
==========================================
Files 340 340
Lines 97407 97485 +78
==========================================
+ Hits 90114 90174 +60
- Misses 7293 7311 +18
... and 3 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
Overview
Adds a
BufferedSenderwrapper instreamthat reduces memory fragmentation by re-using a buffer that grows to the size of the largest message sent.send_frame(unbuffered)BufferedSender