Skip to main content

Module entity_cache

Module entity_cache 

Source
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):

  1. A single RwLock serialised every lookup and every invalidation, so a delete-heavy workload paid one global write lock per row.
  2. Eviction was naïve (“drop the first key the iterator yields”) with no recency tracking, so hot entries could be discarded before cold ones.
  3. 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 by N_SHARDS×. Invalidations from delete_batch only collide with reads on the same shard.
  • Bounded LRU per shard: a HashMap carries the entries and a VecDeque<u64> carries access order. On a hit we mark the key most-recently-used; on capacity overflow we drop the least-recently-used.
  • Counters (AtomicU64 for hits / misses / evictions) drive EntityCache::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§

EntityCache
Sharded, bounded LRU cache for UnifiedStore::get_any lookups.
EntityCacheStats