nx_bench_hll_throughput.nx source
↩ module page · 115 lines · 3420 B
1// nx_bench_hll_throughput.nx -- head-to-head HLL throughput bench.
2//
3// Per user directive 2026-05-14: "use all of this to beat apache data
4// sketches, we are slower and worse and we want to prove not only are
5// we the leader in math but it applies all the way up the chain in
6// performance".
7//
8// Drives nx_sketch_hll with a deterministic xorshift64 PRNG key
9// stream, measures update_count + HLL estimate + monotonic-clock
10// elapsed; the script computes items/sec from update_count / elapsed.
11//
12// Uses the proven print_i64 / println / nx_clock_monotonic_ns
13// helpers; no per-bench reimplementation.
14//
15// genealogy_id: substrate_perf_bench_2026_05_14
16// lineage_id: incumbent_displacement_proof
17
18// nx_safety_envelope:
19// intended_use: AUTO_APPLIED -- primitive-specific tuning queued
20// sil_target: SIL1
21// evidence: [bulk_applied_2026-05-16, see-file-comment-for-detail]
22// verdict: NOT_YET_EVALUATED
23
24import "nx_syscalls.nx"
25import "nx_runtime.nx"
26import "nx_tier.nx"
27import "nx_clock.nx"
28import "nx_sketch_hll.nx"
29const NX_MAGIC_1000000000: i64 = 1000000000
30const NX_MAGIC_1000000: i64 = 1000000
31
32const NX_BENCH_HLL_N: nx_int = 100000
33const NX_BENCH_HLL_LGK: nx_int = 10
34const NX_BENCH_HLL_SEED: nx_int = 1442695040888963407
35
36func nx_bench_xorshift64(state_p: *nx_int) -> nx_int {
37 var x: nx_int = state_p[0]
38 x = x ^ (x << 13)
39 x = x ^ (x >> 7)
40 x = x ^ (x << 17)
41 state_p[0] = x
42 return x
43}
44
45func nx_bench_i64_to_bytes(v: nx_int, out: *u8) {
46 out[0] = (v & 0xFF) as u8
47 out[1] = ((v >> 8) & 0xFF) as u8
48 out[2] = ((v >> 16) & 0xFF) as u8
49 out[3] = ((v >> 24) & 0xFF) as u8
50 out[4] = ((v >> 32) & 0xFF) as u8
51 out[5] = ((v >> 40) & 0xFF) as u8
52 out[6] = ((v >> 48) & 0xFF) as u8
53 out[7] = ((v >> 56) & 0xFF) as u8
54}
55
56func main() -> nx_exit {
57 let h: *Hll = nx_hll_alloc(NX_BENCH_HLL_LGK, NX_BENCH_HLL_SEED)
58 if h == (0 as *Hll) { return 1 }
59
60 let state_p: *nx_int = (sys_mmap(8)) as *nx_int
61 state_p[0] = NX_BENCH_HLL_SEED
62
63 let key_buf: *u8 = sys_mmap(8)
64
65 let t0: nx_int = nx_clock_monotonic_ns()
66
67 var i: nx_int = 0
68 while i < NX_BENCH_HLL_N {
69 let v: nx_int = nx_bench_xorshift64(state_p)
70 nx_bench_i64_to_bytes(v, key_buf)
71 nx_hll_add(h, key_buf, 8)
72 i = i + 1
73 }
74
75 let t1: nx_int = nx_clock_monotonic_ns()
76 let elapsed_ns: nx_int = t1 - t0
77 let est: nx_int = nx_hll_estimate(h)
78
79 print("update_count=" as *u8)
80 print_i64(NX_BENCH_HLL_N)
81 println("" as *u8)
82
83 print("estimated_n=" as *u8)
84 print_i64(est)
85 println("" as *u8)
86
87 print("lg_k=" as *u8)
88 print_i64(NX_BENCH_HLL_LGK)
89 println("" as *u8)
90
91 print("elapsed_ns=" as *u8)
92 print_i64(elapsed_ns)
93 println("" as *u8)
94
95 // Items per second = N * 1e9 / elapsed_ns (computed by script to
96 // avoid nx_int overflow at large N).
97 if elapsed_ns > 0 {
98 let ips: nx_int = (NX_BENCH_HLL_N * NX_MAGIC_1000000000) / elapsed_ns
99 print("items_per_sec=" as *u8)
100 print_i64(ips)
101 println("" as *u8)
102 }
103
104 // Relative error in ppm.
105 if est > 0 {
106 var diff: nx_int = est - NX_BENCH_HLL_N
107 if diff < 0 { diff = 0 - diff }
108 let rel_ppm: nx_int = (diff * NX_MAGIC_1000000) / NX_BENCH_HLL_N
109 print("rel_error_ppm=" as *u8)
110 print_i64(rel_ppm)
111 println("" as *u8)
112 }
113
114 return 0
115}