sketch_histogram.nx
buildroot/runtime/sketch_histogram.nx
about
sketch_histogram.nx -- fixed-bin histogram with quantile + merge.
Linear-bin histogram over a CALLER-DECLARED range [min, max). Each
of N buckets holds an exact count. Out-of-range values land in
under_count or over_count.
COMPLEMENTS the quantile sketches:
- sketch_kll: provable uniform rank-error; small footprint
- sketch_tdigest: tail-tight; small footprint
- sketch_histogram: EXACT bucket counts within declared range;
memory = N * 8 bytes (no compression)
Caller picks: histogram for known bounded ranges with audit-grade
counts, sketches for unbounded streams with bounded error.
USE CASES:
- HTTP status code distribution (range [200, 600), 1 bucket per code)
- Latency distribution with known SLA (range [0, 1000ms))
- Score distribution (range [0, 100))
MERGE: count-wise addition across matched ranges. Refused if ranges
differ.
LOSSLESS-LANGUAGE DISCIPLINE:
nx_hist_query_count(value): NX_ENV_ABS with param_a = 0 if in-range,
meaning exact count. For out-of-range we report the under/over
counts honestly.
nx_hist_query_quantile(p): NX_ENV_RANK_ERROR with param_a = bucket
resolution = (max - min) / N expressed in ppb relative to range.
MaturityClass = Production (exact within budget).
dependencies 2 imports · 1 importers
imports: syscalls.nxsketch_types.nx
imported by: sketch_histogram_test.nx
structs
| 37 | struct Histogram { |
consts
| 34 | const NX_HIST_BUCKETS_MIN: i64 = 2 |
| 35 | const NX_HIST_BUCKETS_MAX: i64 = 100000 |
functions
| 50 | func nx_hist_alloc(min_val: i64, max_val: i64, n_buckets: i64) -> *Histogram { |
| 79 | func nx_hist_bucket(h: *Histogram, value: i64) -> i64 { |
| 87 | func nx_hist_add(h: *Histogram, value: i64) -> i64 { |
| 102 | func nx_hist_add_n(h: *Histogram, value: i64, count: i64) -> i64 { |
| 120 | func nx_hist_count_at(h: *Histogram, idx: i64) -> i64 {
called by 1: main |
| 127 | func nx_hist_count_for_value(h: *Histogram, value: i64) -> i64 { |
| 135 | func nx_hist_quantile(h: *Histogram, p_milli: i64) -> i64 { |
| 156 | func nx_hist_query_count_for_value(h: *Histogram, value: i64) -> *ApproxI64 { |
| 165 | func nx_hist_quantile_resolution_ppb(h: *Histogram) -> i64 {
called by 1: nx_hist_query_quantile |
| 169 | func nx_hist_query_quantile(h: *Histogram, p_milli: i64) -> *ApproxI64 { |
| 183 | func nx_hist_merge(a: *Histogram, b: *Histogram) -> *Histogram { |
| 201 | func nx_hist_total(h: *Histogram) -> i64 {
called by 1: main |
| 205 | func nx_hist_in_range(h: *Histogram) -> i64 {
called by 1: main |
| 209 | func nx_hist_memory_bytes(h: *Histogram) -> i64 { |