High-performance cache policies and supporting data structures.
Spec maturity: reference, tla
Executable oracle:
tests/abstract_models/exact/lru.rs(LruOccupancyModel); independent reference:reference/lru.rs(NaiveLruModel). Optional TLA+:Lru.tla— runscripts/run-lru-tlc.sh; see formal/lru/tlc.md.
Least Recently Used cache replacement: evict the key whose most recent access is oldest when space is needed. This spec is independent of LruCore / FastLru internals; implementations must refine it.
Two equivalent formulations:
order: Seq<K> listing resident keys; front = MRU, back = LRU.access: Map<K, ℕ> last-access time per resident key; monotonic clock incremented once per op that records access.This document uses deque notation; the reference model uses timestamps.
order = ⟨⟩ (or access = ∅, clock = 0)capacity = C| Observable | Definition |
|---|---|
resident |
Keys in order |
peek_victim |
Back of order (LRU), or none if empty |
recency_rank(k) |
Index of k in order (0 = MRU); undefined if absent |
hit |
Per op: MustHit / MustMiss from membership |
Timestamp recency rank: sort resident keys by (access[k] desc, k asc); rank = index of k.
Insert(k)k ∈ resident: promote to MRU (move to front / set access[k] = ++clock).|resident| ≥ capacity: evict LRU (pop back / min timestamp), record evicted_on_insert.k at MRU (push front / assign new timestamp).Get(k) / GetMut(k)k ∈ resident: MustHit, promote to MRU.MustMiss, no change.Peek(k)k ∈ resident: MustHit.MustMiss.Touch(k)Get for promotion: promote on hit, MustMiss if absent.Remove(k)k from order / access if present. No promotion.EvictOneorder, victim = Exact(lru).access[k].K by Ord (monotonic clock per op normally keeps timestamps unique).k asc when sorting (timestamp desc, key asc).Op mappingOp |
Cache API | Side effects |
|---|---|---|
Insert(k) |
insert(k, v) |
Promote if resident; else insert/evict |
Get(k) |
get(k) |
Promote on hit |
Peek(k) |
peek(k) |
No promotion |
GetMut(k) |
— | No-op in adapter (LRU tests) |
Touch(k) |
touch(k) |
Promote on hit |
Remove(k) |
remove(k) |
Remove |
EvictOne |
evict_one() |
Evict LRU |
Align with trait hierarchy: Peek must not change recency_rank.