Repository navigation
perf(parser): record the lines a defect check reads while the tree is built (ADR 0010) - #3113
Merged
Merged
Conversation
Contributor
📊 Benchmark ResultsInstruction counts vs base (cachegrind, deterministic)
Instruction counts (cachegrind Wall clock (hyperfine)
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
… built The tree builder hands each line, in the role it gives it (header, posting, metadata, other body line), to a BuildObserver. The converter builds with a recorder that runs the canonical line rules on the builder's own token slice once and keeps only which top-level items hold a line some check could report on. The checks then read the tree for those items alone; a clean file flags nothing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…hing it reports The recorder now also notes what the descendants walk could report on (an invalid token, a cost spec with a shape defect or no closing brace, a link as a metadata value, a custom or pushmeta directive) and collects the column-0 comments by the walk's own rule as the tokens go by. Without occurrences to collect, a file with none of those needs no walk. The line extraction is now slice-based, so the recorder copies no tokens for a clean line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Inline the recorder's per-token hook; a plain metadata line skips the walk's
link scan; the plain-posting shortcut takes a sale's empty cost {}; and
unevaluable_meta_value answers None before allocating when the value holds
no operator or parenthesis, as cost_region_read would.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s read Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ndants walk Since #3101 the descendants walk reports every BOM token in the tree (the leading BOM is stripped before the build, so one there is a second BOM), but the recorder only asked for the walk on an invalid token: the lean parse skipped it on such a file and lost the BomInDirectiveBody error. The recorder now flags a BOM token as it does an invalid one. The node-shape fixtures gain a second BOM at the start of the file and on a later line, and stray \r blanks after a line first token (#3099). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen
force-pushed
the
perf/defects-at-build
branch
from
November 29, 2026 13:54
8d619e2 to
14679a4
Compare
robcohen
marked this pull request as ready for review
November 29, 2026 13:54
Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen
added a commit
that referenced
this pull request
Nov 29, 2026
…check recorder (#3129) The recorder added by #3113 kept each transaction's and posting's metadata keys in a plain list and searched it for every new key, so one entry with n keys cost n²/2 string comparisons. metadata_many_keys (50,000 keys) went from 0.11 s to 1.54 s in release and from 0.73 s to 6.7 s in debug, and fails its 15 s bound on CI runners (21 s), so main's Test job is red. KeySet keeps the list for the first 16 keys, which covers every real ledger, then moves into a hash set. The test is back to 0.12 s / 0.82 s. Co-authored-by: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Since #2802 and the parser-correctness PRs after it, the converter holds every line to beancount's grammar: transaction and directive headers, posting lines and the posting builder's cost and price rules, metadata lines, other transaction body lines, repeated metadata keys, invalid dates. Each check walked the finished tree again and re-collected each line's tokens (flattening the
AMOUNT/COST_SPEC/PRICE_ANNOTATIONnodes of every posting), on every line of every file, and the descendants walk was one more whole-tree pass. On a clean ledger they all found nothing, and together they were the largest part of the clean-path regression against v0.24.0.Design (ADR 0010)
The tree builder is where a line's role is decided: it is the code that opens a
POSTINGor aMETA_ENTRY. A check that re-derived the line from the tree read that decision a second time. Now:parser::BuildObserver): each token as it leaves the lexer; per top-level item its header line, each posting line, each metadata line (and whether it hangs on a posting), each other transaction body line, eachCOST_SPEC. The lines are the builder's own token slices, so nothing is re-collected.()observes nothing and is whatparse_structuredbuilds with: the formatter, the LSP's tree users and every other caller pay nothing.cst::line_checks::Recorder) that runs the same canonical rules on those slices, once, and keeps only a verdict per top-level item: which check could report there (HEADER,POSTING,META,BODY,DUP_META), whether anyDATEis not a calendar date, and whether anything needs the descendants walk (an invalid token or a second BOM, a cost spec with a shape defect or no closing brace, a link as a metadata value, acustom/pushmetadirective). It also collects the column-0 comments by the walk's own rule.parse_without_occurrences) also skips the descendants walk when nothing needs it.A verdict is a superset ("a rule found something on this line"), so skipping an unflagged item hides no error as long as the recorder reads the same tokens the check reads. Pinned three ways:
line_checks::txn_header_tokens,header_line_tokens), shared by the checks and the recorder; the token-collection loops ingreen::tl_transaction_header_check,convert::header_line_tokensandMetaCheck::linenow call them.parse_red_onlyrecords nothing and runs every check and the walk, sogreen_red_propertiesandfuzz_green_eq_redcompare the two paths; newlean_equals_red_on_generated_ledgers(and the node-shape fixtures) do the same forparse_without_occurrences. Mutation-checked: skipping the walk unconditionally fails the fixture test in a release build.Beancount-specific by design: the recorder calls the beancount frontend's rules, the observer is the beancount builder's. No format-neutral layer learns a beancount rule; another grammar's frontend brings its own builder and observer, or none.
Smaller pieces in the same commits, each output-neutral:
name_misspelled_booleans/name_mid_line_hash_commentsonly rename errors already found: skipped when there are none (the second scanned the whole source for#!/#+).unevaluable_meta_valuereturnsNonebefore allocating when the value has no operator or parenthesis, which is wherecost_region_readreturnsNoneanyway.{}(full_posting_checkreplayed by its debug assertion; the shapes test moves{}to the plain list).txn_header::first_header_defect_intakes a slice instead of an ownedVec.Numbers
rledger check,instructions:u,profilingprofile (LTO),BEANCOUNT_DISABLE_LOAD_CACHE=1, 10k transactions, re-measured after the rebase (run-to-run spread about 0.1%):v0.24.0 is built from its tag with today's toolchain, so the comparison is not a toolchain artifact.
By step (measured before the rebase, profiling profile without LTO, against main with #3105 and #3106): recording the line verdicts in the builder −39M / −57M / −62M on simple / metadata-heavy / tagged; slice-based extraction (no copies for a clean line) −4M to −6M; skipping the descendants walk −13M / −32M / −26M; the recorder hook inlined and cheaper metadata and sale-posting verdicts −5M to −16M.
Where the rest goes
(Measured before the rebase; #3099 and #3101 do not touch these paths on a clean ledger.)
Parse alone (
parse_without_occurrenceson the file, same toolchain, LTO), v0.24.0 → this PR: simple 258.6M → 257.9M (−0.3%), investment 346.4M → 354.2M (+2.3%), tagged 470.6M → 485.8M (+3.2%), multicurrency 382.7M → 387.8M (+1.3%), fifo 300.6M → 308.9M (+2.8%), metadata-heavy 535.1M → 551.3M (+3.0%). The parser, with every correctness check added since v0.24.0, is within ~3% of it. Of that, the lexer is more than all of it: lexing alone (lossless_kind_tokens) went 28.0M → 51.9M on simple and 54.9M → 105.4M on metadata-heavy, about 60% in the logos DFA (the token regexes changed for correctness, e.g. the Unicode classes of account names) and 40% in theTokenIterrewrite chain #3105 started on; the defect-check and walk savings above more than pay for it. That is the next lever, and it is a lexer change, not a checks change.The rest of the
checkgap to v0.24.0 is outside the parser: booking and inventory work for fifo / investment (fifo +24M after parse), post-parse work on simple (+9.4M, the exact-decimal validation among it), and process start-up relocations of a larger binary (_dl_relocate_object_no_relro, about +6M). So the ≤5% target holds on simple and investment, is missed by a few tenths of a percent on tagged and metadata-heavy (where the lexer is the remaining parser cost), and is not reachable on fifo from the parser side.Rebase
SourceFile::parse_stripped_with_checks→parser::build_structured_stripped(KindTokenIter::new_stripped), so the recorder sees exactly the token streamparse_strippedbuilds from.fix(parser): the line-check recorder flags a second BOM for the descendants walk): fix(parser): report a second BOM at the start of a file, as on any later line #3101 made the descendants walk report everyBOMtoken in the tree (a second BOM, at the start of the file or of any later line) asBomInDirectiveBody, but the recorder only asked for the walk on anERROR_TOKEN. On the rebased commits before the fix,parseandparse_without_occurrencesdisagreed on 25 of the 45,182 inputs (allfuzz_green_eq_redcorpus entries), so the loader would have lost that error. The recorder now flags aBOMtoken as it does an invalid one; the node-shape fixtures ofgreen_red_propertiesgain a second BOM at the start of the file and on a later line (the debug replay fires on them without the fix), and stray-lines (fix(parser): read a stray \r after a line's first token as a blank #3099).after a line's first token lexes as a blank): it changes the tokens the builder emits, and the recorder reads those same tokens and applies the walk's column-0 comment rule by the same token kinds, so nothing to change; pinned by the new fixtures and the corpus runs below.oracle::beancount::built_state_directives): it runs after the gated checks, unconditionally, and none of its inputs come from a skipped check (each of which provably reports nothing), so nothing to change.No output change
Against origin/main (2ecf161), on 45,182 inputs (the pinned
fuzz_parse+fuzz_green_eq_redcorpus, the compat corpus, every tracked ledger, the profiling workloads), at 4d63d00, the last code change (the commits after it touch only the CHANGELOG, the ADR and a doc comment):parse_error_lines --jsonlandsemantic_snapshot: byte-identical (tempdir names normalized).debug-assertions = true(parse_error_linesandsemantic_snapshot): no assertion fires, so every skipped check and walk was replayed and found nothing.parsevsparse_without_occurrences(directives, options, includes, plugins, comments, errors, warnings) in release: 0 mismatches (25 before the BOM fix).scripts/compat-beancount-oracle-drift.py: 123,007 compared, tokens and errors all agree.compat-parse-error-parity.py --checkandcompat-semantic-parity.py --check: no regressions.cargo test -p rustledger-parser -p rustledger-loader -p rustledger -p rustledger-lsp -p rustledger-surface-parity-tests(debug, so every check is replayed): 3,497 passed, 0 failed.cargo clippy --all-features --all-targets -- -D warnings: clean.🤖 Generated with Claude Code