Skip to content

feat(buffer): add BufferMut::shrink_to_fit - #11454

Merged
robert3005 merged 5 commits into
developfrom
ji/kind-dijkstra-zb1akm
Nov 19, 2026
Merged

robert3005 merged 5 commits into
developfrom
ji/kind-dijkstra-zb1akm

Conversation

@joseph-isaacs

@joseph-isaacs joseph-isaacs commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A buffer that grows by appending can have up to 2x its length in capacity, and freeze keeps all of it. This adds BufferMut::shrink_to_fit, which uses realloc to release the spare capacity in place, so callers such as FsstSink::finish can trim a buffer before they freeze it.

  • Allocation::shrink forwards to Allocator::shrink. If the allocator refuses, the allocation does not change.
  • shrink_to_fit keeps the data at its offset, and moves it back to an aligned offset if realloc misaligns it. A sliced buffer is copied once into a new allocation. An empty buffer becomes unallocated. A second call does nothing.
  • Tests use an allocator that moves each shrunk block to a misaligned address. New shrink_to_fit and shrink_by_copy benchmarks are in benches/allocation.rs.

On glibc, shrink_to_fit is 30–550x faster than a copy (for example, 4 MiB: 663 ns vs 366 µs). mimalloc copies on realloc, so there it costs the same as a copy.

API Changes

Adds BufferMut::shrink_to_fit. After a shrink, the data has only the buffer's own alignment, not the DEFAULT_ALIGNMENT over-alignment. Growth already has the same guarantee.

joseph-isaacs and others added 2 commits November 13, 2026 15:49
Shrink a mutable buffer's allocation in place with the allocator's
`shrink` (a realloc) instead of copying into an exactly sized buffer.
When the allocation is less aligned than the buffer, keep up to
`alignment - 1` bytes of padding so the data can be moved back to an
aligned offset if the allocator moves the block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UeniBBwV6kFoHCGWSM96FM
Signed-off-by: Joe Isaacs <joe.isaacs@live.co.uk>
Keep the data at its constructor offset when shrinking so an in-place
realloc copies nothing, and copy a buffer with a slice prefix once into a
new allocation instead of moving it and then reallocating. Add
shrink_to_fit and copy benchmarks at three sizes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UeniBBwV6kFoHCGWSM96FM
Signed-off-by: Joe Isaacs <joe.isaacs@live.co.uk>
@codspeed

codspeed Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ Walltime results may be unreliable

Some benchmarks used the Walltime instrument outside CodSpeed Macro Runners, which can make the results inconsistent.

Learn more about Macro Runners

See affected benchmarks

✅ 1975 untouched benchmarks
⏩ 53 skipped benchmarks1


Comparing ji/kind-dijkstra-zb1akm (41a9414) with develop (7ebd1d3)

Open in CodSpeed

Footnotes

  1. 53 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@robert3005

Copy link
Copy Markdown
Contributor

I think cpu simulation is not going to show us anything

@robert3005 robert3005 added the changelog/feature A new feature label Nov 13, 2026
Miri's symbolic alignment check only trusts an allocation's declared
alignment, so checking the data address directly let a misaligned slice
through after a realloc. Use align_offset, as the constructors do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UeniBBwV6kFoHCGWSM96FM
Signed-off-by: Joe Isaacs <joe.isaacs@live.co.uk>
Comment thread vortex-buffer/benches/allocation.rs Resolved
CPU simulation leaves out the realloc system calls that the shrink
benchmarks measure, so they report nothing useful there.

Signed-off-by: Robert Kruszewski <github@robertk.io>
The no-op check in shrink_to_fit used the largest offset a constructor
can produce, not the real offset, so a buffer with its data at offset 0
kept up to 256 + alignment - 1 bytes of spare capacity. Check against
the real offset instead. To keep a second call a no-op, realign moved
data to the next aligned offset after its old one rather than to the
first aligned offset in the block.

Signed-off-by: Robert Kruszewski <github@robertk.io>
@robert3005
robert3005 merged commit e981976 into develop Nov 19, 2026
85 checks passed
@robert3005
robert3005 deleted the ji/kind-dijkstra-zb1akm branch November 19, 2026 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/feature A new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants