code wiki / (root) / sketch_cuckoo.nx

sketch_cuckoo.nx

buildroot/runtime/sketch_cuckoo.nx

9682 B277 linesdepth 4pulls 4 transitivereach 3 importersview sourcekind sketch/demotopic sketch
docsdependenciesstructsconstsfunctions

about

sketch_cuckoo.nx -- Cuckoo Filter (Fan et al. 2014). Approximate-set-membership primitive with DELETION support -- the headline capability Bloom filters lack. Each item gets one of two candidate buckets (the second derived from the fingerprint via XOR), so lookups check both buckets and inserts fall back to relocation when both are full. Versus Bloom (runtime/bloom.nx already shipped): Bloom: k bit-sets per insert, no delete, false-positive rate ~ (1 - e^(-kn/m))^k Cuckoo: fingerprint slots per bucket, supports delete, false- positive rate ~ 2 * bucket_size / (2^fp_bits) At the same memory + FP rate, Cuckoo is typically more space-efficient than Bloom AND supports deletes. Parameters: n_buckets: power of 2 (for XOR-friendly indexing). Total capacity is roughly 95% * n_buckets * 4. bucket_size: we use 4 (canonical; trade-off well-studied). fingerprint: 8 bits (1 byte per slot). False-positive rate ~ 2*4/256 = 3.1% at high load. 16-bit fingerprints (0.024% FP) are queued for v2. LOSSLESS-LANGUAGE DISCIPLINE (doc 20): Membership query returns ApproxI64 with envelope_kind = NX_ENV_ABS, param_a = 0 (false positives but NEVER false negatives for items truly inserted), conf_ppb = 1e9 - expected_fpr_ppb. Substrate's typed envelope precisely declares "absent" is exact and "present" is bounded-uncertain.

dependencies 2 imports · 3 importers

syscalls.nx sketch_types.nx sketch_cuckoo.nx sketch_cb_vs_cuckoo_delete_bench.n sketch_cuckoo_delete_vs_bloom_benc sketch_cuckoo_test.nx

imports: syscalls.nxsketch_types.nx

imported by: sketch_cb_vs_cuckoo_delete_bench.nxsketch_cuckoo_delete_vs_bloom_bench.nxsketch_cuckoo_test.nx

structs

45struct CuckooFilter {

consts

35const NX_CUCKOO_BUCKET_SIZE: i64 = 4
36const NX_CUCKOO_FP_BITS: i64 = 8
37const NX_CUCKOO_MAX_KICKS: i64 = 500
38const NX_CUCKOO_EMPTY: i64 = 0 // fingerprint 0 = slot empty
41const NX_CUCKOO_LCG_A: i64 = 1103515245
42const NX_CUCKOO_LCG_C: i64 = 12345
43const NX_CUCKOO_LCG_MOD: i64 = 0x7FFFFFFF

functions

59func nx_cuckoo_fingerprint(hash: i64) -> i64 {
73func nx_cuckoo_bucket1(c: *CuckooFilter, hash: i64) -> i64 {
78func nx_cuckoo_fp_hash(fp: i64) -> i64 {
called by 1: nx_cuckoo_bucket2
83func nx_cuckoo_bucket2(c: *CuckooFilter, b1: i64, fp: i64) -> i64 {
92func nx_cuckoo_alloc(n_buckets: i64, seed: i64) -> *CuckooFilter {
called by 3: mainmainmain
114func nx_cuckoo_rng_next(c: *CuckooFilter) -> i64 {
called by 1: nx_cuckoo_insert
122func nx_cuckoo_bucket_addr(c: *CuckooFilter, b: i64) -> *u8 {
126func nx_cuckoo_try_insert_bucket(c: *CuckooFilter, b: i64, fp: i64) -> i64 {
140func nx_cuckoo_bucket_contains(c: *CuckooFilter, b: i64, fp: i64) -> i64 {
150func nx_cuckoo_bucket_remove(c: *CuckooFilter, b: i64, fp: i64) -> i64 {
170func nx_cuckoo_insert(c: *CuckooFilter, hash: i64) -> i64 {
210func nx_cuckoo_contains(c: *CuckooFilter, hash: i64) -> i64 {
225func nx_cuckoo_delete(c: *CuckooFilter, hash: i64) -> i64 {
247func nx_cuckoo_fpr_ppb() -> i64 {
called by 1: nx_cuckoo_query
251func nx_cuckoo_query(c: *CuckooFilter, hash: i64) -> *ApproxI64 {
264func nx_cuckoo_memory_bytes(c: *CuckooFilter) -> i64 {
268func nx_cuckoo_total(c: *CuckooFilter) -> i64 {
274func nx_cuckoo_load_ppt(c: *CuckooFilter) -> i64 {