code wiki / (root) / nx_colframe_gate.nx

nx_colframe_gate.nx source

↩ module page · 106 lines · 5485 B

1// nx_colframe_gate.nx -- proves the zero-copy columnar interchange is REALLY zero-copy and REALLY an 2// interchange: schema round-trips, the column accessor returns a VIEW into the shared buffer (same address 3// twice, inside the buffer, 8-aligned), an analytics organ runs DIRECTLY on that view with byte-identical 4// results, and the frame is an independent SNAPSHOT (mutating the source afterward does not change it). 5// D001 verdict via nx_gate_verdict. expect_exit: 0 license_tier: ORIGINAL 6import "nx_gate_verdict.nx" 7import "nx_colframe.nx" 8import "_hdl_build/nx_analyst_store.nx" 9 10func cg_eq(a: i64, b: i64) -> i64 { if a == b { return 1 } return 0 } 11 12func main() -> i64 { 13 let ctr: *i64 = gv_ctr() 14 gv_head("nx_colframe_gate -- is the columnar interchange truly zero-copy, and truly an interchange?" as *u8) 15 16 let N: i64 = 250 17 let size: *i64 = sys_mmap(8 * N) as *i64 18 let age: *i64 = sys_mmap(8 * N) as *i64 19 let price: *i64 = sys_mmap(8 * N) as *i64 20 var s: i64 = 7 21 var i: i64 = 0 22 while i < N { 23 s = (s * 1103515245 + 12345) & 0x7fffffff 24 let sz: i64 = 50 + (s % 150) 25 s = (s * 1103515245 + 12345) & 0x7fffffff 26 let ag: i64 = s % 40 27 s = (s * 1103515245 + 12345) & 0x7fffffff 28 size[i] = sz 29 age[i] = ag 30 price[i] = 5 * sz - 2 * ag + (s % 20) 31 i = i + 1 32 } 33 let cols: *i64 = sys_mmap(8 * 3) as *i64 34 let names: *i64 = sys_mmap(8 * 3) as *i64 35 cols[0] = size as i64 36 cols[1] = age as i64 37 cols[2] = price as i64 38 names[0] = "size" as *u8 as i64 39 names[1] = "age" as *u8 as i64 40 names[2] = "price" as *u8 as i64 41 42 let total: i64 = cf_encoded_bytes(names, 3, N) 43 let buf: *u8 = sys_mmap(total + 64) 44 let wrote: i64 = cf_encode(cols, names, 3, N, buf) 45 46 // ---- self-describing header ------------------------------------------------------------------ 47 gv_check("T1 the frame is valid (magic + version)" as *u8, cg_eq(cf_valid(buf), 1), ctr) 48 gv_check("T2 ncol round-trips" as *u8, cg_eq(cf_ncol(buf), 3), ctr) 49 gv_check("T3 nrows round-trips" as *u8, cg_eq(cf_nrows(buf), N), ctr) 50 gv_check("T4 cf_encoded_bytes predicted the exact write size" as *u8, cg_eq(wrote, total), ctr) 51 gv_check("T5 column type is i64" as *u8, cg_eq(cf_type(buf, 0), CF_T_I64), ctr) 52 gv_check("T6 columns are addressable BY NAME (find 'price')" as *u8, cg_eq(cf_col_index(buf, "price" as *u8), 2), ctr) 53 gv_check("T6b a missing name returns -1, not a false hit" as *u8, cg_eq(cf_col_index(buf, "nope" as *u8), 0 - 1), ctr) 54 55 // ---- ZERO-COPY: the accessor returns a VIEW into the shared buffer ---------------------------- 56 let pv: *i64 = cf_col(buf, 2) 57 let pv2: *i64 = cf_col(buf, 2) 58 gv_check("T7 two cf_col calls return the SAME address (a view, not a per-call copy)" as *u8, cg_eq(pv as i64, pv2 as i64), ctr) 59 var inside: i64 = 0 60 if (pv as i64) >= (buf as i64) { if (pv as i64) < ((buf as i64) + total) { inside = 1 } } 61 gv_check("T8 the column pointer lies INSIDE the frame buffer (zero-copy, no allocation)" as *u8, inside, ctr) 62 gv_check("T9 the data offset is 8-BYTE ALIGNED (the i64 cast is valid, not UB)" as *u8, cg_eq(cf_data_off(buf, 2) % 8, 0), ctr) 63 64 // ---- FIDELITY: every value read through the view equals the source -------------------------- 65 var fid: i64 = 1 66 let sv: *i64 = cf_col(buf, 0) 67 let av: *i64 = cf_col(buf, 1) 68 i = 0 69 while i < N { 70 if sv[i] != size[i] { fid = 0 } 71 if av[i] != age[i] { fid = 0 } 72 if pv[i] != price[i] { fid = 0 } 73 i = i + 1 74 } 75 gv_check("T10 every value read through the zero-copy view equals the source (all 3 cols x N rows)" as *u8, fid, ctr) 76 77 // ---- CROSS-ORGAN ANALYTICS run DIRECTLY on the view, no deserialization ---------------------- 78 // profile the price column via the frame view, and independently via the original array; identical. 79 let p1: *i64 = sys_mmap(8 * 16) as *i64 80 let p2: *i64 = sys_mmap(8 * 16) as *i64 81 ad_profile(cf_col(buf, 2), N, 10, p1) // analytics on the INTERCHANGE view 82 ad_profile(price, N, 10, p2) // analytics on the original 83 var same_prof: i64 = 1 84 if p1[AD_MEAN] != p2[AD_MEAN] { same_prof = 0 } 85 if p1[AD_MIN] != p2[AD_MIN] { same_prof = 0 } 86 if p1[AD_MAX] != p2[AD_MAX] { same_prof = 0 } 87 if p1[AD_MEDIAN] != p2[AD_MEDIAN] { same_prof = 0 } 88 if p1[AD_STDDEV] != p2[AD_STDDEV] { same_prof = 0 } 89 gv_check("T11 an analytics organ profiles the ZERO-COPY VIEW to byte-identical stats (cross-organ passing works)" as *u8, same_prof, ctr) 90 // and correlation across two views is the real relationship 91 let r_sp: i64 = am_pearson_milli(cf_col(buf, 2), cf_col(buf, 0), N) 92 var t12: i64 = 0 93 if r_sp > 900 { t12 = 1 } 94 gv_check("T12 correlation computed across TWO frame views recovers price~size (r>900)" as *u8, t12, ctr) 95 96 // ---- INTERCHANGE SNAPSHOT: mutating the source afterward must NOT change the frame ----------- 97 let before: i64 = pv[0] 98 price[0] = 999999 99 var t13: i64 = 0 100 if pv[0] == before { if pv[0] != 999999 { t13 = 1 } } 101 gv_check("T13 the frame is an independent SNAPSHOT: mutating the source does not alter the frame view" as *u8, t13, ctr) 102 103 let rc: i64 = gv_verdict("COLFRAME-GATE" as *u8, ctr, "a bits-up zero-copy columnar interchange: self-describing, view-not-copy, analytics run directly on the wire format" as *u8) 104 sys_exit(rc) 105 return rc 106}