High-performance cache policies and supporting data structures.
A “lazy heap” pattern keeps:
(score, key) snapshots that may contain stale entriesOn pop(), you discard stale heap entries until the top matches the authoritative score.
Used for:
scores: HashMap<K, Score> (authoritative)heap: BinaryHeap<Reverse<(Score, K)>> (min-heap) or max-heap depending on policyupdate(key, new_score):
scores[key] = new_scoreheap.push((new_score, key))pop_best():
(score, key) matches scores[key], accept itupdate: O(log n)pop_best: O(log n) amortized, but can do multiple pops if many stale entries existIf heap.len() grows much larger than scores.len(), rebuild:
scores itemThis keeps memory bounded and improves worst-case pop latency.