Skip to content

fix(parser): find a repeated metadata key in linear time in the line-check recorder - #3129

Merged
robcohen merged 2 commits into
mainfrom
fix/recorder-dup-meta-linear
Nov 29, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/recorder-dup-meta-linear

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

The line-check recorder from #3113 (ADR 0010) checks each metadata key against a plain list of the earlier keys on the same transaction or posting, so one entry with n keys costs n²/2 string comparisons.

KeySet keeps the list for the first 16 keys, which covers every real ledger, then moves into a hash set.

Why

metadata_many_keys (one transaction with 50,000 keys) regressed at #3113:

build before #3113 main this PR
release 0.11 s 1.54 s 0.12 s
debug 0.73 s 6.7 s 0.82 s

On CI runners it takes 21 s against its 15 s bound, so main's Test job is red, and every open PR's build check fails with it. Callgrind on main: 31 billion instructions, 59% in memcmp.

Testing

  • cargo test -p rustledger-parser: 1240 passed, 0 failed.
  • New unit test test_key_set_finds_repeats_before_and_after_the_list_limit: a repeat is found both before and after the move to the hash set, and clear resets both.
  • Clippy -D warnings clean. Only the recorder's bookkeeping changes; which items get flagged is unchanged.

No CHANGELOG entry: #3113 is not released yet.

🤖 Generated with Claude Code

…check recorder

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]>
@robcohen
robcohen requested a review from a team as a code owner November 29, 2026 19:03
@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

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Instruction counts vs base (cachegrind, deterministic)

Workload base this PR Change vs v0.24.0
check/simple 357.7M 357.8M +0.04% ✅ +2.4%
check/investment 544.6M 544.8M +0.03% ✅ +5.6%
check/tagged 538.8M 539.1M +0.06% ✅ +4.6%
check/multicurrency 511.3M 511.6M +0.05% ✅ -12.5%
check/fifo 527.9M 528.2M +0.05% ✅ -0.0%
check/metadata 858.0M 858.9M +0.11% ✅ +7.6%
check-warm/simple 157.6M 157.6M -0.00% ✅ -0.0%
format/simple 977.9M 978.2M +0.03% ✅ +81.5%
format/metadata 2.49G 2.49G +0.07% ✅ +109.3%
format/errors 117.1M 117.1M +0.01% ✅ +158.5%
check/errors 297.1M 297.1M -0.01% ✅ —
check-json/errors 177.2M 177.2M +0.00% ✅ —
check/junkline 31.0M 31.0M -0.10% ✅ +305.7%
check-json/junkline 44.4M 44.3M -0.19% ✅ +621.3%
query/journal/simple 584.8M 584.6M -0.03% ✅ -6.1%
query/groupby/simple 454.0M 453.9M -0.03% ✅ -1.3%
query/groupby/investment 734.2M 734.1M -0.02% ✅ -1.5%

Instruction counts (cachegrind I refs) are deterministic: the same binary on the same input gives the same count to within ~0.02%, so unlike wall clock a 1% change is real. 🟡 ≥ 1%, 🔴 ≥ 5%. The last column is cumulative drift from the pinned v0.24.0 baseline (scripts/perf-baseline.json).

Wall clock (hyperfine)

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 44.2ms ± 3.2ms 829.4ms ± 15.4ms 18.8x 36.9 MB / 50.9 MB
investment 68.0ms ± 0.5ms 2958.2ms ± 19.0ms 43.5x 45.1 MB / 59.2 MB
fifo 68.0ms ± 0.5ms 15286.8ms ± 318.5ms 224.9x 43.3 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 44.2ms ± 0.8ms 44.2ms ± 3.2ms +0.0% ✅ within noise 37.1 MB → 36.9 MB (-0.6%)
investment 70.1ms ± 0.8ms 68.0ms ± 0.5ms -3.0% ✅ within noise 45.2 MB → 45.1 MB (-0.2%)
fifo 71.0ms ± 0.9ms 68.0ms ± 0.5ms -4.2% ✅ within noise 43.0 MB → 43.3 MB (+0.5%)

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

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@robcohen
robcohen merged commit 8790bdf into main Nov 29, 2026
66 of 67 checks passed
@robcohen
robcohen deleted the fix/recorder-dup-meta-linear branch November 29, 2026 19:41
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