Skip to content

perf(http1): reuse spare read buffer capacity - #4291

Open
zaidoon1 wants to merge 1 commit into
hyperium:masterfrom
zaidoon1:reuse-read-buf-spare-capacity
Open

zaidoon1 wants to merge 1 commit into
hyperium:masterfrom
zaidoon1:reuse-read-buf-spare-capacity

Conversation

@zaidoon1

@zaidoon1 zaidoon1 commented Nov 30, 2026 •

Copy link
Copy Markdown

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, for example because a client is still holding the previous response's headers or body, BytesMut::reserve can'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_io only reserves when the spare capacity is smaller than the smallest read the adaptive strategy still counts as in range. That's prev_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 than next, so growth can come one read later than it does now. Exact read sizes reserve the same way they do today.

  • The description, docs, and comments (words meant for humans) are written by a human, not by an LLM.

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

No deployments
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.

1 participant