sketch_sliding_window.nx
buildroot/runtime/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 · 1 importers
imports: syscalls.nxsketch_types.nx
imported by: sketch_sliding_window_test.nx
structs
| 31 | struct SlidingWindow { |
consts
| 28 | const NX_SW_MIN_W: i64 = 2 |
| 29 | const NX_SW_MAX_W: i64 = 1000000 |
functions
| 44 | func nx_sw_alloc(window: i64) -> *SlidingWindow {
called by 1: main |
| 67 | func nx_sw_recompute_extrema(s: *SlidingWindow) -> i64 { |
| 91 | func nx_sw_push(s: *SlidingWindow, value: i64) -> i64 {
called by 1: main |
| 123 | func nx_sw_sum(s: *SlidingWindow) -> i64 {
called by 1: main |
| 127 | func nx_sw_mean(s: *SlidingWindow) -> i64 { |
| 132 | func nx_sw_count(s: *SlidingWindow) -> i64 {
called by 1: main |
| 136 | func nx_sw_min(s: *SlidingWindow) -> i64 { |
| 144 | func nx_sw_max(s: *SlidingWindow) -> i64 { |
| 152 | func nx_sw_range(s: *SlidingWindow) -> i64 { |
| 160 | func nx_sw_query_mean(s: *SlidingWindow) -> *ApproxI64 { |
| 169 | func nx_sw_memory_bytes(s: *SlidingWindow) -> i64 { |
| 173 | func nx_sw_clear(s: *SlidingWindow) -> i64 {
called by 1: main |