feat(bytesbuf): clamp minimum reservation size in BytesBufWriter#277
Merged
sandersaares merged 1 commit intomainfrom Feb 20, 2026
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #277 +/- ##
=======================================
Coverage 100.0% 100.0%
=======================================
Files 141 141
Lines 8626 8635 +9
=======================================
+ Hits 8626 8635 +9 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
BytesBufWriter previously reserved only the exact number of bytes requested on each write. Since the Write trait encourages many small incremental writes, this caused excessive memory allocations. The writer now uses a two-tier reservation strategy: - Empty buffer, small payload (<=4 KiB): reserve 4 KiB. Assumes total data will be modest, avoiding waste for small payloads like short JSON error responses (which could otherwise open a door to resource exhaustion if they over-reserved). - Otherwise: reserve at least 64 KiB (or the payload size if larger). Once data crosses the 4 KiB boundary we are likely dealing with a large payload, so we take big bites to reduce allocation frequency. The reservation logic is factored into ensure_sufficient_capacity() on BytesBufWriter, keeping write() clean and providing a natural place for the design rationale documentation. Closes #271 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
84b7eaf to
bfd668c
Compare
martintmk
approved these changes
Feb 20, 2026
sgalkin
approved these changes
Feb 20, 2026
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
BytesBufWriter(thestd::io::Writeadapter forBytesBuf) previously reserved only the exact number of bytes requested on eachwrite()call. SinceWriteis designed for piece-by-piece writing in small increments, this caused excessive memory allocations - each tiny write triggered its own reservation.Changes
The writer now uses a two-tier minimum reservation strategy via a new
ensure_sufficient_capacity()method:The reservation logic is factored out of
write()intoensure_sufficient_capacity()onBytesBufWriter, keeping theWriteimpl clean and providing a natural home for the design rationale documentation.Testing
Three new tests covering:
Closes #271