Skip to main content

CountMinSketch

Struct CountMinSketch 

Source
pub struct CountMinSketch { /* private fields */ }
Expand description

A high-concurrency, memory-efficient frequency estimator.

Uses AtomicU16 counters in a 2D matrix to provide a probabilistic upper bound on item frequency. Designed for high-throughput environments where a small overestimation error is acceptable in exchange for wait-free/lock-free performance.

Implementations§

Source§

impl CountMinSketch

Source

pub fn new(columns: usize, rows: usize) -> Self

Creates a new CountMinSketch with the specified logical dimensions.

§Arguments
  • columns - The logical width per row. The actual storage is doubled and rounded to the next power of two to optimize indexing via bitwise masking.
  • rows - The number of independent hash functions.
§Panics

Panics if rows > 8, as it exceeds the available static prime seeds.

Source

pub fn increment<K>(&self, key: &K, context: &ThreadContext)
where K: Eq + Hash + ?Sized,

Increments the frequency estimate for a key across all rows.

Uses a saturating atomic CAS loop to ensure counters never overflow. If contention is detected, the provided backoff is utilized to reduce CPU cache-coherency traffic.

Source

pub fn decrement<K>(&self, key: &K, context: &ThreadContext)
where K: Eq + Hash + ?Sized,

Decrements the frequency estimate for a key, saturating at zero.

Useful for manual aging or correction. The CAS loop prevents integer underflow, which would otherwise erroneously transform a “cold” item into an “ultra-hot” item (65,535).

Source

pub fn decay(&self, context: &ThreadContext)

Performs a global aging operation by halving every counter in the sketch.

This reduces the “weight” of historical data, allowing the sketch to adapt to changes in key distribution over time. Uses a CAS loop per counter to maintain atomicity during the bit-shift.

Source

pub fn contains<K: Eq + Hash>(&self, key: &K) -> bool

Checks if an item is likely present in the sketch (estimate > 0).

Source

pub fn get<K>(&self, key: &K) -> u16
where K: Eq + Hash + ?Sized,

Retrieves the estimated frequency of a key.

The estimate is the minimum value found across all rows for the given key. This is a mathematically guaranteed upper bound of the true frequency.

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, 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V