Skip to content

perf(parser): build the error oracle's line index once per parse - #3170

Merged
robcohen merged 1 commit into
mainfrom
perf/oracle-line-index
Nov 30, 2026
Merged

robcohen merged 1 commit into
mainfrom
perf/oracle-line-index

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

The parse-error oracle built its LineIndex of the source two to three times per parse with errors, each a byte-by-byte scan:

  • reconcile_errors_with (oracle/beancount/mod.rs);
  • BomInAccount::transform_reference (on a file holding a BOM);
  • lazily in the stray-carriage-return and divide-by-zero claims (overlays.rs).

Now:

  • Reference carries lines: OnceCell<LineIndex>, read through Reference::line_index(src) by the reconciler and every overlay, built on first use.
  • Frames are respected: Reference::shift (which moves offsets past a byte-0 BOM, after the transforms ran in the BOM-less frame) clears the cell, as does BomOrphanLine's shifted copy; a debug assertion checks the cached index is of the text asked for (LineIndex records its text's length).
  • LineIndex::new uses src.match_indices('\n') (memchr), as certify.rs's does.
  • Not unified with certify's LineIndex: that one stores 0-based line starts and answers 0-based lines; this one stores newline offsets and answers the 1-based lines the reconciler's slots carry. Merging them would mean converting at every call site for no measured gain.

Numbers

scripts/instr-bench.py run (cachegrind) on release builds of origin/main 1f4b9dc and this branch (same build flags for both), then compare:

workload main this PR change
format/errors 107,212,149 107,097,830 −0.11%
check/errors 297,323,495 297,200,622 −0.04%
check-json/errors 172,050,881 171,929,090 −0.07%

Small: on these workloads the index was already a minor cost next to the oracle's parse. It removes repeated work rather than a hotspot.

No output change

  • parse_error_lines --jsonl and semantic_snapshot on 45,182 inputs (pinned fuzz_parse + fuzz_green_eq_red corpus, compat corpus, tracked ledgers, profiling workloads): byte-identical to origin/main. So the parity harnesses cannot move, and no cache bump.
  • cargo test -p rustledger-parser -p rustledger-loader -p rustledger-lsp --all-features (debug, the frame assertion on): 2,220 passed, 0 failed. Clippy -D warnings: clean.

Merge note

#3162 removes the stray-carriage-return overlay (one of the users here), and #3158 / #3168 edit the decimal-precision overlay and the reconcile call: trivial conflicts for whichever lands later.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-parser/src/cst/oracle/mod.rs 91.66% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Instruction counts and wasm fuel vs base (deterministic)

Workload base this PR Change vs v0.24.0
check/simple 289.7M 289.8M +0.03% ✅ -17.1%
check/investment 450.1M 450.0M -0.01% ✅ -12.8%
check/tagged 420.2M 420.2M -0.00% ✅ -18.5%
check/multicurrency 439.3M 439.2M -0.01% ✅ -24.8%
check/fifo 439.4M 439.4M +0.01% ✅ -16.8%
check/metadata 614.2M 614.1M -0.02% ✅ -23.1%
check-warm/simple 128.5M 128.5M -0.00% ✅ -18.4%
format/simple 613.8M 613.7M -0.02% ✅ +13.9%
format/metadata 1.52G 1.52G +0.02% ✅ +27.5%
format/errors 92.3M 91.8M -0.55% ✅ +102.6%
check/errors 269.4M 268.8M -0.21% ✅ —
check-json/errors 149.5M 148.9M -0.39% ✅ —
check/junkline 30.6M 30.5M -0.27% ✅ +299.3%
check-json/junkline 43.9M 43.8M -0.31% ✅ +613.4%
query/journal/simple 442.5M 442.4M -0.02% ✅ -29.0%
query/groupby/simple 338.9M 339.1M +0.07% ✅ -26.3%
query/groupby/investment 556.0M 556.6M +0.11% ✅ -25.3%
lsp/open-diagnostics/simple 336.9M 336.9M -0.00% ✅ —
lsp/open-diagnostics/metadata 901.0M 901.0M +0.01% ✅ —
lsp/format/simple 1.03G 1.03G +0.02% ✅ —
lsp/format/metadata 2.53G 2.53G +0.00% ✅ —
lsp/edit/simple 670.9M 670.9M -0.00% ✅ —
Workload (wasmtime fuel) base this PR Change vs v0.24.0
wasm/validate/simple 1.14G 1.14G +0.00% ✅ —
wasm/validate/metadata 2.79G 2.79G +0.00% ✅ —
wasm/format/simple 1.40G 1.40G +0.00% ✅ —
wasm/format/metadata 3.55G 3.55G +0.00% ✅ —
wasm/query/simple 1.28G 1.28G +0.00% ✅ —
ffi/load/simple 1.75G 1.75G +0.00% ✅ —
ffi/load/metadata 4.04G 4.04G +0.00% ✅ —
ffi/query/simple 1.38G 1.38G +0.00% ✅ —
ffi/validate/simple 1.22G 1.22G +0.00% ✅ —
ffi/format/simple 1.47G 1.47G +0.00% ✅ —

