Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .claude/board/EPIPHANIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,47 @@
> `**Status:**`/`**Confidence:**` line. A correction gets its own new,
> dated entry that references the one it corrects — the storno rule.

## 2026-08-17 — E-LGJ-VECTOR-API-BEATS-THE-CROSSING-1

**Status:** FINDING. **Confidence:** High (real JMH 1.37, `Data.crossCheck()` guards every fork,
independently cross-checked against a second, mechanically-generated computation of the same CSV).

Completes D-LGJ-G, the mission's mandated "where does execution belong — measure it, do not assume
the Rust side wins" comparison. The honest answer complicates the thesis in a useful way: **for a
single predicate over one native lane, the Java Vector API — reading the SAME native
`MemorySegment` zero-copy via `IntVector.fromMemorySegment`, no `byte[]`, no bounce buffer — beats
the native `lgj_plan_eval` crossing at every row count tested, from 64 to 4,194,304**, by 56.4× at
small sizes down to 1.33-1.41× at the largest:

| rows | native (µs) | vectorApi (µs) | vectorApi wins by |
|---:|---:|---:|---:|
| 64 | 0.612 | 0.011 | 56.40× |
| 65,536 | 15.324 | 8.027 | 1.91× |
| 4,194,304 | 1858.686 | 1319.107 | 1.41× |

A second, separate crossover is also real: native beats a plain Java **scalar** loop only past
roughly 4,096-16,384 rows — below that the crossing's own fixed cost (consistent with Component A's
measured ~22 ns bare-downcall floor) is not yet repaid.

**Why this does not overturn the project's thesis, and where the thesis's own machinery already
shows the real answer.** Component C isolates exactly one predicate, one lane — the case with
nothing to fuse and nothing to coordinate, which is precisely the case a zero-copy Vector kernel is
best at. Component E (multi-predicate fusion) shows the picture change: SIMD-vs-scalar is the
largest lever measured anywhere in this benchmark (10.8×-31.1×, growing with predicate count), and
`fused`/`unfused` land within this harness's own stated ~10% noise floor of each other at 65,536
rows — meaning the fused plan's real value is the STRUCTURAL guarantee of exactly one crossing
regardless of predicate count (already proven separately by `LazinessTest`), not a large measured
time saving at this scale. The honest verdict, matching the mission brief's own framing rather than
either extreme: **the crossing is worth paying for composed, multi-predicate work — not for reading
one predicate off one lane, where Java on the same memory is simply faster.**

**Method note, since two independent computations of the same data is itself worth recording as a
discipline:** `RESULTS.md` was hand-written from the raw `results/jmh-results.csv`, then verified
against `bench/summarise.sh` — a separate script the same PR ships that mechanically regenerates
the tables from the CSV "so a re-run's numbers can be regenerated mechanically — a table
transcribed by hand is a table that can drift from its own data" (the script's own doc comment).
Both productions of the same 50-row CSV agreed to 3 decimal places on every cell checked.

## 2026-08-17 — E-LGJ-VALHALLA-MEASURED-NOT-ASSUMED-1

