code wiki / _hdl_build / nx_dod_cache_gate.nx
nx_dod_cache_gate.nx source
↩ module page · 49 lines · 3123 B
1// nx_dod_cache_gate.nx -- proves the SoA/ECS cache EXCEEDS vs AoS. System touches 2 of 8 fields over
2// 1024 entities (8B fields, 64B lines). SoA streams 2 component arrays; AoS drags all 8 fields. Measured:
3// SoA uses 4x fewer cache lines (= F/A), advantage GROWS with field count, AoS utilization 25% vs 100%.
4// NEG-CONTROL: a system using ALL 8 fields -> SoA == AoS (PARITY), so the win is honest (selective access
5// only). license_tier: ORIGINAL expect_exit: 0
6import "nx_syscalls.nx"
7import "nx_dod_cache.nx"
8
9func dw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
10func dn(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(1,bb,k); return 0 }
11
12func main() -> i64 {
13 let N: i64 = 1024
14 let BPF: i64 = 8
15 let LINE: i64 = 64
16 var pass: i64 = 0
17 var total: i64 = 0
18
19 // system touches 2 of 8 fields (e.g. a movement system reading position only)
20 let aos8: i64 = dc_aos_lines(N, 8, BPF, LINE) // 1024 lines
21 let soa2: i64 = dc_soa_lines(N, 2, BPF, LINE) // 256 lines
22 total=total+1; if dc_verdict(aos8, soa2) == DC_EXCEEDS { pass=pass+1 } else { dw("D1 FAIL no exceed @2of8\n") }
23 let ratio8f: i64 = aos8 / soa2
24 total=total+1; if ratio8f == 4 { pass=pass+1 } else { dw("D2 FAIL ratio="); dn(ratio8f); dw(" want 4\n") }
25
26 // advantage GROWS with field count: 16 fields, still touch 2 -> 8x
27 let aos16: i64 = dc_aos_lines(N, 16, BPF, LINE) // 2048 lines
28 let ratio16f: i64 = aos16 / soa2
29 total=total+1; if ratio16f > ratio8f { pass=pass+1 } else { dw("D3 FAIL advantage does not grow with fields\n") }
30
31 // NEG-CONTROL: a system using ALL 8 fields -> SoA == AoS -> PARITY (honest)
32 let soa8: i64 = dc_soa_lines(N, 8, BPF, LINE)
33 total=total+1; if dc_verdict(aos8, soa8) != DC_EXCEEDS { pass=pass+1 } else { dw("D4 FAIL claimed exceed when all fields used\n") }
34
35 // utilization
36 let util: i64 = dc_aos_util_permil(2, 8) // 250 permil = 25%
37 total=total+1; if util == 250 { pass=pass+1 } else { dw("D5 FAIL util="); dn(util); dw("\n") }
38
39 dw("=== SoA/ECS vs AoS cache traffic (N="); dn(N); dw(", 8B fields, 64B lines) ===\n")
40 dw(" system touches 2 of 8 fields: AoS="); dn(aos8); dw(" lines SoA="); dn(soa2); dw(" lines advantage="); dn(ratio8f); dw("x\n")
41 dw(" 16 fields, touch 2: AoS="); dn(aos16); dw(" lines SoA="); dn(soa2); dw(" lines advantage="); dn(ratio16f); dw("x (grows)\n")
42 dw(" AoS utilization="); dn(util); dw(" permil (25%) vs SoA 1000 permil (100%)\n")
43 dw(" neg-control (use all 8 fields): SoA="); dn(soa8); dw(" == AoS -> PARITY (honest)\n")
44 dw("DOD-CACHE "); dn(pass); dw("/"); dn(total); dw("\n")
45 if pass == total { dw("DOD-CACHE VERDICT: EXCEEDS (SoA/ECS uses F/A fewer cache lines for selective access; MEASURED, neg-controlled)\n"); sys_exit(0) }
46 dw("DOD-CACHE VERDICT: NOT PROVEN\n")
47 sys_exit(1)
48 return 1
49}