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]>
What
The parse-error oracle built its
LineIndexof 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);stray-carriage-returnanddivide-by-zeroclaims (overlays.rs).Now:
Referencecarrieslines: OnceCell<LineIndex>, read throughReference::line_index(src)by the reconciler and every overlay, built on first use.Reference::shift(which moves offsets past a byte-0 BOM, after the transforms ran in the BOM-less frame) clears the cell, as doesBomOrphanLine's shifted copy; a debug assertion checks the cached index is of the text asked for (LineIndexrecords its text's length).LineIndex::newusessrc.match_indices('\n')(memchr), ascertify.rs's does.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 oforigin/main1f4b9dc and this branch (same build flags for both), thencompare:format/errorscheck/errorscheck-json/errorsSmall: 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 --jsonlandsemantic_snapshoton 45,182 inputs (pinnedfuzz_parse+fuzz_green_eq_redcorpus, compat corpus, tracked ledgers, profiling workloads): byte-identical toorigin/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-returnoverlay (one of the users here), and #3158 / #3168 edit thedecimal-precisionoverlay and thereconcilecall: trivial conflicts for whichever lands later.🤖 Generated with Claude Code