code wiki / _hdl_build / nx_js_speed_bench.nx
nx_js_speed_bench.nx source
↩ module page · 28 lines · 1950 B
1// nx_js_speed_bench.nx -- the BENCHMARK-FIRST baseline for the bytecode-VM arc: measure the tree-walker's
2// throughput on a hot arithmetic loop (the workload that makes production bundles 1000x too slow). The VM
3// must beat this number on the SAME workload -- measured, never asserted. license_tier: ORIGINAL
4import "nx_js_eval.nx"
5const K_MAGIC_200000: i64 = 200000
6const K_MAGIC_1000000: i64 = 1000000
7
8func bw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
9func bn(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m } let t: *u8=sys_mmap(32); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } let o: *u8=sys_mmap(32); var q: i64=k-1; var x: i64=0; while q>=0 { o[x]=t[q]; x=x+1; q=q-1 } sys_write(1,o,x); return 0 }
10func bslen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
11
12func main(argc: i64, argv: *i64) -> i64 {
13 bw("=== nx_js_speed_bench: tree-walker baseline (hot loop) ===\n" as *u8)
14 // s = sum(0..N-1) = N*(N-1)/2 ; N=100000 -> 4999950000 (proves the loop ran fully, no cap)
15 let src: *u8 = "var s=0;var i=0;while(i<100000){s=s+i;i=i+1;}s" as *u8
16 let out: *i64 = sys_mmap(16) as *i64
17 let t0: i64 = sys_now_us()
18 let rc: i64 = js_run_source(src, bslen(src), out)
19 let t1: i64 = sys_now_us()
20 let dus: i64 = t1 - t0
21 bw(" N=100000 while-loop rc="); bn(rc); bw(" result="); if out[0]==VAL_NUM { bn(out[1]) } else { bw("(tag "); bn(out[0]); bw(")" as *u8) }
22 bw(" (expect 4999950000)\n" as *u8)
23 bw(" TIME="); bn(dus); bw(" us ("); bn(dus/1000); bw(" ms)\n" as *u8)
24 // ~200000 statement-evals (2 per iteration). throughput = evals/sec.
25 if dus > 0 { bw(" THROUGHPUT ~"); bn((K_MAGIC_200000 * K_MAGIC_1000000) / dus); bw(" stmt-evals/sec\n" as *u8) }
26 bw("=== this is the number the bytecode VM must beat ===\n" as *u8)
27 return 0
28}