Skip to content

Bound reserve() ghost headroom by additional - #125

Merged
arthurprs merged 1 commit into
masterfrom
reserve-cap-ghost-headroom
Jun 24, 2026
Merged

arthurprs merged 1 commit into
masterfrom
reserve-cap-ghost-headroom

Conversation

@arthurprs

Copy link
Copy Markdown
Owner

Follow-up to #123.

Problem

#123 changed CacheShard::reserve to add the full per-shard capacity_non_resident as ghost headroom:

let additional = additional.saturating_add(self.capacity_non_resident);

capacity_non_resident is a fixed per-shard value (shard_estimated_items * ghost_allocation), independent of the additional argument. So a small reserve on a cache with a large estimated_items_capacity over-allocates massively: e.g. estimated_items_capacity = 1e9, 64 shards, ghost_allocation = 0.5 → ~7.8M ghost slots/shard, so cache.reserve(1000) allocates ~40 GB instead of space for 1000 entries.

Fix

Cap the ghost term at additional:

let additional = additional.saturating_add(additional.min(self.capacity_non_resident));

This is the true upper bound, not just a clamp: additional insertions evict at most additional residents into ghosts, and ghosts are also hard-capped at capacity_non_resident, so min(additional, capacity_non_resident) is the most ghost slots those insertions can need.

  • Full pre-load (additional >= capacity_non_resident): additional + capacity_non_resident — unchanged from Pre-allocate the slab in reserve() #123, the intended case.
  • Small reserve (additional < capacity_non_resident): bounded at 2·additional — no blowup.

Tests

  • New reserve_caps_ghost_headroom test: reserve(100) on a shard with capacity_non_resident = 500_000 reserves <1000 slab slots.
  • Adds a test-only LinkedSlab::capacity() accessor.
  • Full lib suite passes (53 tests), clippy clean.

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
arthurprs force-pushed the reserve-cap-ghost-headroom branch from f837cb8 to b4f9e37 Compare June 24, 2026 21:58
@arthurprs
arthurprs merged commit 79fc813 into master Jun 24, 2026
12 checks passed
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