Skip to main content

BoundedLruCache

Struct BoundedLruCache 

Source
pub struct BoundedLruCache<K, V> { /* private fields */ }
Expand description

A small bounded LRU cache for 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.

The index holds slot numbers only and compares a probe against the key in its slot, so each key is stored once: a full text cache’s index takes five bytes an entry instead of a second copy of every key.

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>
where K: Eq + Hash,

Source

pub fn new(cap: NonZeroUsize) -> Self

Source

pub fn with_capacity_at_least_one(cap: usize) -> Self

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn cap(&self) -> NonZeroUsize

Source

pub fn set_cap(&mut self, cap: NonZeroUsize)

Sets the bound, evicting the least recently used entries past it.

Source

pub fn clear(&mut self)

Drops every entry and gives back the storage they took.

Source

pub fn contains<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

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.

Source

pub fn get<Q>(&mut self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source

pub fn peek<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source

pub fn push(&mut self, key: K, value: V) -> Option<(K, V)>

Source

pub fn put(&mut self, key: K, value: V) -> Option<V>

Source

pub fn peek_lru(&self) -> Option<(&K, &V)>

The least recently used entry, without touching its recency.

Source

pub fn pop_lru(&mut self) -> Option<(K, V)>

Source

pub fn pop(&mut self, key: &K) -> Option<V>

Source

pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>

Entries most recently used first.

Auto Trait Implementations§

§

impl<K, V> Freeze for BoundedLruCache<K, V>
where Vec<Option<CacheSlot<K, V>>>: Freeze,

§

impl<K, V> RefUnwindSafe for BoundedLruCache<K, V>
where Vec<Option<CacheSlot<K, V>>>: RefUnwindSafe,

§

impl<K, V> Send for BoundedLruCache<K, V>
where Vec<Option<CacheSlot<K, V>>>: Send,

§

impl<K, V> Sync for BoundedLruCache<K, V>
where Vec<Option<CacheSlot<K, V>>>: Sync,

§

impl<K, V> Unpin for BoundedLruCache<K, V>
where Vec<Option<CacheSlot<K, V>>>: Unpin,

§

impl<K, V> UnsafeUnpin for BoundedLruCache<K, V>
where Vec<Option<CacheSlot<K, V>>>: UnsafeUnpin,

§

impl<K, V> UnwindSafe for BoundedLruCache<K, V>
where Vec<Option<CacheSlot<K, V>>>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.