Skip to content

perf(parser): skip the defect checks a clean posting or metadata line cannot fail - #3106

Merged
robcohen merged 1 commit into
mainfrom
perf/cst-defect-fastpath
Nov 29, 2026
Merged

robcohen merged 1 commit into
mainfrom
perf/cst-defect-fastpath

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

Since #2802 (and #2893 for repeated metadata), every posting line and every metadata line of every transaction is flattened and checked against beancount's grammar, on clean files too. That is the largest piece of the clean-path regression in parser::cst::convert (hot spot 1 of the perf review). This takes the clean lines off it without changing what is reported:

  • Plain postings: first_posting_defect returns at once for the shapes nearly every posting has: an account, optionally a signed number currency, a {...} / {{...}} cost whose components are amounts, dates or labels, and an @ / @@ price, with every currency two or more letters (is_plain_posting). The rules give no defect on those shapes (no optflag, incomplete_amount, cost_comp list, price, end of line, no glued one-letter currency, no {#). Every other shape takes the full rule, and posting_builder_error still runs on every cost or price.
  • Plain metadata lines: MetaCheck::line returns at once for key: value with one string / account / date / tag / boolean / NULL / 2+-letter currency (is_plain_meta_line). Those have no grammar defect and no expression value, so the check had nothing to report.
  • Repeated metadata (report_duplicate_metadata): holds_an_invalid_token (a walk of the whole transaction) runs only once a repeat is found; an owner with too few META_ENTRY children to repeat a key returns at once; the keys seen live in a fixed [&str; 16] instead of a Vec per owner (the set fallback past 16 keys is unchanged).
  • node_content_span (every directive, in the span fixup) finds the first non-trivia token by walking green children instead of descendants_with_tokens() on the red tree.

Not a second grammar: the shortcuts only name shapes, and every shortcut line is checked against the full rule by a debug_assert! in the same function, so every debug test run replays it. node_content_span likewise debug-asserts the red walk's answer.

Numbers

rledger check, instructions:u, profiling profile with LTO off / 16 CGUs, BEANCOUNT_DISABLE_LOAD_CACHE=1, 10k transactions:

workload before after delta
simple 523.8M 501.8M -4.2%
investment 758.2M 729.4M -3.8%
tagged 853.9M 813.5M -4.7%
multicurrency 727.7M 702.5M -3.5%
fifo 722.2M 698.2M -3.3%
metadata-heavy 954.3M 889.5M -6.8%

Per item on simple / metadata-heavy: duplicate-metadata reorder plus early return ~-15M / -15M, plain postings ~-6M / -6M (more on cost/price ledgers), plain metadata lines ~0 / -35M, fixed key buffer ~0 / -13M, node_content_span ~-3M.

No output change

  • Byte-identical outputs against main on 45,182 inputs (the pinned fuzz_parse + fuzz_green_eq_red corpus, the compat corpus, every tracked ledger, the profiling workloads): parse_error_lines --jsonl and semantic_snapshot (tempdir names normalized).
  • The same 45,182 inputs through parse_error_lines and semantic_snapshot built with debug-assertions = true: no assertion fires (each shortcut line is replayed against the full rule, and the green first-token walk against the red one).
  • scripts/compat-beancount-oracle-drift.py: 123,007 compared, all agree. compat-parse-error-parity.py --check and compat-semantic-parity.py --check: no regressions.
  • cargo test -p rustledger-parser -p rustledger-loader (debug, so every shortcut is replayed), plus test_plain_line_shortcuts_take_the_common_shapes, which pins the shapes taken and refused. Clippy -D warnings.
  • No cache-version bump: nothing cached changes.

Touches cst/convert.rs in four small, local places (MetaCheck::line, node_content_span, report_duplicate_metadata); the rest is in cst/directive_header.rs.

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner November 29, 2026 01:48
@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 179 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

@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 50.1ms ± 3.5ms 789.6ms ± 4.1ms 15.7x 43.1 MB / 51.1 MB
investment 75.9ms ± 0.7ms 2875.3ms ± 5.5ms 37.9x 54.4 MB / 59.1 MB
fifo 74.6ms ± 0.4ms 14629.1ms ± 159.6ms 196.2x 58.9 MB / 59.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 50.7ms ± 0.4ms 50.1ms ± 3.5ms -1.0% ✅ within noise 36.9 MB → 43.1 MB (+16.7%)
investment 77.8ms ± 0.6ms 75.9ms ± 0.7ms -2.4% ✅ within noise 45.4 MB → 54.4 MB (+19.9%)
fifo 77.0ms ± 0.4ms 74.6ms ± 0.4ms -3.2% ✅ within noise 55.4 MB → 58.9 MB (+6.4%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 1.6ms, investment 0.4ms, fifo 0.3ms), 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

@codecov

codecov Bot commented Nov 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.15498% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-parser/src/cst/convert.rs 91.22% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

… cannot fail

Since #2802 every posting and metadata line is flattened and checked
against beancount's grammar, and every metadata value against the
expression check, on clean files too.

- `first_posting_defect` returns at once for the shapes nearly every
  posting has (an account; a signed amount; a `{...}` cost of amounts,
  dates and labels; an `@` / `@@` price; currencies of two or more
  letters), and `MetaCheck::line` for `key: value` with one plain
  value. Any other shape takes the full rule; in debug builds the full
  rule is replayed on every shortcut line and must agree.
- `report_duplicate_metadata` tests `holds_an_invalid_token` (a walk
  of the whole transaction) only once it has found a repeat, returns
  at once on an owner with too few keys to repeat one, and keeps the
  keys in a fixed buffer instead of a vector per owner.
- `node_content_span` finds the first content token on the green tree
  instead of building red nodes (debug-asserted equal to the red walk).

`rledger check` on 10k-transaction ledgers: -3.3% to -4.7%
instructions, -6.8% on a metadata-heavy one. Errors and directives are
byte-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the perf/cst-defect-fastpath branch from b87760c to 4925b11 Compare November 29, 2026 04:16
@robcohen
robcohen merged commit 6c7b84a into main Nov 29, 2026
66 of 67 checks passed
@robcohen
robcohen deleted the perf/cst-defect-fastpath branch November 29, 2026 06:47
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