1212
1313namespace ad_utility {
1414
15- // ============================================================================
16- // StableLRUCache
17- // ============================================================================
18- //
15+ // _____________________________________________________________________________
1916// An LRU (Least Recently Used) cache with pointer/reference stability.
20- //
21- // POINTER STABILITY GUARANTEE
22- // ---------------------------
23- // References returned by getOrCompute() remain valid until the referenced
17+ // References returned by `getOrCompute()` remain valid until the referenced
2418// entry is evicted by LRU replacement. This is achieved by pre-reserving the
25- // underlying hash map to prevent rehashing.
26- //
27- // This differs from ad_utility::util::LRUCache which uses absl::flat_hash_map
28- // without pre-reservation, meaning insertions can trigger rehashing and
29- // invalidate all existing references.
30- //
31- // USE CASE
32- // --------
33- // Use this cache when you need to store pointers/references to cached values
34- // for later use (within the same batch of operations). For example,
35- // ConstructTripleGenerator stores pointers to cached strings in a batch
36- // buffer to avoid repeated hash lookups during triple instantiation.
37- //
38- // COMPLEXITY
39- // ----------
40- // - getOrCompute: O(1) average for lookup/insert, O(1) for LRU bookkeeping
41- // - Space: O(capacity) for hash map + O(capacity) for LRU list
42- //
43- // ============================================================================
19+ // underlying hash map to prevent rehashing. This differs from
20+ // `ad_utility::util::LRUCache` which uses `absl::flat_hash_map` w/o
21+ // pre-reservation, meaning insertions can trigger rehashing and invalidate all
22+ // existing references.
4423template <typename K, typename V>
4524class StableLRUCache {
4625 public:
@@ -54,7 +33,6 @@ class StableLRUCache {
5433
5534 // Look up key in cache. On hit, mark as recently used and return reference.
5635 // On miss, compute value, insert (evicting LRU if at capacity), return ref.
57- //
5836 // The returned reference is stable until this entry is evicted by LRU.
5937 // Within a batch where you access at most `capacity` unique keys, all
6038 // returned references remain valid.
0 commit comments