Skip to main content

TinyLfuWidthCache

Struct TinyLfuWidthCache 

Source
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:

  1. Check main cache → hit? Return value (verify fingerprint).
  2. Check window cache → hit? Return value (verify fingerprint).
  3. Miss: compute width, insert into window cache.

On window cache eviction:

  1. The evicted item becomes a candidate.
  2. The LRU victim of the main cache is identified.
  3. 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

Source

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.

Source

pub fn get_or_compute(&mut self, text: &str) -> usize

Get cached width or compute and cache it.

Source

pub fn get_or_compute_with<F>(&mut self, text: &str, compute: F) -> usize
where F: FnOnce(&str) -> usize,

Get cached width or compute using a custom function.

Source

pub fn contains(&self, text: &str) -> bool

Check if a key is in the cache (window or main).

Source

pub fn stats(&self) -> CacheStats

Get cache statistics.

Source

pub fn clear(&mut self)

Clear all caches and reset sketch/doorkeeper.

Source

pub fn reset_stats(&mut self)

Reset statistics.

Source

pub fn len(&self) -> usize

Current number of cached entries.

Source

pub fn is_empty(&self) -> bool

Check if cache is empty.

Source

pub fn capacity(&self) -> usize

Total capacity (window + main).

Source

pub fn main_len(&self) -> usize

Number of entries in the main cache.

Source

pub fn window_len(&self) -> usize

Number of entries in the window cache.

Trait Implementations§

Source§

impl Debug for TinyLfuWidthCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more