pub struct BoundedLruCache<K, V> { /* private fields */ }Expand description
Small bounded LRU cache used by renderer hot-path caches.
Hits update recency in place, so the common path is a single hash lookup. Eviction unlinks the oldest entry, which costs the same whether the cache holds ten entries or ten thousand.
The recency order is a linked list rather than a timestamp per entry because a timestamp makes eviction a scan for the minimum. These caches are large – thousands of glyph masks – and the workloads that need them most are the ones that miss steadily: text whose size animates re-rasterises every glyph of every frame, and every one of those inserts was walking the whole table to decide what to drop.
A key is held twice, once in the index and once in its slot, so an eviction
can find the index entry to remove without searching for it. The keys these
caches use are small Copy structs, and the duplicate is what keeps the
links free of raw pointers.
The index and slots grow with the entries rather than reserving the bound: most caches of a process never come near it, and a table sized for thousands of entries each is megabytes a small screen never touches.
Implementations§
Source§impl<K, V> BoundedLruCache<K, V>
impl<K, V> BoundedLruCache<K, V>
pub fn new(cap: NonZeroUsize) -> Self
pub fn with_capacity_at_least_one(cap: usize) -> Self
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn cap(&self) -> NonZeroUsize
Sourcepub fn contains<Q>(&self, key: &Q) -> bool
pub fn contains<Q>(&self, key: &Q) -> bool
The lookups take any form of the key the stored key borrows as, so a caller can probe with a borrowed view instead of building an owned key.
pub fn get<Q>(&mut self, key: &Q) -> Option<&V>
pub fn peek<Q>(&self, key: &Q) -> Option<&V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn push(&mut self, key: K, value: V) -> Option<(K, V)>
pub fn put(&mut self, key: K, value: V) -> Option<V>
Sourcepub fn peek_lru(&self) -> Option<(&K, &V)>
pub fn peek_lru(&self) -> Option<(&K, &V)>
The least recently used entry, without touching its recency.