pub struct TinyLfuWidthCache { /* private fields */ }Expand description
Width cache using W-TinyLFU admission policy.
Architecture:
- Window cache (small LRU, ~1% of capacity): captures recent items.
- Main cache (larger LRU, ~99% of capacity): for frequently accessed items.
- Count-Min Sketch + Doorkeeper: frequency estimation for admission decisions.
- Fingerprint guard: secondary hash per entry to detect hash collisions.
On every access:
- Check main cache → hit? Return value (verify fingerprint).
- Check window cache → hit? Return value (verify fingerprint).
- Miss: compute width, insert into window cache.
On window cache eviction:
- The evicted item becomes a candidate.
- The LRU victim of the main cache is identified.
- If
freq(candidate) > freq(victim), candidate enters main cache (victim is evicted). Otherwise, candidate is discarded.
Frequency tracking uses Doorkeeper → CMS pipeline:
- First access: doorkeeper records.
- Second+ access: CMS is incremented.
Implementations§
Source§impl TinyLfuWidthCache
impl TinyLfuWidthCache
Sourcepub fn new(total_capacity: usize) -> Self
pub fn new(total_capacity: usize) -> Self
Create a new TinyLFU cache with the given total capacity.
The window gets ~1% of capacity (minimum 1), main gets the rest.
Sourcepub fn get_or_compute(&mut self, text: &str) -> usize
pub fn get_or_compute(&mut self, text: &str) -> usize
Get cached width or compute and cache it.
Sourcepub fn get_or_compute_with<F>(&mut self, text: &str, compute: F) -> usize
pub fn get_or_compute_with<F>(&mut self, text: &str, compute: F) -> usize
Get cached width or compute using a custom function.
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Get cache statistics.
Sourcepub fn reset_stats(&mut self)
pub fn reset_stats(&mut self)
Reset statistics.
Sourcepub fn window_len(&self) -> usize
pub fn window_len(&self) -> usize
Number of entries in the window cache.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for TinyLfuWidthCache
impl RefUnwindSafe for TinyLfuWidthCache
impl Send for TinyLfuWidthCache
impl Sync for TinyLfuWidthCache
impl Unpin for TinyLfuWidthCache
impl UnsafeUnpin for TinyLfuWidthCache
impl UnwindSafe for TinyLfuWidthCache
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
Mutably borrows from an owned value. Read more