Instruction counts (cachegrind I refs) are deterministic: the same binary on the same input gives the same count to within ~0.02%, so unlike wall clock a 1% change is real. 🟡 ≥ 1%, 🔴 ≥ 5%. The wasm/ and ffi/ rows are wasmtime fuel (WebAssembly operators the guest executed), which is exact: the same .wasm on the same input gives the same fuel on any machine. Static data moves the component's heap, so a change to embedded strings alone can move ffi/ by a few hundredths of a percent. The last column is cumulative drift from the pinned v0.24.0 baseline (scripts/perf-baseline.json).

Wall clock (hyperfine)

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 38.1ms ± 1.4ms 787.8ms ± 6.3ms 20.7x 34.7 MB / 51.1 MB
investment 60.1ms ± 1.1ms 2907.0ms ± 9.4ms 48.4x 44.9 MB / 59.0 MB
fifo 59.5ms ± 1.0ms 14626.5ms ± 29.1ms 245.8x 42.8 MB / 59.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 37.1ms ± 0.4ms 38.1ms ± 1.4ms +2.5% ✅ within noise 34.7 MB → 34.7 MB (-0.0%)
investment 59.5ms ± 0.5ms 60.1ms ± 1.1ms +0.9% ✅ within noise 44.8 MB → 44.9 MB (+0.3%)
fifo 59.0ms ± 0.5ms 59.5ms ± 1.0ms +0.9% ✅ within noise 42.8 MB → 42.8 MB (+0.0%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.6ms, investment 0.5ms, fifo 0.5ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 180 masked)
Full AST Match 100%
Total Files 828
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
    The headline is the EFFECTIVE rate, after pairs where beanquery
    itself is wrong are masked. raw is the same run with nothing
    masked. A widening gap means the registry grew, not that rledger
    changed.
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

A parse with errors built the oracle's `LineIndex` of the source two or
three times: in `reconcile_errors_with`, in `BomInAccount`'s reference
transform (on a file holding a BOM), and lazily in the
`stray-carriage-return` and `divide-by-zero` claims. Each scanned the
text byte by byte.

`Reference` now carries a `OnceCell<LineIndex>` (`Reference::line_index`)
that every one of them reads, built on first use. `Reference::shift`, which
moves the offsets to another frame (past a byte-0 BOM), clears it, and so
does `BomOrphanLine`'s moved copy; a debug assertion checks the index is of
the text it is asked for. `LineIndex::new` finds the line ends with
`match_indices`, as certify.rs's does. Certify's index stays separate: it
counts 0-based line starts, this one 1-based lines from newline offsets.

Error output is unchanged (corpus byte-identical). Instruction counts
(`scripts/instr-bench.py run` + `compare`): format/errors -0.11%,
check/errors -0.04%, check-json/errors -0.07%.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the perf/oracle-line-index branch from 44bb8b7 to dac39e0 Compare November 30, 2026 20:33
@robcohen
robcohen merged commit 6585988 into main Nov 30, 2026
65 of 67 checks passed
@robcohen
robcohen deleted the perf/oracle-line-index branch November 30, 2026 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant