Expand description
Sharded, bounded LRU cache for entity lookups by raw id.
Replaces the original store-wide RwLock<HashMap<u64, (String, UnifiedEntity)>>
that backed UnifiedStore::get_any(). The original cache had three problems
flagged in docs/perf/delete-sequential-2026-05-06.md (#85):
- A single
RwLockserialised every lookup and every invalidation, so a delete-heavy workload paid one global write lock per row. - Eviction was naïve (“drop the first key the iterator yields”) with no recency tracking, so hot entries could be discarded before cold ones.
- There was no observability: hit-rate had to be guessed from access patterns. The OLTP DELETE workload (hit-rate ≈ 0%) was indistinguishable from a graph-traversal workload (hit-rate > 0%) at the metric layer.
This module solves all three:
- Sharding by
id & (N_SHARDS - 1)cuts contention byN_SHARDS×. Invalidations fromdelete_batchonly collide with reads on the same shard. - Bounded LRU per shard: a
HashMapcarries the entries and aVecDeque<u64>carries access order. On a hit we mark the key most-recently-used; on capacity overflow we drop the least-recently-used. - Counters (
AtomicU64for hits / misses / evictions) driveEntityCache::hit_rate()for live observability.
§Shape
Same key/value shape as the original cache so callers don’t change:
ⓘ
type Key = u64; // EntityId::raw()
type Value = (String, UnifiedEntity);// (collection, entity)Structs§
- Entity
Cache - Sharded, bounded LRU cache for
UnifiedStore::get_anylookups. - Entity
Cache Stats