code wiki / (root) / nx_sketch_space_saving.nx

nx_sketch_space_saving.nx

buildroot/runtime/nx_sketch_space_saving.nx

7223 B226 linesdepth 3pulls 3 transitivereach 0 importersview sourcekind sketch/demotopic sketch
docsdependenciesstructsconstsfunctions

about

sketch_space_saving.nx -- SpaceSaving top-K heavy hitters. Metwally-Agrawal-El-Abbadi 2005. Bounded set of (key, count, error) triples of capacity K. On add: if key tracked, increment; else if room, insert with count=1, error=0; else evict the minimum-count entry and install (new_key, min_count + count, min_count). Guarantee: any item with true frequency > N/K is tracked (no false negatives for heavy hitters). The reported count over-estimates by at most `error`; true_count is in [count - error, count] for tracked items. COMPLEMENTS CMS: - CMS: per-query frequency for ANY key (overestimate-only). - SpaceSaving: enumerate the top-K most frequent keys directly. Together they cover the full frequency-counting axis. API takes i64 keys (caller hashes strings to i64 via murmur3 if they have string-keyed streams). LOSSLESS-LANGUAGE DISCIPLINE (doc 20): nx_ss_query returns ApproxI64 with envelope_kind = NX_ENV_ABS; param_a holds the per-key error bound (count - error <= true <= count); conf_ppb = 1e9 (deterministic; not probabilistic).

dependencies 2 imports · 0 importers

nx_syscalls.nx nx_sketch_types.nx nx_sketch_space_saving.nx

imports: nx_syscalls.nxnx_sketch_types.nx

imported by: nobody (leaf or entry point)

structs

40struct SsCounter
46struct SpaceSaving

consts

36const NX_SS_K_MIN: i64 = 2
37const NX_SS_K_MAX: i64 = 100000

functions

55func nx_ss_alloc(k: i64) -> *SpaceSaving
calls 1: sys_mmap
71func nx_ss_find_key(s: *SpaceSaving, key: i64) -> i64
82func nx_ss_find_min(s: *SpaceSaving) -> i64
called by 1: nx_ss_add
103func nx_ss_add(s: *SpaceSaving, key: i64, count: i64) -> i64
132func nx_ss_estimate(s: *SpaceSaving, key: i64) -> i64
called by 1: nx_ss_query calls 1: nx_ss_find_key
139func nx_ss_lower_bound(s: *SpaceSaving, key: i64) -> i64
calls 1: nx_ss_find_key
146func nx_ss_error(s: *SpaceSaving, key: i64) -> i64
calls 1: nx_ss_find_key
155func nx_ss_max_overcount(s: *SpaceSaving) -> i64
called by 1: nx_ss_query
159func nx_ss_query(s: *SpaceSaving, key: i64) -> *ApproxI64
173func if_min(a: i64, b: i64) -> i64
called by 1: nx_ss_top_k
178func nx_ss_top_k(s: *SpaceSaving, n: i64, out_keys: *i64, out_counts: *i64) -> i64
calls 2: if_minsys_mmap
224func nx_ss_memory_bytes(s: *SpaceSaving) -> i64