7 releases

0.2.0 Mar 8, 2024
0.1.5 Sep 28, 2023
0.1.4 May 22, 2023

#250 in Data structures

Download history 5563/week @ 2023-12-23 4560/week @ 2023-12-30 5521/week @ 2024-01-06 5998/week @ 2024-01-13 5882/week @ 2024-01-20 6739/week @ 2024-01-27 4977/week @ 2024-02-03 1012/week @ 2024-02-10 3764/week @ 2024-02-17 5410/week @ 2024-02-24 5236/week @ 2024-03-02 5252/week @ 2024-03-09 9359/week @ 2024-03-16 9102/week @ 2024-03-23 7671/week @ 2024-03-30 8551/week @ 2024-04-06

36,224 downloads per month
Used in 5 crates (2 directly)

Apache-2.0

19KB
301 lines

cmsketch

A count min sketch implementation in Rust.

Inspired by Facebook/CacheLib and Caffeine.

Usage

use cmsketch::CMSketchU32;

const ERROR: f64 = 0.01;
const CONFIDENCE: f64 = 0.95;

fn main() {
    let mut cms = CMSketchU32::new(ERROR, CONFIDENCE);
    for i in 0..10 {
        for _ in 0..i {
            cms.inc(i);
        }
    }
    
    for i in 0..10 {
        assert!(cms.estimate(i) >= i as u32);
    }
    
    cms.halve();
    
    for i in 0..10 {
        assert!(cms.estimate(i) >= (i as f64 * 0.5) as u32);
    }
}

Roadmap

  • simd halve
  • benchmark

lib.rs:

A probabilistic counting data structure that never undercounts items before it hits counter's capacity. It is a table structure with the depth being the number of hashes and the width being the number of unique items. When a key is inserted, each row's hash function is used to generate the index for that row. Then the element's count at that index is incremented. As a result, one key being inserted will increment different indices in each row. Querying the count returns the minimum values of these elements since some hashes might collide.

Users are supposed to synchronize concurrent accesses to the data structure.

E.g. insert(1) hash1(1) = 2 -> increment row 1, index 2 hash2(1) = 5 -> increment row 2, index 5 hash3(1) = 3 -> increment row 3, index 3 etc.

Usage

use cmsketch::CMSketchU32;

const ERROR: f64 = 0.01;
const CONFIDENCE: f64 = 0.95;

fn main() {
    let mut cms = CMSketchU32::new(ERROR, CONFIDENCE);
    for i in 0..10 {
        for _ in 0..i {
            cms.inc(i);
        }
    }
    
    for i in 0..10 {
        assert!(cms.estimate(i) >= i as u32);
    }
    
    cms.halve();
    
    for i in 0..10 {
        assert!(cms.estimate(i) >= (i as f64 * 0.5) as u32);
    }
}

Dependencies