pub struct SlotCache<V: Copy, const N: usize, const MAX_KEY: usize> { /* private fields */ }Expand description
Keyed, fixed-capacity cache with LRU eviction and pinning.
Implementations§
Source§impl<V: Copy, const N: usize, const MAX_KEY: usize> SlotCache<V, N, MAX_KEY>
impl<V: Copy, const N: usize, const MAX_KEY: usize> SlotCache<V, N, MAX_KEY>
Sourcepub fn bump_epoch(&mut self)
pub fn bump_epoch(&mut self)
Advance the LRU clock. Call once per cache cycle (e.g. per frame) so
get touches and eviction comparisons share a monotonic timeline.
Never returns 0, so a freshly touched slot always beats the initial
last_used of an untouched one.
Sourcepub fn slot_of(&self, key: u16) -> Option<usize>
pub fn slot_of(&self, key: u16) -> Option<usize>
Slot index currently mapped to key in any non-empty state, validated
against the forward store (so a stale reverse entry never lies).
Sourcepub fn contains_ready(&self, key: u16) -> bool
pub fn contains_ready(&self, key: u16) -> bool
Is key resident and usable (Ready)?
Sourcepub fn is_loading(&self, key: u16) -> bool
pub fn is_loading(&self, key: u16) -> bool
Is key reserved with a load in flight (Loading)?
Sourcepub fn get(&mut self, key: u16) -> Option<&V>
pub fn get(&mut self, key: u16) -> Option<&V>
Get a Ready value, marking it most-recently-used. Returns None for an
absent or still-Loading key.
Sourcepub fn begin_load(&mut self, key: u16) -> Option<Reservation>
pub fn begin_load(&mut self, key: u16) -> Option<Reservation>
Reserve a slot for key and mark it Loading, evicting the
least-recently-used unpinned Ready slot if the cache is full. Returns
a Reservation for the caller to complete with
finish_load, or None if key is out of range
or every slot is pinned or already loading. If key already has a
slot, a reservation for that slot is returned unchanged (no duplicate
load is started).
Sourcepub fn finish_load(
&mut self,
reservation: Reservation,
value: V,
) -> Result<(), Stale>
pub fn finish_load( &mut self, reservation: Reservation, value: V, ) -> Result<(), Stale>
Complete a begin_load: store the value and mark
the slot Ready and most-recently-used. Returns Stale when the
reservation’s key no longer owns its slot (it was evicted, and the slot
may now be loading another key), and stores nothing.
Sourcepub fn reserve(&mut self, key: u16) -> Option<usize>
👎Deprecated: renamed to begin_load
pub fn reserve(&mut self, key: u16) -> Option<usize>
renamed to begin_load
Renamed to begin_load, which returns the key with
the slot so a stale completion can be told from a live one.
Sourcepub fn mark_ready(&mut self, slot: usize, value: V)
👎Deprecated: renamed to finish_load, which checks the key still owns the slot
pub fn mark_ready(&mut self, slot: usize, value: V)
renamed to finish_load, which checks the key still owns the slot
Renamed to finish_load. A bare slot index cannot
tell that the slot was evicted and reused for another key since
reserve, so a late completion stores its value under
the wrong key; this keeps that behaviour for one stage.
Sourcepub fn get_or_insert_with<F>(&mut self, key: u16, build: F) -> Option<&V>where
F: FnOnce() -> Option<V>,
pub fn get_or_insert_with<F>(&mut self, key: u16, build: F) -> Option<&V>where
F: FnOnce() -> Option<V>,
Return the cached value for key, or synchronously build and insert it.
Evicts LRU on a miss when full. If build returns None the reservation
is released and this returns None.
Sourcepub fn set_pinned(&mut self, keys: &[u16])
pub fn set_pinned(&mut self, keys: &[u16])
Replace the pin set: unpin everything, then pin each present key in
keys. This is the “declare the working set” call a streaming reconcile
makes each cycle.
Sourcepub fn evict_ready_outside(&mut self, keep: &[u16]) -> usize
pub fn evict_ready_outside(&mut self, keep: &[u16]) -> usize
Evict every Ready, unpinned slot whose key is not in keep. Used by a
reconcile to drop rooms that left the desired set while protecting the
pinned working set. Returns the number evicted.