pub struct HeaplessLruCache<K, V, const N: usize, I: IndexType = u8> {
pub map: FnvIndexMap<K, I, N>,
pub keys: [MaybeUninit<K>; N],
pub values: [MaybeUninit<V>; N],
pub prevs: [I; N],
pub nexts: [I; N],
pub free_head: I,
pub head: I,
pub tail: I,
pub num_entries: I,
}Expand description
A stack-allocated LRU cache with $O(1)$ performance.
§Architecture & Pseudocode
This cache uses a Struct-of-Arrays (SoA) layout combined with an intrusive doubly-linked list and a free list, all residing entirely on the stack.
map: Aheapless::IndexMap<K, I, N>mapping keys to their physical slot indices.keys,values: Storage slots for the actual data (MaybeUninitto avoid genericDefaultbounds).prevs,nexts: Parallel arrays representing the doubly-linked list of LRU order. Thenextsarray also doubles as the singly-linked free list for available slots.head,tail: Pointers to the Most Recently Used (MRU) and Least Recently Used (LRU) slots.free_head: Pointer to the first available empty slot.
§Put Algorithm
1. If key exists in `map` at `idx`:
a. Update `values[idx]`.
b. Promote `idx` to `head` (MRU).
2. Else (new key):
a. If cache is logically full (`len >= cap`), call `pop_lru_internal()`:
i. Read `idx = tail`.
ii. Remove `keys[idx]` from `map`.
iii. Detach `idx` from LRU list, push to `free_head`.
b. If `len == N` (absolute stack capacity), return Err (triggers heap spill in wrapper).
c. Pop `idx` from `free_head`.
d. Write `key` to `keys[idx]` and `value` to `values[idx]`.
e. Insert `key -> idx` into `map`.
f. Attach `idx` to `head` (MRU).Fields§
§map: FnvIndexMap<K, I, N>Automatically generated documentation for this item.
keys: [MaybeUninit<K>; N]Automatically generated documentation for this item.
values: [MaybeUninit<V>; N]Automatically generated documentation for this item.
prevs: [I; N]Automatically generated documentation for this item.
nexts: [I; N]Automatically generated documentation for this item.
free_head: IAutomatically generated documentation for this item.
head: IAutomatically generated documentation for this item.
tail: IAutomatically generated documentation for this item.
num_entries: IAutomatically generated documentation for this item.
Implementations§
Source§impl<K, V, const N: usize, I: IndexType> HeaplessLruCache<K, V, N, I>
impl<K, V, const N: usize, I: IndexType> HeaplessLruCache<K, V, N, I>
Sourcepub fn get<Q>(&mut self, key: &Q) -> Option<&V>
pub fn get<Q>(&mut self, key: &Q) -> Option<&V>
Returns a reference to the value corresponding to the key.
Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Returns a reference to the value corresponding to the key.
Trait Implementations§
Source§impl<K, V, const N: usize, I: IndexType> AnyLruCache<K, V> for HeaplessLruCache<K, V, N, I>
impl<K, V, const N: usize, I: IndexType> AnyLruCache<K, V> for HeaplessLruCache<K, V, N, I>
Source§fn len(&self) -> usize
fn len(&self) -> usize
Source§fn cap(&self) -> NonZeroUsize
fn cap(&self) -> NonZeroUsize
Source§fn put(&mut self, key: K, value: V) -> Option<V>
fn put(&mut self, key: K, value: V) -> Option<V>
Source§fn put_with_cap(
&mut self,
key: K,
value: V,
cap: NonZeroUsize,
) -> (Option<V>, Result<(), (K, V)>)
fn put_with_cap( &mut self, key: K, value: V, cap: NonZeroUsize, ) -> (Option<V>, Result<(), (K, V)>)
(old_value, Result<(), (key, value)>). The result is an error if capacity is reached and the backend cannot grow.Source§fn get<Q>(&mut self, key: &Q) -> Option<&V>
fn get<Q>(&mut self, key: &Q) -> Option<&V>
Source§fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Source§fn peek<Q>(&self, key: &Q) -> Option<&V>
fn peek<Q>(&self, key: &Q) -> Option<&V>
Source§fn pop_lru(&mut self) -> Option<(K, V)>
fn pop_lru(&mut self) -> Option<(K, V)>
Source§fn push(&mut self, key: K, value: V) -> Option<(K, V)>
fn push(&mut self, key: K, value: V) -> Option<(K, V)>
Source§fn pop<Q>(&mut self, key: &Q) -> Option<V>
fn pop<Q>(&mut self, key: &Q) -> Option<V>
Source§fn pop_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
fn pop_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
Source§impl<K: Hash + Eq + Clone, V, const N: usize, I: IndexType> Default for HeaplessLruCache<K, V, N, I>
impl<K: Hash + Eq + Clone, V, const N: usize, I: IndexType> Default for HeaplessLruCache<K, V, N, I>
Source§impl<K, V, const N: usize, I> Extend<(K, V)> for HeaplessLruCache<K, V, N, I>
impl<K, V, const N: usize, I> Extend<(K, V)> for HeaplessLruCache<K, V, N, I>
Source§fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K, V, const N: usize, I> FromIterator<(K, V)> for HeaplessLruCache<K, V, N, I>
impl<K, V, const N: usize, I> FromIterator<(K, V)> for HeaplessLruCache<K, V, N, I>
Source§impl<'a, K, V, const N: usize, I: IndexType> IntoIterator for &'a HeaplessLruCache<K, V, N, I>
impl<'a, K, V, const N: usize, I: IndexType> IntoIterator for &'a HeaplessLruCache<K, V, N, I>
Source§impl<'a, K, V, const N: usize, I: IndexType> IntoIterator for &'a mut HeaplessLruCache<K, V, N, I>
impl<'a, K, V, const N: usize, I: IndexType> IntoIterator for &'a mut HeaplessLruCache<K, V, N, I>
Source§impl<K, V, const N: usize, I: IndexType> IntoIterator for HeaplessLruCache<K, V, N, I>
impl<K, V, const N: usize, I: IndexType> IntoIterator for HeaplessLruCache<K, V, N, I>
Source§impl<'a, K, V, const N: usize, I: IndexType> LruIteratorSupport<'a, K, V> for HeaplessLruCache<K, V, N, I>
impl<'a, K, V, const N: usize, I: IndexType> LruIteratorSupport<'a, K, V> for HeaplessLruCache<K, V, N, I>
Auto Trait Implementations§
impl<K, V, const N: usize, I> Freeze for HeaplessLruCache<K, V, N, I>
impl<K, V, const N: usize, I> RefUnwindSafe for HeaplessLruCache<K, V, N, I>
impl<K, V, const N: usize, I> Send for HeaplessLruCache<K, V, N, I>
impl<K, V, const N: usize, I> Sync for HeaplessLruCache<K, V, N, I>
impl<K, V, const N: usize, I> Unpin for HeaplessLruCache<K, V, N, I>
impl<K, V, const N: usize, I> UnsafeUnpin for HeaplessLruCache<K, V, N, I>
impl<K, V, const N: usize, I> UnwindSafe for HeaplessLruCache<K, V, N, I>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.