Repository navigation
feat(buffer): add BufferMut::shrink_to_fit - #11454
Merged
Merged
Conversation
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>
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
Contributor
|
I think cpu simulation is not going to show us anything |
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>
robert3005
reviewed
Nov 13, 2026
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
approved these changes
Nov 19, 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
A buffer that grows by appending can have up to 2x its length in capacity, and
freezekeeps all of it. This addsBufferMut::shrink_to_fit, which usesreallocto release the spare capacity in place, so callers such asFsstSink::finishcan trim a buffer before they freeze it.Allocation::shrinkforwards toAllocator::shrink. If the allocator refuses, the allocation does not change.shrink_to_fitkeeps the data at its offset, and moves it back to an aligned offset ifreallocmisaligns it. A sliced buffer is copied once into a new allocation. An empty buffer becomes unallocated. A second call does nothing.shrink_to_fitandshrink_by_copybenchmarks are inbenches/allocation.rs.On glibc,
shrink_to_fitis 30–550x faster than a copy (for example, 4 MiB: 663 ns vs 366 µs). mimalloc copies onrealloc, 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 theDEFAULT_ALIGNMENTover-alignment. Growth already has the same guarantee.