**Status:** FINDING. **Confidence:** High (real numbers, both JDKs actually
Expand Down
6 changes: 3 additions & 3 deletions .claude/board/STATUS_BOARD.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ list.
| D-LGJ-D | Java FFM membrane `internal/ffm` | **DONE 2026-08-17** — compiles clean with `-Xlint:all`; 7 `[restricted]` warnings, all in `internal/ffm/*` or a test deliberately exercising it; `AbiContractTest` 7/7 incl. proving the manifest cross-check genuinely rejects a wrong `.so` (`libz.so.1` loads but is refused for exporting no `lgj_abi_manifest`) | E |
| D-LGJ-E | Java public facade (`NativePattern`/`View`/`Predicate`/`Pattern`/`Mask`) | **DONE 2026-08-17** — `AllTests` **132/132**: `ApiSurfaceTest` (reflection-enforced zero-FFM-leakage), `SmokeTest` 14/14, `FixtureParityTest` 30/30 (Java independently recomputes expected counts from the transcribed generator), `FusionParityTest` 31/31 (fused/unfused/scalar bit-identical across 6 row-count shapes), `LazinessTest` 8/8 (empirically: 0 crossings to build a 16-condition chain, exactly 1 to evaluate it, independent of rows up to 1,000,000 — the thesis's central claim, measured), `NarrowingTest` 16/16, `LifetimeTest` 23/23 | F, G |
| D-LGJ-F | Valhalla lab — three-truths method on the small semantic value vocabulary | **DONE 2026-08-17** — `valhalla-lab/`: 4 experiments + a self-verifying `run.sh` (mechanically diffs the two `Vocab.java`s modulo the `value` keyword before trusting the A/B) + 3 causal-isolation runs (escape-analysis off; `UseArrayFlattening`/`UseFieldFlattening` toggled independently). 3 real Valhalla limitations reproduced and filed under `reproducers/` (R1: `@NullRestricted` field on an identity class is a `VerifyError`, javac's fault — no source form expresses required strict-field order; **R2: array flattening has a hard 8-byte payload cliff, VM-confirmed via `-XX:+PrintFlatArrayLayout`** — `LaneId`/`Ordinal`/`MaskId` (≤8B) flatten, `RowRange`/`Row` (16B) do not, so "Valhalla helps descriptors not entities" is a measured VM cutoff, not a hand-wave, and `RowRange` landing on the wrong side is flagged as the one place the expectation was too optimistic; R3: the densest null-restricted array form is `jdk.internal`-only and generics erase flattening entirely — `Foo!` type syntax confirmed NOT to parse, matching the archaeology finding). 1 real defect found + fixed before landing (see `EPIPHANIES.md`). None of the three limitations changed the production API — the migration path stays exactly `record` → `value record` | I |
| D-LGJ-G | Java Vector API comparative bench vs Panama→`ndarray::simd` | **In flight** — real JMH + JOL jars fetched (`jmh-core`/`jmh-generator-annprocess`/`jopt-simple`/`commons-math3`/`jol-core`) to `bench/lib/` (gitignored); no bench source written yet; the ONLY remaining open row | I |
| D-LGJ-H | Falsification: handle lifecycle (adversarial), SIMD/scalar parity, Java/native parity | **DONE 2026-08-17 for the Rust+Java core** — see D-LGJ-C's disable-verification and D-LGJ-E's `FusionParityTest`/`LifetimeTest`. Re-opens for F/G once the Lab lands | I |
| D-LGJ-I | Docs: `architecture.md`, `panama.md`, `valhalla-lab.md`, `execution-boundary.md` | **Queued** — gated on F/G landing (the docs synthesize Lab results, not just the core) | — |
| D-LGJ-G | Java Vector API comparative bench vs Panama→`ndarray::simd` | **DONE 2026-08-17** — real JMH 1.37 (fork+warmup+blackholes confirmed in the log), 50/50 rows, 0 failures, `Data.crossCheck()` guards every fork. **Headline (Component C, single predicate, zero-copy `IntVector.fromMemorySegment`): the Java Vector API beats the native crossing at EVERY row count tested, 64 to 4,194,304** — 56.4x at small sizes down to 1.3-1.4x at the largest. Native beats a plain Java scalar loop only past ~4,096-16,384 rows. Component E: SIMD-vs-scalar is the biggest lever measured (10.8x-31.1x); fused vs unfused are within noise of each other at 65,536 rows (crossing-count guarantee matters more than measured time here, since Component A puts one downcall at ~22ns). Independently cross-checked: hand-written `RESULTS.md` numbers verified byte-for-byte against `summarise.sh`'s mechanically-generated tables from the same CSV | I |
| D-LGJ-H | Falsification: handle lifecycle (adversarial), SIMD/scalar parity, Java/native parity | **DONE 2026-08-17, all scopes closed** — Rust+Java core (D-LGJ-C disable-verification, D-LGJ-E `FusionParityTest`/`LifetimeTest`); Valhalla lab (`run.sh`'s vocab-honesty self-check + causal-isolation runs); bench (`Data.crossCheck()` on every fork, `summarise.sh` cross-check) | I |
| D-LGJ-I | Docs: `architecture.md`, `panama.md`, `valhalla-lab.md`, `execution-boundary.md` | **Unblocked** — F and G both landed; next action | — |
| D-LGJ-AUDIT | Mechanical post-fan-out audit: `grep` for `ndarray::hpc` imports, any `.h`/`cbindgen`/`jextract` artifact, any FFM type leaking into public Java API | **DONE 2026-08-17** — 1 real violation found (`kernels.rs::simd_popcount` used the internal `ndarray::hpc::bitwise` path), fixed in place; everything else confirmed to be the one sanctioned exception or explanatory prose | closed D-LGJ-C/D/E for the core |

### Reading this table
Expand Down
113 changes: 113 additions & 0 deletions bench/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# bench — where does execution belong?

A JMH harness that keeps the cost components **separate**, because conflating them is how a
benchmark ends up arguing for whichever side its author already preferred. Put enough work behind
a downcall and the crossing disappears; put none behind it and the crossing is everything.

> **This is real JMH** — `jmh-core 1.37`, forked JVMs, per-fork warm-up, compiler blackholes
> (the run log confirms `Compiler Blackholes ... are in use`). Not a hand-rolled loop. The
> hand-rolled harness in [`valhalla-lab`](../valhalla-lab) is labelled as such in its own README
> and its timings are secondary evidence there.

**The measured numbers and the verdict are in [`RESULTS.md`](RESULTS.md).**

## Run it

```sh
cd bench && ./run.sh # everything (~12 min on 4 vCPU)
cd bench && ./run.sh C_ # only the execution-boundary row sweep
```

Requires `liblgj_abi.so`. Build it first if missing:

```sh
cd native/lgj-abi && CARGO_TARGET_DIR=$(cd ../.. && pwd)/target cargo build --release
```

Output: `results/jmh-run.txt` (full log, including every warm-up iteration) and
`results/jmh-results.csv` (machine-readable).

## The components, and why each is isolated

| | class | question it answers alone |
|---|---|---|
| **A** | `A_DowncallOverhead` | what does crossing the membrane cost, with no work behind it? |
| **B** | `B_SegmentAccess` | how fast can Java read native memory at all? |
| **C** | `C_ExecutionBoundary` | native kernel vs Java Vector API vs Java scalar, swept over row count |
| **D** | *(in C)* | the Vector API arm — same segment, zero copy |
| **E** | `E_FusionAndPlanning` | fused (1 crossing) vs unfused (N crossings), swept over predicate count |
| **F** | *(in E)* | what does building the fluent chain cost, with no crossing at all? |

C and D live in one class on purpose: the question is a comparison, and separate classes would let
a difference in setup masquerade as a difference in execution. E and F likewise share a fixture.

## The rules this harness holds itself to

**1. Every arm computes the same answer, and it is checked before anything is timed.**
`Data.crossCheck()` runs in `@Setup` and throws if the native, vector, and scalar kernels disagree
on either the count or the sum. This is not politeness: a Vector kernel with a broken tail is
*faster* than a correct one, so an unchecked comparison rewards the bug.

**2. The Vector API arm is genuinely zero-copy.** `IntVector.fromMemorySegment(species, segment,
offset, ByteOrder.nativeOrder())` reads the native lane in place. No `byte[]`, no `int[]`, no
`MemorySegment.toArray`, no bounce buffer. A copy anywhere would make the comparison dishonest in
both directions at once — the Java side would pay a cost the Rust side does not, and the Rust side
would get credit for avoiding a copy the design never requires.

The one heap `int[]` in the harness (`Data.valuesHeap`) is used **only** by Component B, as the
"data already in Java" ceiling. Nothing that compares against the native kernel touches it.

**3. Nothing was added to the native library to be benchmarked.** Component A binds
`lgj_abi_manifest` and `lgj_mask_count` — both real ABI symbols. A symbol that exists only to be
measured is not the thing being measured.

**4. Component A binds its own method handles** rather than reusing
`internal.ffm.Downcalls`, so it measures the JDK's linker rather than this project's wrapper. The
wrapper's own overhead is then visible as the difference between A and C.

**5. The laziness claim is asserted, not assumed.** `planConstructionOnly` reads
`Diagnostics.crossings()` before and after building the chain and throws if it moved.

**6. Warm JVM, always.** JMH forks, warms each fork, and discards warm-up. Cold-start numbers
appear nowhere. `shouldDoGC(true)` runs a GC between iterations so a collection triggered by one
arm is not attributed to the next.

## Settings

`@Fork(1)`, `@Warmup(5 × 500 ms)`, `@Measurement(8 × 500 ms)`, `Mode.AverageTime`. Reported as
mean ± 99.9 % confidence interval, which is JMH's default and what the CSV contains.

**One fork, and that is a limitation worth naming.** A single fork cannot see run-to-run variance
from JIT compilation-order nondeterminism or address-space layout. Two or more forks would be
better; on a 4-vCPU container the full sweep already takes ~12 minutes and doubling it was judged
not worth the wall-clock. Treat differences under roughly 10 % between arms as not established by
this harness.

**Shared container, not a tuned benchmark host.** No CPU pinning, no isolated cores, no disabled
turbo, no disabled hyperthreading. The confidence intervals reflect that. Large effects (order of
magnitude) are safe to read; small ones are not.

## Fetching JMH

The jars are not vendored. Fetch them into `lib/`:

```sh
mkdir -p bench/lib && cd bench/lib
B=https://repo1.maven.org/maven2
curl -sSLO --noproxy '*' $B/org/openjdk/jmh/jmh-core/1.37/jmh-core-1.37.jar
curl -sSLO --noproxy '*' $B/org/openjdk/jmh/jmh-generator-annprocess/1.37/jmh-generator-annprocess-1.37.jar
curl -sSLO --noproxy '*' $B/net/sf/jopt-simple/jopt-simple/5.0.4/jopt-simple-5.0.4.jar
curl -sSLO --noproxy '*' $B/org/apache/commons/commons-math3/3.6.1/commons-math3-3.6.1.jar
```

### One trap, recorded so nobody loses an hour to it

On JDK 23+ annotation processing is **off by default**. Without `-proc:full`, JMH's generator
never runs, no `META-INF/BenchmarkList` is produced, and the harness dies at startup with:

```
ERROR: Unable to find the resource: /META-INF/BenchmarkList
```

which reads like a classpath problem and is not one. `run.sh` passes `-proc:full` with a comment
saying why. JMH also rejects benchmark classes in the default package, with a clear message.
Loading