code wiki / _hdl_build / nx_doccount_gate.nx
nx_doccount_gate.nx source
↩ module page · 80 lines · 4281 B
1// nx_doccount_gate.nx -- THE CORPUS-SIZE FAST PATH, PROVEN AGAINST THE WALK ON THE LIVE SHARD (search L5, 2026-09-14).
2//
3// WHY. idf needs N, the number of doc: keys across the live segments. ss_doc_count used to WALK every .keys entry
4// of every segment on every query; the phase timers measured that at 324 ms of a 331 ms prep, on every query,
5// warm or cold -- 98 percent of prep and the largest cold term left after L0 and L3. The cut: each row carries its
6// own count from load time in its aux row (ss_doccount_build, one predicate ss_is_doc_key shared with the walk)
7// and ss_doc_count sums them, falling back to the walk when any row lacks one. Ranking is byte-identical only if
8// the sum IS the walk, so this gate opens the REAL shard and asserts that -- plus a poisoned-row neg-control that
9// proves the fast path is what answers, and a cleared-row neg-control that proves the fallback still answers.
10// license_tier: ORIGINAL No hw writes (Rule 26).
11import "nx_seg_store.nx"
12import "nx_gate_verdict.nx"
13
14const DG_POISON: i64 = 1000
15
16func main(argc: i64, argv: *i64) -> i64 {
17 let ctr: *i64 = gv_ctr()
18 gv_head("nx_doccount_gate -- the per-row doc-key sum equals the whole-shard walk on the live shard, and the fast path is the one that answers" as *u8)
19 var prefix: *u8 = "knowledge/store/dp-web-pub-" as *u8
20 let anch: i64 = sys_openat_rd("../knowledge/store/dp-web-pub-manifest.txt" as *u8)
21 if anch >= 0 { sys_close(anch); prefix = "../knowledge/store/dp-web-pub-" as *u8 }
22 if argc >= 2 { prefix = argv[1] as *u8 }
23 gv_puts(" shard prefix=" as *u8); gv_puts(prefix); gv_puts("\n" as *u8)
24 let h: *i64 = ss_open2(prefix, 1)
25 var t1: i64 = 0
26 if (h as i64) != 0 { t1 = 1 }
27 gv_check("T1 the shard opens (nothing below means anything if it does not)" as *u8, t1, ctr)
28 if t1 == 0 {
29 let rcx: i64 = gv_verdict("DOCCOUNT" as *u8, ctr, "corpus-size fast path against the walk" as *u8)
30 sys_exit(rcx)
31 return rcx
32 }
33 let ns: i64 = h[0]
34 gv_check("T2 the shard has MULTIPLE segments so the sum has more than one term" as *u8, (ns > 1) as i64, ctr)
35 let w0: i64 = sys_now_us()
36 let walk: i64 = ss_doc_count_walk(h)
37 let w1: i64 = sys_now_us()
38 let fast: i64 = ss_doc_count(h)
39 let w2: i64 = sys_now_us()
40 var rows: i64 = 0
41 var rowsum: i64 = 0
42 var q: i64 = 0
43 while q < ns {
44 let r: *i64 = ss_aux_row(h, q)
45 if (r as i64) != 0 { if r[SS_AUX_DOCN] > 0 { rows = rows + 1; rowsum = rowsum + (r[SS_AUX_DOCN] - 1) } }
46 q = q + 1
47 }
48 gv_kv("segments" as *u8, ns)
49 gv_kv("doc_count_walk" as *u8, walk)
50 gv_kv("doc_count_fast" as *u8, fast)
51 gv_kv("rows_with_count" as *u8, rows)
52 gv_kv("walk_us" as *u8, w1 - w0)
53 gv_kv("fast_us" as *u8, w2 - w1)
54 gv_check("T3 fixture-reached-condition: the walk counts at least one doc key" as *u8, (walk > 0) as i64, ctr)
55 gv_check("T4 every row carries a count from load time" as *u8, (rows == ns) as i64, ctr)
56 gv_check("T5 THE NUMBER: the fast path equals the walk" as *u8, (fast == walk) as i64, ctr)
57 gv_check("T6 partition: the per-row counts sum to the walk" as *u8, (rowsum == walk) as i64, ctr)
58 // neg-controls on row 0: the fast path must be the one answering, and the fallback must still answer
59 let r0: *i64 = ss_aux_row(h, 0)
60 var t7: i64 = 0
61 var t8: i64 = 0
62 var t9: i64 = 0
63 if (r0 as i64) != 0 {
64 let keep: i64 = r0[SS_AUX_DOCN]
65 r0[SS_AUX_DOCN] = keep + DG_POISON
66 let poisoned: i64 = ss_doc_count(h)
67 if poisoned == walk + DG_POISON { t7 = 1 }
68 r0[SS_AUX_DOCN] = keep
69 if ss_doc_count(h) == walk { t8 = 1 }
70 r0[SS_AUX_DOCN] = 0
71 if ss_doc_count(h) == walk { t9 = 1 }
72 r0[SS_AUX_DOCN] = keep
73 }
74 gv_check("T7 neg-control-a-poisoned-row moves the fast answer by exactly its poison (the sum is what answers, not the walk)" as *u8, t7, ctr)
75 gv_check("T8 restoring the row restores the number" as *u8, t8, ctr)
76 gv_check("T9 neg-control-a-cleared-row falls back to the walk and still answers the same number" as *u8, t9, ctr)
77 let rc: i64 = gv_verdict("DOCCOUNT" as *u8, ctr, "corpus-size fast path against the walk on the live shard" as *u8)
78 sys_exit(rc)
79 return rc
80}