Skip to content

perf(parser): send tokens no lexer rewrite applies to straight to emission - #3105

Merged
robcohen merged 1 commit into
mainfrom
perf/lexer-plain-token
Nov 29, 2026
Merged

robcohen merged 1 commit into
mainfrom
perf/lexer-plain-token

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

TokenIter::next (the lexer wrapper both parse paths use) runs every token through the chain of rewrites that reproduce beancount's lexing rules: mid-line #! / #+ / % splits, stray-\r trivia, the three lone-capital rules, comment-to-line-end, the AST path's blank dropping, then the column-0 arms. Each step tests the token before finding it has nothing to do, and on a clean ledger that is every step for nearly every token.

  • A token none of the steps can touch (account, number, string, currency of two or more letters, date, keyword, punctuation...) now goes straight to emission: emit_token, which is the old Ok(token) arm moved into a method, unchanged.
  • Blanks with no blank or \r right after them skip the stray-\r step, which for them only records an empty run (the same stray_cr_failed_until update is made).
  • split_comment_start decides on the first byte (# / %) before the column test and the slice.

Not a second classifier: rewrite_free only lists the token kinds each existing step asks for, and every other token still goes through the full chain. Part of the clean-path work against v0.24.0 (hot spot 2 of the perf review).

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.6M 517.3M -1.2%
investment 757.9M 748.5M -1.2%
tagged 852.9M 842.0M -1.3%
multicurrency 726.9M 718.8M -1.1%
fifo 722.0M 714.7M -1.0%
metadata-heavy 953.9M 941.4M -1.3%

emit_token is #[inline(always)] (with allow(clippy::inline_always)): a plain #[inline] stayed out of line and kept only a third of the gain.

The perf review's estimate for the lexer (+40M) counted what moved from lossless_kind_tokens into TokenIter::next. Against a v0.24.0 build with the same profile, the wrapper's own cost grew by about 22M on simple; this takes back about 6M of that. The rest is per-call overhead spread across the wrapper, not any one rewrite.

No output change

  • New drift guard rewrite_free_tokens_lex_as_before: with a test-only switch (test_counters::SLOW_PATH_ONLY) the fast paths are skipped, and the token streams of tokenize and tokenize_lossless must be equal both ways on every tracked ledger, every fuzz regression, and every three-character string over 22 fragments (' ', \t, \r, \r\n, A, #, %, !, +, :, ;, *, &, ", {, -, BOM, USD, ...) after five line prefixes. Mutation-checked: dropping Flag from rewrite_free fails it at once.
  • Byte-identical outputs against main on 45,182 inputs (pinned fuzz corpus, compat corpus, tracked ledgers): parse_error_lines --jsonl and semantic_snapshot.
  • scripts/compat-beancount-oracle-drift.py: 123,007 compared, tokens and errors all agree. compat-parse-error-parity.py --check and compat-semantic-parity.py --check: no regressions.
  • cargo test -p rustledger-parser -p rustledger-loader (including bc_lexer_drift and the fuzz_green_eq_red corpus test); clippy -D warnings.
  • No cache-version bump: tokens and output are unchanged.

🤖 Generated with Claude Code

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

github-actions Bot commented Nov 29, 2026 •

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 31.6ms ± 1.1ms 384.3ms ± 2.8ms 12.2x 37.4 MB / 51.1 MB
investment 51.9ms ± 6.8ms 1499.8ms ± 12.0ms 28.9x 45.4 MB / 59.1 MB
fifo 48.0ms ± 0.7ms 7925.3ms ± 156.5ms 165.0x 55.4 MB / 58.8 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 33.9ms ± 1.4ms 31.6ms ± 1.1ms -6.8% 🟢 faster 36.8 MB → 37.4 MB (+1.5%)
investment 49.6ms ± 1.2ms 51.9ms ± 6.8ms +4.7% ✅ within noise 45.2 MB → 45.4 MB (+0.3%)
fifo 47.7ms ± 1.0ms 48.0ms ± 0.7ms +0.7% ✅ within noise 55.2 MB → 55.4 MB (+0.2%)

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

@codecov

codecov Bot commented Nov 29, 2026

Copy link
Copy Markdown

Codecov Report

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

Files with missing lines Patch % Lines
crates/rustledger-parser/src/logos_lexer.rs 99.11% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

…ssion

`TokenIter::next` runs every token through the chain of rewrites that
reproduce beancount's lexing rules (mid-line comment markers, stray `\r`
trivia, lone capitals glued to a run), each step testing the token
before finding nothing to do. A token none of the steps can touch now
goes straight to emission (`emit_token`, the old `Ok(token)` arm), and
so do blanks with no blank or `\r` after them, for which the stray-`\r`
step only records an empty run. `split_comment_start` also decides on
the first byte before slicing.

`rewrite_free` names the kinds each step asks for; the new
`rewrite_free_tokens_lex_as_before` test replays the full chain against
the fast path (both lexing paths) on every tracked ledger, every fuzz
regression and every three-character string over the bytes the rules
key on, and fails when a kind is dropped from the list.

`rledger check` on 10k-transaction ledgers: -1.0% to -1.3%
instructions. Output is byte-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the perf/lexer-plain-token branch from 9dd4576 to 8134929 Compare November 29, 2026 04:16
@robcohen
robcohen merged commit 05a8c5f into main Nov 29, 2026
66 of 67 checks passed
@robcohen
robcohen deleted the perf/lexer-plain-token branch November 29, 2026 06:35
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