Skip to content

feat(bytesbuf): clamp minimum reservation size in BytesBufWriter#277

Merged
sandersaares merged 1 commit intomainfrom
feat/bytesbuf-writer-reservation-clamping
Feb 20, 2026
Merged

feat(bytesbuf): clamp minimum reservation size in BytesBufWriter#277
sandersaares merged 1 commit intomainfrom
feat/bytesbuf-writer-reservation-clamping

Conversation

@sandersaares
Copy link
Member

Summary

BytesBufWriter (the std::io::Write adapter for BytesBuf) previously reserved only the exact number of bytes requested on each write() call. Since Write is 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:

  • Empty buffer, small payload (<=4 KiB): reserve 4 KiB. We assume total data will be modest, avoiding waste for small payloads (e.g. short JSON error responses, where over-reserving could open a door to resource exhaustion).
  • 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 out of write() into ensure_sufficient_capacity() on BytesBufWriter, keeping the Write impl clean and providing a natural home for the design rationale documentation.

Testing

Three new tests covering:

  • Empty buffer with small write -> 4 KiB reservation
  • Empty buffer with large write (>4 KiB) -> 64 KiB reservation
  • Non-empty buffer needing more capacity -> 64 KiB reservation

Closes #271

@codecov
Copy link

codecov bot commented Feb 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.0%. Comparing base (2a15324) to head (bfd668c).
⚠️ Report is 1 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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>
@sandersaares sandersaares force-pushed the feat/bytesbuf-writer-reservation-clamping branch from 84b7eaf to bfd668c Compare February 20, 2026 08:11
@sandersaares sandersaares marked this pull request as ready for review February 20, 2026 08:19
@sandersaares sandersaares marked this pull request as draft February 20, 2026 08:20
@sandersaares sandersaares marked this pull request as ready for review February 20, 2026 08:20
@sandersaares sandersaares merged commit 158febf into main Feb 20, 2026
27 checks passed
@sandersaares sandersaares deleted the feat/bytesbuf-writer-reservation-clamping branch February 20, 2026 12:33
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.

feat(bytesbuf): clamp minimum reservation size by BytesBuf::into_writer()

3 participants

Comments