tonic: avoid excess decode buffer reservation#2735
Open
mingley wants to merge 2 commits into
Open
Conversation
Reserve only when the current decode buffer capacity cannot hold the announced message. This avoids reallocating and copying data that is already fully buffered while retaining the existing fast path for fragmented messages.\n\nAdd representative unary and streaming decode benchmarks covering default HTTP/2 frame sizing, fragmentation, and coalesced data.
Exercise fragmented, coalesced, and custom-buffer decoding with distinct message payloads. Rename the representative benchmarks around body chunk sizes so they remain transport agnostic.
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.
Summary
hold the announced gRPC message.
decode paths.
Motivation
BytesMut::reservetreats its argument as additional capacity. Callingself.buf.reserve(len)after consuming the gRPC header can therefore targetself.buf.len() + lenbytes even when some or all of the message is alreadybuffered.
For complete or coalesced messages, this can trigger an unnecessary allocation
and copy of the buffered payload. It can also retain substantially more memory
than the message requires.
Solution
Check whether the current
BytesMutcapacity is smaller than the announcedmessage length. Only in that case, reserve the difference between the message
length and the bytes already buffered.
The subtraction is safe because
BytesMut::len()cannot exceedBytesMut::capacity(), and it only executes when capacity is below the messagelength. The decode state machine, size limits, compression handling, and wire
behavior are unchanged.
Benchmark Results
Measured on an Apple M4 Pro running macOS 26.5.1 with rustc 1.97.0. Baseline
and optimized benchmark binaries were compiled from the same source path.
Each result is the median of 10 interleaved A/B process runs using the release
benchmark profile. Negative changes are faster.
The large-message 16 KiB chunk cases are unchanged within measurement noise.
The improvement is concentrated where a body chunk already contains a complete
message or multiple messages, which is where the previous reservation could
force an avoidable reallocation.
Testing Done
custom-buffer decoding
cargo fmt --all -- --checkcargo clippy --workspace --all-features --all-targetscargo test --workspace --all-features --no-fail-fastzstd, server, and channel feature configurations