code wiki / (root) / nx_sketch_sliding_window.nx

nx_sketch_sliding_window.nx

buildroot/runtime/nx_sketch_sliding_window.nx

5310 B187 linesdepth 3pulls 3 transitivereach 0 importersview sourcekind sketch/demotopic sketch
docsdependenciesstructsconstsfunctions

about

sketch_sliding_window.nx -- last-W-samples rolling aggregator. Circular buffer of size W with running sum + lazy min/max tracking. Useful for "last 5 minutes / last 100 events" rolling computations when the EXACT window is required (vs EWMA's exponential decay). AGGREGATES MAINTAINED: sum -- running sum (O(1) update) count -- min(window_size, observations) — useful at warm-up min -- minimum (lazy: recompute via full scan when displaced) max -- maximum (lazy: same) USE CASES: - SRE: last-5-minute p99 latency window - finance: 20-period moving average - sensor: rolling-window anomaly thresholds - rate-limiting: requests per fixed window MIN/MAX LAZY STRATEGY: cheaper than monotonic-deque for typical W. Cached min/max valid until the displaced sample equaled the cached value; then we rescan the buffer. Worst-case O(W) on eviction, O(1) amortized for "no extremum eviction." For deterministic O(1)-per-op monotonic-deque variant, swap in v2.

dependencies 2 imports · 0 importers

nx_syscalls.nx nx_sketch_types.nx nx_sketch_sliding_window.nx

imports: nx_syscalls.nxnx_sketch_types.nx

imported by: nobody (leaf or entry point)

structs

37struct SlidingWindow

consts

34const NX_SW_MIN_W: i64 = 2
35const NX_SW_MAX_W: i64 = 1000000

functions

50func nx_sw_alloc(window: i64) -> *SlidingWindow
calls 1: sys_mmap
73func nx_sw_recompute_extrema(s: *SlidingWindow) -> i64
called by 2: nx_sw_minnx_sw_max
97func nx_sw_push(s: *SlidingWindow, value: i64) -> i64
129func nx_sw_sum(s: *SlidingWindow) -> i64
133func nx_sw_mean(s: *SlidingWindow) -> i64
called by 1: nx_sw_query_mean
138func nx_sw_count(s: *SlidingWindow) -> i64
142func nx_sw_min(s: *SlidingWindow) -> i64
called by 1: nx_sw_range calls 1: nx_sw_recompute_extrema
150func nx_sw_max(s: *SlidingWindow) -> i64
called by 1: nx_sw_range calls 1: nx_sw_recompute_extrema
158func nx_sw_range(s: *SlidingWindow) -> i64
166func nx_sw_query_mean(s: *SlidingWindow) -> *ApproxI64
175func nx_sw_memory_bytes(s: *SlidingWindow) -> i64
179func nx_sw_clear(s: *SlidingWindow) -> i64