High-performance cache policies and supporting data structures.
Feature: policy-clock
Approximate LRU with lower overhead by avoiding strict recency ordering and linked list manipulation.
Maintain entries in a circular buffer (“clock”). Each entry has a reference bit.
get): set ref = 1insert existing): set ref = 1ref = 0 (new entries start unreferenced)ref = 0, evict itref = 1, set ref = 0 (second chance) and continueImplementation uses ClockRing:
index: FxHashMap<K, usize> for O(1) key → slot lookupslots: Vec<Option<Entry<K, V>>> circular buffer
key, value, referenced (bool)#[repr(C)] with referenced first for cache-line optimizationNone indicates empty slothand: usize clock pointer into the ringget(key)referenced = trueinsert(key, value)referenced = true, return old valuereferenced = falseevict()contains(key)remove(key)as_ring())peek(key): Get value without setting reference bittouch(key): Set reference bit without retrieving valuepeek_victim(): Preview next eviction candidatepop_victim(): Manually evict next candidate| Operation | Time | Notes |
|---|---|---|
get |
O(1) | Hash lookup + bit set |
insert |
O(1)* | *Amortized; eviction may sweep |
contains |
O(1) | Hash lookup only |
remove |
O(1) | Hash lookup + clear slot |
#[repr(C)] for cache locality| Aspect | Clock | True LRU |
|---|---|---|
| Access cost | O(1) bit set | O(1) list move |
| Memory layout | Contiguous (cache-friendly) | Scattered nodes |
| Eviction | Approximate LRU | Exact LRU |
| Overhead/entry | ~1 byte (ref bit) | ~16 bytes (2 pointers) |
ClockCache: Implements Send and Sync when K and V are Send/SyncMutex for concurrent accessConcurrentClockRing for a thread-safe alternativeClockRing data structure