Skip to content

perf(extract): load each --ledger/--existing ledger once per run - #2524

Merged
robcohen merged 2 commits into
mainfrom
perf/extract-load-ledger-once
Oct 9, 2026
Merged

robcohen merged 2 commits into
mainfrom
perf/extract-load-ledger-once

Conversation

@robcohen

@robcohen robcohen commented Oct 8, 2026

Copy link
Copy Markdown
Member

What changed

rledger extract now loads each --ledger / --existing path once per run and shares the Ledger between its three readers:

  • the --ledger profile reader (ledger_profile::load_profiles),
  • the currency lookup for an entry without currency (resolve_entry_currency → ledger_profile::open_currencies),
  • --existing dedup (duplicate::load_existing).

A new per-run cache, extract_cmd::ledgers::Ledgers, stores rustledger_loader::load's Result keyed by path. All three readers already used identical options (run_plugins: false, validate: false), and those options now live in one place. Dedup is the last reader, so it takes the ledger out of the cache by value and keeps its transactions without cloning them.

Why

One ledger given as both --ledger and --existing was parsed and booked three times per import (#2503).

Messages unchanged

Each consumer still turns the cached result into its own message, naming its own flag, at the point in the run where it did before. Only the load is shared:

  • the broken---existing warning (--existing ledger … has N error(s)), printed at dedup time;
  • no \open …` directive loaded from …, which has N load error(s)`, from the currency lookup;
  • Failed to load existing ledger: …, with the loader error as its cause (dedup takes ownership, so the anyhow context chain is intact);
  • the profile reader's failed to load ledger … / cannot be read for importer profiles.

The existing tests for each message pass unchanged.

Measured

Release builds of origin/main and this branch, on a 50,000-transaction ledger, importing a 2-row CSV with --importer bank --ledger L --existing L (an entry without currency, so all three readers run). The machine was heavily loaded (load average ~34 on 8 cores), so CPU time is the comparable number, not wall time:

CPU (user) peak RSS
main 1.36–1.39 s 158 MB
this PR 0.50–0.51 s 106 MB

With --existing alone (one load on both): 0.90 s on main, 0.50 s here. Most of that 0.40 s is the second load: on that path main loads --existing in both the currency lookup and dedup.

How tested

  • New each_ledger_is_loaded_once_per_run: runs the real pipeline through run_with_ledgers and checks the cache's load counter. One path given as both flags loads 1 time (it was 3 before), and two different paths load 2 times. It also checks the output: the currency comes from the open, the duplicate is skipped and the new row is kept. Shown to fail without the fix: with caching turned off in Ledgers::index, it fails with left: 3, right: 1.
  • cargo test -p rustledger --all-features: all pass (529 lib tests).
  • cargo clippy --workspace --all-features --all-targets -- -D warnings: clean. RUSTDOCFLAGS="-D warnings" cargo doc --no-deps -p rustledger --all-features: clean.

Not changed: a ledger that relies on plugin "auto_accounts" still has no open for the account, since plugins stay off for these loads (see #2503).

Closes #2503

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

The --ledger profile reader, the currency lookup for an entry without
`currency`, and --existing dedup each called rustledger_loader::load
themselves, with the same options, so one ledger given as both flags was
parsed and booked three times per import. A per-run `Ledgers` cache now
loads each path once and hands it to every reader; dedup, the last one,
takes it by value so its transactions are not cloned.

Each consumer still formats its own error from the cached result, at the
point in the run where it did before, so every message names the same
flag in the same order.

On a 50,000-transaction ledger given as both flags: 1.38 s -> 0.50 s CPU,
158 MB -> 106 MB peak RSS.

Closes #2503

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen
robcohen requested a review from a team as a code owner October 8, 2026 17:07
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 41.3ms ± 3.6ms 586.4ms ± 10.5ms 14.2x 51.2 MB / 50.0 MB
investment 61.3ms ± 3.3ms 2253.8ms ± 13.5ms 36.7x 55.3 MB / 58.2 MB
fifo 58.9ms ± 1.9ms 12673.2ms ± 95.8ms 215.1x 55.2 MB / 57.8 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 39.6ms ± 1.9ms 41.3ms ± 3.6ms +4.3% ✅ within noise 51.1 MB → 51.2 MB (+0.2%)
investment 62.4ms ± 4.5ms 61.3ms ± 3.3ms -1.7% ✅ within noise 55.3 MB → 55.3 MB (-0.1%)
fifo 60.4ms ± 1.9ms 58.9ms ± 1.9ms -2.4% ✅ within noise 55.2 MB → 55.2 MB (-0.1%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 1.8ms, investment 2.5ms, fifo 1.2ms), 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 Oct 8, 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 Oct 8, 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 819
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

@robcohen

robcohen commented Oct 8, 2026 •

Copy link
Copy Markdown
Member Author

Review (adversarial pass)

The cache is keyed by the path as given, so --ledger ./main.beancount --existing main.beancount still loads twice. Behavior is unchanged in that case (the result is identical, just not deduplicated). I left canonicalization out deliberately: it would change which path the error messages print.

Checked that dedup's take() happens after every other reader, so nothing reads the ledger once dedup has taken it.

Cross-PR integration (applies to #2524, #2526, #2527, #2531, #2532, #2533)

All six PRs were merged onto origin/main 25fc6be in two orders: 2524→2526→2527→2531→2532→2533, and the reverse.

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.

extract: load each --ledger/--existing ledger once per run

1 participant