Skip to content

perf(parser): skip LSP-only occurrence collection on the load path (-23MB heap, -2.6% instructions) - #1590

Merged
robcohen merged 3 commits into
mainfrom
perf/walk-descendants-presize
Jun 25, 2026
Merged

robcohen merged 3 commits into
mainfrom
perf/walk-descendants-presize

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

Profiling-driven optimization ④ — the biggest yet. Skips collecting the currency_occurrences / account_occurrences indices on the loader / CLI processing path, where they're never read.

walk_descendants_once was the #1 allocation-count site in the profile. For every ACCOUNT / CURRENCY token it builds a Spanned<Account> / Spanned<Currency> (an Account::new / Currency::new allocation) and does an O(depth) in-ERROR_NODE ancestor walk. But these two indices are consumed solely by the LSP (rename / references / highlight) — confirmed by grep: zero consumers outside rustledger-parser + rustledger-lsp. The loader / rledger check|query pipeline builds them and throws them away.

Change

  • New parse_without_occurrences(source) threads collect_occurrences = false into walk_descendants_once, which then skips the ACCOUNT/CURRENCY construction and their ancestor walks. Inline errors + top-level comments are still collected (the pipeline needs them).
  • parse() is unchanged — the LSP and all 293 other call sites keep full behavior.
  • The loader's 2 parse sites switch to the new variant.

Measured impact (10k-txn workload, dhat + cachegrind)

Metric Before After Δ
Heap 158.7 MB 135.1 MB −23.6 MB (−14.9%)
Allocations 1,223,484 1,183,412 −40,072
Instructions 1,121,094,109 1,092,010,959 −29.1M (−2.6%)

Largest heap and CPU win of the profiling series.

Safety

  • parse() semantics unchanged → LSP unaffected; the returned ParseResult differs from parse()'s only in the two now-empty occurrence vectors.
  • Regression test (parse_without_occurrences_skips_indices_but_matches_directives) pins: occurrences empty, but directives / errors / options identical to parse().
  • cargo test -p rustledger-parser (280) + -p rustledger-loader (89) pass; parser+loader+lsp cargo check + clippy clean.
  • Parser change → the BQL compat suite runs as the CI gate on this PR.

Found via

The nightly profiling data branch — fourth optimization it surfaced.

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner June 25, 2026 04:36
Copilot AI review requested due to automatic review settings June 25, 2026 04:36
@codecov

codecov Bot commented Jun 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 35.3ms ± 1ms 23.6 MB
beancount 905ms ± 5.5ms 50.6 MB
Comparison 25.6x faster 2.1x less

vs main branch (2026-06-25)

Metric main this PR Change
Time 37.4ms 35.3ms -5.6% 🟢
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves loader/CLI performance by avoiding construction of LSP-only currency_occurrences / account_occurrences indices during parsing on the load path, while keeping the existing parse() behavior unchanged for editor/LSP usage.

Changes:

  • Add parse_without_occurrences() (and underlying parse_via_cst_opts(..., collect_occurrences)) to allow parsing without collecting occurrence indices.
  • Thread collect_occurrences into the CST descendant walk to skip per-token allocations and ancestor probes when occurrences aren’t needed.
  • Update the loader to use parse_without_occurrences() and add a regression test for semantic equivalence (minus occurrences).

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
crates/rustledger-parser/tests/parser_integration_test.rs Adds a regression test covering the new lean parse path.
crates/rustledger-parser/src/lib.rs Exposes parse_without_occurrences() at the public API level.
crates/rustledger-parser/src/cst/mod.rs Re-exports the new CST parse entry point with options.
crates/rustledger-parser/src/cst/convert.rs Implements parse_via_cst_opts and gates occurrence collection in walk_descendants_once.
crates/rustledger-loader/src/lib.rs Switches loader parsing to the lean variant to realize the allocation/CPU savings.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 792
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

@robcohen
robcohen force-pushed the perf/walk-descendants-presize branch 2 times, most recently from 0e859f5 to 0136c04 Compare June 25, 2026 06:02
@robcohen

Copy link
Copy Markdown
Member Author

Both addressed: (1) the assertions now compare full directives/errors/options contents via Debug, not just lengths — a content regression that preserves counts now fails. (2) On naming: this file is ~50/50 between test_* and bare descriptive names (e.g. parse_ok, string_escapes_are_decoded), so I kept the descriptive form to match the latter group.

robcohen added 2 commits June 25, 2026 10:39
…23MB, -2.6%)

currency_occurrences/account_occurrences are consumed only by the LSP, but the
loader/CLI pipeline collected them anyway -- walk_descendants_once built a
Spanned<Account>/Spanned<Currency> (+ an in-ERROR_NODE ancestor walk) per
ACCOUNT/CURRENCY token, the #1 allocation-count site. Add
parse_without_occurrences (collect_occurrences=false) and point the loaders
two parse sites at it; parse() is unchanged so the LSP keeps full data.
Measured on 10k txns: -23.6MB heap (-14.9%), -40072 allocations, -29.1M
instructions (-2.6%). Regression test + 280 parser / 89 loader tests pass.
@robcohen
robcohen force-pushed the perf/walk-descendants-presize branch from 6238175 to b25ef7c Compare June 25, 2026 10:39
parse_via_cst_opts is pub; rustdoc -D warnings rejects a public doc linking to
the private walk_descendants_once. Use a plain code span instead of a link.
@robcohen
robcohen merged commit 0182a09 into main Jun 25, 2026
47 of 71 checks passed
@robcohen
robcohen deleted the perf/walk-descendants-presize branch June 25, 2026 12:23
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.

2 participants