Repository navigation
Conversation
When the read buffer has less spare capacity than the read strategy's next size, `poll_read_from_io` reserves `next` bytes. If an earlier message's bytes are still referenced, `BytesMut::reserve` can't reclaim the buffer and allocates a new one. A connection reading many small messages while holding the earlier ones allocates a new 8 KiB buffer for each read, and most of each old buffer goes unused. Only reserve when the spare capacity is below the smallest read the adaptive strategy still counts as in range. Filling that space never shrinks the strategy. It can't grow it either, so growth may come one read later. An exact read size reserves as before.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When the read buffer has less spare capacity than the read strategy's
nextsize,poll_read_from_ioreservesnextbytes. If an earlier message's bytes are still referenced, for example because a client is still holding the previous response's headers or body,BytesMut::reservecan't reclaim the buffer and allocates a new one. So a connection that reads lots of small messages allocates a new 8 KiB buffer for each read, and most of each old buffer goes unused.I ran into this while profiling a service that makes a lot of small HTTP/1 requests over keep-alive connections. The allocations showed up in its CPU profiles on both its client and server connections.
With this change,
poll_read_from_ioonly reserves when the spare capacity is smaller than the smallest read the adaptive strategy still counts as in range. That'sprev_power_of_two(next), so 4 KiB at the default 8 KiB. Filling that space never shrinks the strategy. It can't grow it either, because the read is smaller thannext, so growth can come one read later than it does now.Exactread sizes reserve the same way they do today.