Shipped: three reusable packages for Terraphim’s search and agent substrate, written in Goose — the compiled language from the author of FlatBuffers — and verified not against hand-written expectations but against the Rust crates’ own test suites.

All merged to flashmap-goose on task/19-substrate-packages, PR #20.

Why Goose

We already run flashmap-goose: a Goose port of the FlashMap speculative-decoding substrate, with 25+39 golden checks green. Terraphim’s grep and agent components need the same treatment, but they sit on three crates with exact semantics — you can’t hand-wave “mostly the same”:

The Discipline: Crate Corpus, Not Assertions

Rather than writing tests from our reading of the semantics, we built a small Rust harness that runs the real crates and emits TSV golden vectors:

The Goose tests then replay the TSVs and compare exactly. The oracle is the crate itself — if our understanding of “Standard semantics” was wrong, the corpus says so by name.

What the Corpus Taught Us

The interesting part is what we got wrong initially:

Standard ≠ leftmost-first. Aho-Corasick’s Standard match semantics commit the earliest-ending match, not the longest or earliest-inserted. standard030 (["abcd","abc","ab"] on "abcd") proves it: (2,0,2) — the shortest wins at a shared start.

Zero-width patterns have adjacency rules. Empty patterns match at every position, but a zero-width hit is suppressed when it sits exactly on the end of a committed real match — and after a zero-width commit, the next match must start strictly past it. leftfirst010 (["","a"] on "a" → (0,0,0),(0,1,1)) pins the rule: the empty pattern wins position 0, suppresses "a", then matches again at position 1.

Yield order matters. The overlapping iterator emits real outputs before zero-width hits at the same end position (over320–over340 prove it by pattern index).

None of this is obvious from a casual read of the algorithm. It’s all in the tables, and the tables are now regression armour.

Numbers

Compile time: clean cargo build --release of the reference bench ≈ 2.9 s; goose check on each bench ≈ 12 ms — one reason the iteration loop on the Goose side is pleasant.

Run time, identical workloads both sides (same LCG streams — outputs match bit-for-bit):

metricRustGooseratio
AC scan, 10×1MiB41ms196ms4.8x
B+ insert 200k19ms89ms4.7x
B+ ordered walk0.4ms0.7ms1.7x
ahash 1M keys3.2ms128ms39x

The hash gap is honest: Goose lacks unaligned 128-bit reads, so le64 is a byte loop. Reason-then-measure follow-up. The data-structure ratios are where the port actually lives.

The Optimisation Arc: Closing the Gap

We then ran that follow-up properly — a full optimisation harness: crate-oracle fuzz corpora, property testing, an evolutionary variant sweeper, and a native extern fn tier. The arc:

The optimisation arc

metricbaselineoptimisedRustratio now
ahash 1M keys128ms2.04ms3.2ms0.64x — faster than Rust
fxhash 1M110ms1.05ms1.1ms0.95x — parity
AC scan 10x1MiB196ms52.9ms44ms1.2x — near parity
btree get (batch lanes)89ms17.6ms16ms1.1x — parity

How — the layers that measured, in order:

  1. gs_ld64 gather shape — the codegen folds the s[i..i+8] + byte-OR pattern into memcpy unaligned loads; the for loop form it didn’t
  2. extern fn + c_sources — package-native C for __uint128_t folded multiply and whole hot loops; no compiler fork needed
  3. Batch APIs — one call per million keys instead of one per key: the ABI amortisation was the biggest single win (hash 10x → parity)
  4. Byte-class packed DFA — the aho-corasick crate’s own dense-DFA shape: 256-wide transitions folded to ~30 equivalence classes (8x smaller table, better cache residency)
  5. Batch lanes — 16 interleaved btree descents so the memory system overlaps the per-level dependent loads

Verified, triple-oracle — every optimisation is gated by the same corpora plus a fuzz layer: 8,298 hash vectors from the real crates (incl. regex 1.13.1 testdata mined), 51 random AC cases, 35,000 btree ops across std::BTreeMap and a Python oracle, and self-oracle property tests (native ≡ pure on every random input). Sinks stay bit-identical.

Verification pipeline

The evolutionary sweeper (bench/evolve.py) earned its keep on day one: it found FAN=16 optimal for the flat-blob btree (smaller nodes beat scan-width on this workload) — applied and measured, not guessed. Things tried and reverted after measuring: binary search inside nodes, special-state flags, ocnt gating — all slower here, all documented.

Dedup, and What’s Next

The port killed three duplicated copies of the automaton and thesaurus modules — tgrep, agent, and thesaurus/ (now a package) all consume libs/. The next step is swapping the remaining bespoke structures in terraphim-grep’s Goose port onto these packages, then the agent command tables onto ahashmap + bbtree.