High-performance cache policies and supporting data structures.
Feature: policy-arc
Implemented in src/policy/arc.rs
Adapt between recency and frequency automatically, without fixed partition tuning.
Maintain four lists:
T1: resident, recent (recency)T2: resident, frequent (frequency-ish)B1: ghost history of items evicted from T1B2: ghost history of items evicted from T2ARC maintains a target parameter p that controls the balance between T1 and T2.
Hits in ghost lists adjust p:
B1 ⇒ increase p (favor recency list T1)B2 ⇒ decrease p (favor frequency list T2)Production ARC uses:
K -> NonNull<Node>T1, T2B1, B2 (keys only, backed by GhostList)T1 ⇒ move to T2 head (promote to frequent)T2 ⇒ move within T2 to head (update recency)B1 ⇒ adjust p upward, perform replacement, insert into T2, remove from B1B2 ⇒ adjust p downward, perform replacement, insert into T2, remove from B2T1 and potentially evict via replacement stepChooses victim from T1 vs T2 depending on p and where the last hit occurred:
|T1| >= max(1, p): evict from T1 LRU → move key to B1T2 LRU → move key to B2The parameter p is adjusted based on ghost hits:
B1: increase p by δ = max(1, |B2|/|B1|)B2: decrease p by δ = max(1, |B1|/|B2|)This adaptation mechanism allows ARC to automatically favor recency or frequency depending on the workload’s characteristics.
capacity keys each (2× memory overhead in keys)use cachekit::policy::arc::ARCCore;
use cachekit::traits::CoreCache;
// Create ARC cache with 100 entry capacity
let mut cache = ARCCore::new(100);
// Insert items (go to T1 - recent list)
cache.insert("page1", "content1");
cache.insert("page2", "content2");
// First access promotes to T2 (frequent list)
assert_eq!(cache.get(&"page1"), Some(&"content1"));
// Second access keeps in T2 (MRU position)
assert_eq!(cache.get(&"page1"), Some(&"content1"));
// Check list sizes
println!("T1 len: {}, T2 len: {}", cache.t1_len(), cache.t2_len());
println!("Adaptation parameter p: {}", cache.p_value());
| Metric | Value |
|---|---|
| Get | O(1) |
| Insert | O(1) amortized |
| Remove | O(1) |
| Space | O(n) entries + O(n) ghost keys |
| Scan resistance | High (via ghost lists) |
| Adaptivity | Self-tuning |
cachekit::ds::GhostListp starts at capacity / 2capacity each