Feature: policy-two-q
Reduce scan pollution with a simple two-queue design that approximates "access twice before protection".
Maintain:
A1(in): FIFO queue for newly admitted items (probation)Am: LRU queue for items that were accessed again (protected/main)
Behavior:
- New items go to
A1. - If an item in
A1is accessed again, it moves toAm. - Eviction prefers
A1(discard one-hit-wonders) before evicting fromAm.
Common implementation:
HashMap<K, NodePtr>mapping keys to node pointers- Two doubly-linked lists:
- FIFO list for
A1(newest at front, oldest at back) - LRU list for
Am(MRU at front, LRU at back)
- FIFO list for
Optional (from the original paper):
A1out: "ghost" history of recently evictedA1items to inform admission/tuning (seeTwoQWithGhost)
insert: intoA1front (new items enter; oldest at back for eviction)get:- if in
A1: promote toAmfront (MRU position) - if in
Am: move toAmfront (MRU position)
- if in
evict:- if
A1exceeds its target size: evict fromA1back (oldest) - otherwise: evict from
Amback (LRU) - fallback: evict from
A1even if under cap whenAmis empty
- if
- O(1) operations with doubly-linked lists + hashmap index
- Requires tuning partition sizes (
|A1|vs|Am|) viaa1_fracparameter
- Johnson, Shasha (1994): "2Q: A Low Overhead High Performance Buffer Management Replacement Algorithm".
- Wikipedia: https://en.wikipedia.org/wiki/Cache_replacement_policies