code wiki / (root) / sketch_space_saving.nx

sketch_space_saving.nx

buildroot/runtime/sketch_space_saving.nx

7186 B220 linesdepth 4pulls 4 transitivereach 2 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 · 2 importers

syscalls.nx sketch_types.nx sketch_space_saving.nx sketch_mg_ss_bracket_bench.nx sketch_space_saving_test.nx

imports: syscalls.nxsketch_types.nx

imported by: sketch_mg_ss_bracket_bench.nxsketch_space_saving_test.nx

structs

34struct SsCounter {
40struct SpaceSaving {

consts

30const NX_SS_K_MIN: i64 = 2
31const NX_SS_K_MAX: i64 = 100000

functions

49func nx_ss_alloc(k: i64) -> *SpaceSaving {
called by 2: mainmain
65func nx_ss_find_key(s: *SpaceSaving, key: i64) -> i64 {
76func nx_ss_find_min(s: *SpaceSaving) -> i64 {
called by 1: nx_ss_add
97func nx_ss_add(s: *SpaceSaving, key: i64, count: i64) -> i64 {
126func nx_ss_estimate(s: *SpaceSaving, key: i64) -> i64 {
called by 3: mainnx_ss_querymain calls 1: nx_ss_find_key
133func nx_ss_lower_bound(s: *SpaceSaving, key: i64) -> i64 {
called by 2: mainmain calls 1: nx_ss_find_key
140func nx_ss_error(s: *SpaceSaving, key: i64) -> i64 {
called by 1: main calls 1: nx_ss_find_key
149func nx_ss_max_overcount(s: *SpaceSaving) -> i64 {
called by 1: nx_ss_query
153func nx_ss_query(s: *SpaceSaving, key: i64) -> *ApproxI64 {
167func if_min(a: i64, b: i64) -> i64 {
called by 1: nx_ss_top_k
172func nx_ss_top_k(s: *SpaceSaving, n: i64, out_keys: *i64, out_counts: *i64) -> i64 {
called by 1: main calls 1: if_min
218func nx_ss_memory_bytes(s: *SpaceSaving) -> i64 {