code wiki / (root) / sketch_sliding_window.nx

sketch_sliding_window.nx

buildroot/runtime/sketch_sliding_window.nx

5234 B181 linesdepth 4pulls 4 transitivereach 1 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 · 1 importers

syscalls.nx sketch_types.nx sketch_sliding_window.nx sketch_sliding_window_test.nx

imports: syscalls.nxsketch_types.nx

imported by: sketch_sliding_window_test.nx

structs

31struct SlidingWindow {

consts

28const NX_SW_MIN_W: i64 = 2
29const NX_SW_MAX_W: i64 = 1000000

functions

44func nx_sw_alloc(window: i64) -> *SlidingWindow {
called by 1: main
67func nx_sw_recompute_extrema(s: *SlidingWindow) -> i64 {
called by 2: nx_sw_minnx_sw_max
91func nx_sw_push(s: *SlidingWindow, value: i64) -> i64 {
called by 1: main
123func nx_sw_sum(s: *SlidingWindow) -> i64 {
called by 1: main
127func nx_sw_mean(s: *SlidingWindow) -> i64 {
132func nx_sw_count(s: *SlidingWindow) -> i64 {
called by 1: main
136func nx_sw_min(s: *SlidingWindow) -> i64 {
144func nx_sw_max(s: *SlidingWindow) -> i64 {
152func nx_sw_range(s: *SlidingWindow) -> i64 {
called by 1: main calls 2: nx_sw_maxnx_sw_min
160func nx_sw_query_mean(s: *SlidingWindow) -> *ApproxI64 {
called by 1: main calls 2: nx_sw_meannx_approx_new
169func nx_sw_memory_bytes(s: *SlidingWindow) -> i64 {
173func nx_sw_clear(s: *SlidingWindow) -> i64 {
called by 1: main