vector: let a storage opt into shrink_to_fit - #164
Merged
Conversation
shrink_to_fit() called storage_shrink_to( size() ), which is by construction a no-op for a storage whose spare capacity is a resource in its own right: a file-backed mapping's capacity is its file length, and the size is already the target, so the whole high-water allocation stayed allocated. Storages like that now opt in with an explicit storage_shrink_to_fit(); vm_storage forwards it to mem_mapping:: shrink_to_fit(), which truncates to the page-aligned live length. The generic fallback is unchanged - it is what a storage that reaches its capacity through reallocation needs.
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.
Problem
vector::shrink_to_fit()isFor a storage whose spare capacity is a resource in its own right this is by construction a no-op: a file-backed mapping's capacity is its file length,
mem_mapping::shrink_to()early-returns whenlive_size() == target_size, and the size is already the target. So avm_vectorthat grew large and shrank back kept its whole high-water file allocation, with no way to ask for it back through the container interface.mem_mapping::shrink_to_fit()does the right thing, butvector's own name shadows it.Change
Storages that can actually return capacity opt in with an explicit
storage_shrink_to_fit():vm_storage::storage_shrink_to_fit()forwards tomem_mapping::shrink_to_fit()(truncates the file to the page-aligned live length, unmapping/remapping the view first where views are not downsizeable).vector::shrink_to_fit()prefers it when present, keepingstorage_shrink_to( size() )as the fallback — which is what a storage reaching its capacity through reallocation (heap_storage,sbo_hybrid,fixed) needs, and what those get today.No behaviour change for any storage that does not define the new hook.
Verified
Built and exercised by a downstream workload's test suite (clang-cl 22, Windows, LTO Release + an assert-enabled config): a mapped vector whose live length dropped from 200k to 100 elements now hands the file space back — a container measured at 4,456,544 B of backing files before the call and 28,768 B after, with the content and a subsequent reopen unchanged. Interior free space in higher-level allocators is unaffected, as intended: this only truncates the tail past the live length.
🤖 Generated with Claude Code