code wiki / _hdl_build / nx_js_safepoint_cost.nx
nx_js_safepoint_cost.nx source
↩ module page · 34 lines · 2190 B
1// nx_js_safepoint_cost.nx -- measure the PURE per-back-edge safepoint overhead on a hot JIT loop. GC-off = no
2// safepoint emitted (baseline). GC-on with a HUGE threshold = safepoint emitted+called every iteration but
3// gc_poll never fires a collection => isolates the call cost. expect_exit: 0 license_tier: ORIGINAL
4import "nx_js_vm.nx"
5const K_MAGIC_1000000000: i64 = 1000000000
6func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
7func nn(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(24); 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(24); 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 }
8func timeloop(src: *u8) -> i64 {
9 let ov: *i64 = sys_mmap(16) as *i64
10 let t0: i64 = sys_now_us()
11 compile_run(src, ov)
12 let t1: i64 = sys_now_us()
13 return t1 - t0
14}
15func main(argc: i64, argv: *i64) -> i64 {
16 w("=== nx_js_safepoint_cost: per-back-edge safepoint overhead on a 5M-iter JIT loop ===\n" as *u8)
17 let src: *u8 = "var s=0;var i=0;while(i<5000000){s=s+i;i=i+1;} s" as *u8
18 let warm: *i64 = sys_mmap(16) as *i64
19 compile_run(src, warm) // warm caches/pages
20 let off1: i64 = timeloop(src)
21 let off2: i64 = timeloop(src)
22 w(" GC-off (JIT, NO safepoint): " as *u8); nn(off1); w("us / " as *u8); nn(off2); w("us jit=" as *u8); nn(jit_ran_get()); w("\n" as *u8)
23 gc_configure(K_MAGIC_1000000000) // 1GB threshold: safepoint runs every back-edge but NEVER collects
24 gc_enable(1)
25 let on1: i64 = timeloop(src)
26 let on2: i64 = timeloop(src)
27 w(" GC-on (JIT, safepoint/it): " as *u8); nn(on1); w("us / " as *u8); nn(on2); w("us jit=" as *u8); nn(jit_ran_get()); w(" GCs=" as *u8); nn(gc_collection_count()); w("\n" as *u8)
28 var best_off: i64 = off1
29 if off2 < best_off { best_off = off2 }
30 var best_on: i64 = on1
31 if on2 < best_on { best_on = on2 }
32 w(" overhead x1000 (on/off): " as *u8); nn(best_on * 1000 / best_off); w(" (1000=none, 2000=2x)\n" as *u8)
33 return 0
34}