Skip to main content

cranpose_render_common/
bounded_lru_cache.rs

1use std::{hash::Hash, num::NonZeroUsize};
2
3use cranpose_core::collections::map::HashMap;
4
5struct CacheSlot<K, V> {
6    key: K,
7    value: V,
8    newer: Option<usize>,
9    older: Option<usize>,
10}
11
12/// Small bounded LRU cache used by renderer hot-path caches.
13///
14/// Hits update recency in place, so the common path is a single hash lookup.
15/// Eviction unlinks the oldest entry, which costs the same whether the cache
16/// holds ten entries or ten thousand.
17///
18/// The recency order is a linked list rather than a timestamp per entry
19/// because a timestamp makes eviction a scan for the minimum. These caches are
20/// large -- thousands of glyph masks -- and the workloads that need them most
21/// are the ones that miss steadily: text whose size animates re-rasterises
22/// every glyph of every frame, and every one of those inserts was walking the
23/// whole table to decide what to drop.
24///
25/// A key is held twice, once in the index and once in its slot, so an eviction
26/// can find the index entry to remove without searching for it. The keys these
27/// caches use are small `Copy` structs, and the duplicate is what keeps the
28/// links free of raw pointers.
29pub struct BoundedLruCache<K, V> {
30    index: HashMap<K, usize>,
31    slots: Vec<Option<CacheSlot<K, V>>>,
32    free: Vec<usize>,
33    newest: Option<usize>,
34    oldest: Option<usize>,
35    cap: NonZeroUsize,
36}
37
38impl<K, V> BoundedLruCache<K, V>
39where
40    K: Clone + Eq + Hash,
41{
42    pub fn new(cap: NonZeroUsize) -> Self {
43        Self {
44            index: HashMap::with_capacity_and_hasher(cap.get(), Default::default()),
45            slots: Vec::with_capacity(cap.get()),
46            free: Vec::new(),
47            newest: None,
48            oldest: None,
49            cap,
50        }
51    }
52
53    pub fn with_capacity_at_least_one(cap: usize) -> Self {
54        let cap = NonZeroUsize::new(cap).unwrap_or(NonZeroUsize::MIN);
55        Self::new(cap)
56    }
57
58    pub fn len(&self) -> usize {
59        self.index.len()
60    }
61
62    pub fn is_empty(&self) -> bool {
63        self.index.is_empty()
64    }
65
66    pub fn cap(&self) -> NonZeroUsize {
67        self.cap
68    }
69
70    pub fn contains(&self, key: &K) -> bool {
71        self.index.contains_key(key)
72    }
73
74    pub fn get(&mut self, key: &K) -> Option<&V> {
75        let slot = *self.index.get(key)?;
76        self.promote(slot);
77        Some(&self.slot(slot).value)
78    }
79
80    pub fn peek(&self, key: &K) -> Option<&V> {
81        let slot = *self.index.get(key)?;
82        Some(&self.slot(slot).value)
83    }
84
85    pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
86        let slot = *self.index.get(key)?;
87        self.promote(slot);
88        Some(&mut self.slot_mut(slot).value)
89    }
90
91    pub fn push(&mut self, key: K, value: V) -> Option<(K, V)> {
92        if let Some(&slot) = self.index.get(&key) {
93            self.promote(slot);
94            let old_value = std::mem::replace(&mut self.slot_mut(slot).value, value);
95            return Some((key, old_value));
96        }
97
98        let evicted = if self.index.len() == self.cap.get() {
99            self.pop_lru()
100        } else {
101            None
102        };
103
104        let slot = self.claim_slot(key.clone(), value);
105        self.index.insert(key, slot);
106        self.link_newest(slot);
107        evicted
108    }
109
110    pub fn put(&mut self, key: K, value: V) -> Option<V> {
111        self.push(key, value).map(|(_, value)| value)
112    }
113
114    pub fn pop_lru(&mut self) -> Option<(K, V)> {
115        let slot = self.oldest?;
116        self.unlink(slot);
117        let entry = self.release_slot(slot);
118        self.index.remove(&entry.key);
119        Some((entry.key, entry.value))
120    }
121
122    pub fn pop(&mut self, key: &K) -> Option<V> {
123        let slot = self.index.remove(key)?;
124        self.unlink(slot);
125        Some(self.release_slot(slot).value)
126    }
127
128    /// Entries most recently used first.
129    pub fn iter(&self) -> impl Iterator<Item = (&K, &V)> {
130        let mut next = self.newest;
131        std::iter::from_fn(move || {
132            let entry = self.slot(next?);
133            next = entry.older;
134            Some((&entry.key, &entry.value))
135        })
136    }
137
138    fn slot(&self, slot: usize) -> &CacheSlot<K, V> {
139        self.slots[slot]
140            .as_ref()
141            .expect("a linked cache slot is always occupied")
142    }
143
144    fn slot_mut(&mut self, slot: usize) -> &mut CacheSlot<K, V> {
145        self.slots[slot]
146            .as_mut()
147            .expect("a linked cache slot is always occupied")
148    }
149
150    fn promote(&mut self, slot: usize) {
151        if self.newest == Some(slot) {
152            return;
153        }
154        self.unlink(slot);
155        self.link_newest(slot);
156    }
157
158    fn link_newest(&mut self, slot: usize) {
159        let previous_newest = self.newest;
160        {
161            let entry = self.slot_mut(slot);
162            entry.newer = None;
163            entry.older = previous_newest;
164        }
165        if let Some(previous) = previous_newest {
166            self.slot_mut(previous).newer = Some(slot);
167        }
168        self.newest = Some(slot);
169        if self.oldest.is_none() {
170            self.oldest = Some(slot);
171        }
172    }
173
174    fn unlink(&mut self, slot: usize) {
175        let (newer, older) = {
176            let entry = self.slot_mut(slot);
177            (entry.newer.take(), entry.older.take())
178        };
179        match newer {
180            Some(newer) => self.slot_mut(newer).older = older,
181            None => self.newest = older,
182        }
183        match older {
184            Some(older) => self.slot_mut(older).newer = newer,
185            None => self.oldest = newer,
186        }
187    }
188
189    fn claim_slot(&mut self, key: K, value: V) -> usize {
190        let entry = CacheSlot {
191            key,
192            value,
193            newer: None,
194            older: None,
195        };
196        match self.free.pop() {
197            Some(slot) => {
198                self.slots[slot] = Some(entry);
199                slot
200            }
201            None => {
202                self.slots.push(Some(entry));
203                self.slots.len() - 1
204            }
205        }
206    }
207
208    fn release_slot(&mut self, slot: usize) -> CacheSlot<K, V> {
209        let entry = self.slots[slot]
210            .take()
211            .expect("a slot being released is always occupied");
212        self.free.push(slot);
213        entry
214    }
215}
216
217#[cfg(test)]
218#[path = "tests/bounded_lru_cache_tests.rs"]
219mod tests;