Skip to content

vector: let a storage opt into shrink_to_fit - #164

Merged
psiha merged 1 commit into
masterfrom
feat/vector-shrink-to-fit-storage-opt-in
Sep 10, 2026
Merged

vector: let a storage opt into shrink_to_fit#164
psiha merged 1 commit into
masterfrom
feat/vector-shrink-to-fit-storage-opt-in

Conversation

@psiha

@psiha psiha commented Sep 10, 2026

Copy link
Copy Markdown
Owner

Problem

vector::shrink_to_fit() is

void shrink_to_fit() noexcept { this->storage_shrink_to( this->size() ); }

For 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 when live_size() == target_size, and the size is already the target. So a vm_vector that 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, but vector'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 to mem_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, keeping storage_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.
  • Documented in the VectorStorage contract's optional-methods list.

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

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.
@psiha
psiha merged commit a6b3c1c into master Sep 10, 2026
10 checks passed
@psiha
psiha deleted the feat/vector-shrink-to-fit-storage-opt-in branch September 10, 2026 13: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.

1 participant