nx_sketch_sliding_window.nx
buildroot/runtime/nx_sketch_sliding_window.nx
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
imports: nx_syscalls.nxnx_sketch_types.nx
imported by: nobody (leaf or entry point)
structs
| 37 | struct SlidingWindow |
consts
| 34 | const NX_SW_MIN_W: i64 = 2 |
| 35 | const NX_SW_MAX_W: i64 = 1000000 |
functions
| 50 | func nx_sw_alloc(window: i64) -> *SlidingWindow calls 1: sys_mmap |
| 73 | func nx_sw_recompute_extrema(s: *SlidingWindow) -> i64 |
| 97 | func nx_sw_push(s: *SlidingWindow, value: i64) -> i64 |
| 129 | func nx_sw_sum(s: *SlidingWindow) -> i64 |
| 133 | func nx_sw_mean(s: *SlidingWindow) -> i64 called by 1: nx_sw_query_mean |
| 138 | func nx_sw_count(s: *SlidingWindow) -> i64 |
| 142 | func nx_sw_min(s: *SlidingWindow) -> i64 |
| 150 | func nx_sw_max(s: *SlidingWindow) -> i64 |
| 158 | func nx_sw_range(s: *SlidingWindow) -> i64 |
| 166 | func nx_sw_query_mean(s: *SlidingWindow) -> *ApproxI64 |
| 175 | func nx_sw_memory_bytes(s: *SlidingWindow) -> i64 |
| 179 | func nx_sw_clear(s: *SlidingWindow) -> i64 |