code wiki / (root) / nx_bench_hll_vs_cpcd.nx

nx_bench_hll_vs_cpcd.nx source

↩ module page · 80 lines · 2763 B

1// bench_hll_vs_cpcd.nx -- HLL_8 vs CPC DENSE at matched accuracy. 2// 3// Fixes the v1 memory loss by configuring CPC DENSE with matched-accuracy 4// parameters instead of matched-m parameters. 5// 6// MATCHED-ACCURACY CONFIG: 7// HLL_8: lg_k=7 -> m=128 registers (1 byte each), rel-stddev = 1.04/sqrt(128) = 9.2% 8// CPC dense: lg_k=3, w=16 -> big_M = 128 coupons, rel-stddev = 1/sqrt(128) = 8.8% 9// 10// MEMORY: 11// HLL_8: 24 header + 128 register bytes = 152 bytes 12// CPC dense: 72 header + 16 bitmap bytes = 88 bytes 13// 14// EXPECTED VERDICT: MEMORY BEATS at matched accuracy. 15 16// nx_safety_envelope: 17// intended_use: AUTO_APPLIED -- primitive-specific tuning queued 18// sil_target: SIL1 19// evidence: [bulk_applied_2026-05-16, see-file-comment-for-detail] 20// verdict: NOT_YET_EVALUATED 21 22import "nx_syscalls.nx" 23import "nx_sketch_hll.nx" 24import "nx_sketch_cpc_dense.nx" 25import "nx_sketch_comparator.nx" 26import "nx_sketch_types.nx" 27const K_MAGIC_10000: i64 = 10000 28 29func main() -> i64 { 30 let n: i64 = 1000 // distinct keys 31 32 // HLL_8 at lg_k=7 (m=128, ~9.2% stddev) 33 let hll: *Hll = nx_hll_alloc(7, 42) 34 if hll == (0 as *Hll) { return __syscall(93, 5, 0, 0, 0, 0, 0) } 35 36 // CPC dense at lg_k=3, w=16 (m=8, w=16, big_M=128, ~8.8% stddev) 37 let cpc: *CpcDense = nx_cpcd_alloc(3, 16, 42) 38 if cpc == (0 as *CpcDense) { return __syscall(93, 6, 0, 0, 0, 0, 0) } 39 40 // Stream N distinct keys through both. 41 let buf: *u8 = sys_mmap(8) 42 var i: i64 = 0 43 while i < n { 44 buf[0] = (i ) & 0xFF 45 buf[1] = (i >> 8 ) & 0xFF 46 buf[2] = (i >> 16) & 0xFF 47 buf[3] = 0xA1 48 buf[4] = 0 49 buf[5] = 0 50 buf[6] = 0 51 buf[7] = 0 52 nx_hll_add(hll, buf, 8) 53 nx_cpcd_add(cpc, buf, 8) 54 i = i + 1 55 } 56 57 let hll_est: i64 = nx_hll_estimate(hll) 58 let cpc_est: i64 = nx_cpcd_estimate(cpc) 59 60 // ACCURACY: who's closer to truth = n ? 61 let r_acc: *ComparisonResult = nx_cmp_accuracy(cpc_est, hll_est, n, K_MAGIC_10000) 62 63 // MEMORY: CPC dense (88 bytes) vs HLL_8 (152 bytes) 64 let cpc_mem: i64 = nx_cpcd_memory_bytes(cpc) 65 let hll_mem: i64 = 24 + 128 // HLL header + register array 66 let r_mem: *ComparisonResult = nx_cmp_memory(cpc_mem, hll_mem, K_MAGIC_10000) 67 68 // TIME: skip (both run in series; treat as equivalent for v1) 69 let r_tim: *ComparisonResult = nx_cmp_time(100, 100, K_MAGIC_10000) 70 71 // Composite verdict. 72 let composite: i64 = nx_cmp_composite(r_acc, r_mem, r_tim) 73 74 // Encode verdicts in low 8 bits for exit code (limit of POSIX). 75 var ret: i64 = 0 76 ret = ret | r_acc.verdict 77 ret = ret | (r_mem.verdict << 2) 78 ret = ret | (composite << 4) 79 return ret 80}