Repository navigation
Bound reserve() ghost headroom by additional - #125
Merged
Merged
Conversation
The previous reserve() unconditionally added the full per-shard capacity_non_resident as ghost headroom. On a cache with a large estimated_items_capacity, a small reserve(n) call would then allocate the entire ghost cap (potentially gigabytes), not space for n entries. Cap the ghost term at additional: n insertions evict at most n residents into ghosts, and ghosts are also bounded by capacity_non_resident, so min(additional, capacity_non_resident) is the true upper bound. Full pre-loads (additional >= cap) are unchanged; small reserves no longer over-allocate.
arthurprs
force-pushed
the
reserve-cap-ghost-headroom
branch
from
June 24, 2026 21:58
f837cb8 to
b4f9e37
Compare
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.
Follow-up to #123.
Problem
#123 changed
CacheShard::reserveto add the full per-shardcapacity_non_residentas ghost headroom:capacity_non_residentis a fixed per-shard value (shard_estimated_items * ghost_allocation), independent of theadditionalargument. So a smallreserveon a cache with a largeestimated_items_capacityover-allocates massively: e.g.estimated_items_capacity = 1e9, 64 shards,ghost_allocation = 0.5→ ~7.8M ghost slots/shard, socache.reserve(1000)allocates ~40 GB instead of space for 1000 entries.Fix
Cap the ghost term at
additional:This is the true upper bound, not just a clamp:
additionalinsertions evict at mostadditionalresidents into ghosts, and ghosts are also hard-capped atcapacity_non_resident, somin(additional, capacity_non_resident)is the most ghost slots those insertions can need.additional >= capacity_non_resident):additional + capacity_non_resident— unchanged from Pre-allocate the slab in reserve() #123, the intended case.additional < capacity_non_resident): bounded at2·additional— no blowup.Tests
reserve_caps_ghost_headroomtest:reserve(100)on a shard withcapacity_non_resident = 500_000reserves <1000 slab slots.LinkedSlab::capacity()accessor.