code wiki / (root) / nx_bench_emit.nx

nx_bench_emit.nx source

↩ module page · 112 lines · 2800 B

1// bench_emit.nx -- stream workload through OUR primitives, emit numbers to stdout. 2// 3// Used by the cross-implementation harness (bench_vs_ds.sh). Writes 4// key=value lines to stdout that the harness parses and compares 5// against Apache DataSketches output. 6 7// nx_safety_envelope: 8// intended_use: AUTO_APPLIED -- primitive-specific tuning queued 9// sil_target: SIL1 10// evidence: [bulk_applied_2026-05-16, see-file-comment-for-detail] 11// verdict: NOT_YET_EVALUATED 12 13import "nx_syscalls.nx" 14import "nx_sketch_hll.nx" 15import "nx_sketch_cpc_dense.nx" 16 17// === decimal printer (i64 to stdout) ============================= 18 19func nx_emit_putc(c: i64) -> i64 { 20 let buf: *u8 = sys_mmap(1) 21 buf[0] = c & 0xFF 22 sys_write(1, buf, 1) 23 return 0 24} 25 26func nx_emit_str(s: *u8, len: i64) -> i64 { 27 sys_write(1, s, len) 28 return 0 29} 30 31func nx_emit_i64(n: i64) -> i64 { 32 if n < 0 { 33 nx_emit_putc(45) // '-' 34 return nx_emit_i64(-n) 35 } 36 if n == 0 { 37 nx_emit_putc(48) // '0' 38 return 0 39 } 40 // Build digits in reverse. 41 let digits: *u8 = sys_mmap(32) 42 var d: i64 = 0 43 var v: i64 = n 44 while v > 0 { 45 digits[d] = (v % 10) + 48 46 v = v / 10 47 d = d + 1 48 } 49 // Print in correct order. 50 while d > 0 { 51 d = d - 1 52 nx_emit_putc(digits[d]) 53 } 54 return 0 55} 56 57func nx_emit_newline() -> i64 { 58 nx_emit_putc(10) 59 return 0 60} 61 62// === main ======================================================== 63 64func main() -> i64 { 65 let n: i64 = 1000 66 67 // OUR HLL_8 at lg_k=7 (m=128, ~9.2% stddev, 24+128 = 152 bytes) 68 let hll: *Hll = nx_hll_alloc(7, 42) 69 70 // OUR CPC dense at lg_k=3, w=16 (big_M=128, ~8.8% stddev, 72+16 = 88 bytes) 71 let cpc: *CpcDense = nx_cpcd_alloc(3, 16, 42) 72 73 // Stream N distinct keys through both. 74 let buf: *u8 = sys_mmap(8) 75 var i: i64 = 0 76 while i < n { 77 buf[0] = (i ) & 0xFF 78 buf[1] = (i >> 8 ) & 0xFF 79 buf[2] = (i >> 16) & 0xFF 80 buf[3] = 0xA1 81 buf[4] = 0 82 buf[5] = 0 83 buf[6] = 0 84 buf[7] = 0 85 nx_hll_add(hll, buf, 8) 86 nx_cpcd_add(cpc, buf, 8) 87 i = i + 1 88 } 89 90 // Output: our_hll_estimate, our_hll_bytes, our_cpc_estimate, our_cpc_bytes 91 nx_emit_str("our_hll_estimate=", 17) 92 nx_emit_i64(nx_hll_estimate(hll)) 93 nx_emit_newline() 94 95 nx_emit_str("our_hll_bytes=", 14) 96 nx_emit_i64(24 + 128) 97 nx_emit_newline() 98 99 nx_emit_str("our_cpc_estimate=", 17) 100 nx_emit_i64(nx_cpcd_estimate(cpc)) 101 nx_emit_newline() 102 103 nx_emit_str("our_cpc_bytes=", 14) 104 nx_emit_i64(nx_cpcd_memory_bytes(cpc)) 105 nx_emit_newline() 106 107 nx_emit_str("workload_n=", 11) 108 nx_emit_i64(n) 109 nx_emit_newline() 110 111 return 0 112}