Repository navigation
perf(booking)!: merge padding transactions in one pass, not one insert each - #2919
Merged
Merged
Conversation
…t each Every consumer of the pad-expanded stream (merge_with_padding, its owned and spanned variants, Ledger::balance_view, and ffi-wasi's expand_pads) inserted each synthesized padding transaction into the date-sorted directive vector one at a time, at an index found by scanning the whole vector: O(n x k). On a ledger with a pad per account, `rledger report balances` and BQL were quadratic. pad_insertion_index is replaced by merge_padding_into_sorted, a generic single-pass merge: the synths are stably sorted by date, and each is emitted just before the first original dated strictly after it. That is exactly the order the one-by-one insert produced (originals first on a date tie, synths in their own order among themselves), which a test pins against a copy of the old algorithm on randomized tie-heavy shapes and end to end through merge_with_padding. A second test counts date lookups and fails on the old insert (37,507,500 against a bound of 30,000). Instructions on 20,000 padded accounts: report balances 35.7G -> 1.1G, BQL 41.8G -> 2.0G, and the branch's counts double from 20,000 to 40,000. Output is byte-identical to main on every in-tree and compat ledger. Closes #2904 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Contributor
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
balance_view hands its owned vector to merge_with_padding_owned rather than inlining the merge; merge_padding_into_sorted bounds date_of calls at 2(n + k) in total, not twice per element; and the same-date-Balance note describes the placement rule, not the removed function. Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # CHANGELOG.md
robcohen
added a commit
that referenced
this pull request
Nov 10, 2026
Brings in #2908 (strict account validator; loader v66, WASM v87), #2919 and #2924. Keeps this branch at loader v68 / WASM v90 with one note per version, drops the merged warn_if_invalid_root (rejection now goes through check_root), and adds the issue's `A😀` root to the rejected-name tests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen
added a commit
that referenced
this pull request
Nov 10, 2026
Brings in #2919's linear merge_padding_into_sorted (with the tolerance_multiplier threaded through), #2908 and #2924. WASM cache v88, loader pin 66. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
Every consumer of the pad-expanded stream (
merge_with_padding,merge_with_padding_owned,merge_with_padding_spanned, and through themLedger::balance_view, the CLI query path, wasm and the FFI component; plus ffi-wasi'sexpand_pads) placed each synthesized padding transaction withpad_insertion_index. That meant one full scan and oneVec::insertper synth, O(n x k). With a pad per account,rledger report balancesand BQL were quadratic. Thepad_insertion_indexdoc comment named this as the condition to revisit ("If a ledger ever arrives with pads in the thousands, merge the (date-sorted) synths in one pass instead").pad_insertion_indexis replaced byrustledger_booking::merge_padding_into_sorted(sorted, synths, date_of). It's a single-pass merge, generic over the element type, so all three element shapes (Directive,Spanned<Directive>,(Directive, tag)) call the one canonical rule instead of each driving the index helper themselves. This removes a public function fromrustledger-booking, hence the!and a Breaking CHANGELOG entry.How the merge works
sort_by_key(date)over the booking-sorted input, so this adds no new sort key and re-derives nothing.sort_by_cached_key, one key computation per synth).Why the order is identical
The old rule inserted each synth, in turn, after the last element dated on or before it. So a synth lands after every original and every earlier-inserted synth dated on or before it, and ahead of everything dated later. The final sequence is therefore:
That is exactly what the merge emits: a strict
<puts the synth after same-date originals, and stable sorting keeps same-date synths in their original order.Tests (in
crates/rustledger-booking/src/pad.rs):test_merge_padding_into_sorted_matches_one_by_one_insertcompares against a test-only copy of the old algorithm over 200 seeded random shapes. These are tie-heavy (1 to 8 distinct dates), with synths dated before, inside and after the stream and arriving in arbitrary order; every element is distinct, so equality pins order.test_merge_with_padding_matches_one_by_one_insert_on_a_padded_ledgerruns the same comparison end to end throughmerge_with_paddingon 60 padded accounts. Their pads share dates with each other, with unrelated transactions and with unrelated balances.test_merge_padding_into_sorted_is_linearis the regression test. It counts date lookups (at most 2 per element). I temporarily put the one-by-one insert back insidemerge_padding_into_sorted, and it failed withmerge made 37507500 date lookups for n = 5000, k = 5000; a linear merge makes at most 30000. Both equivalence tests passed against that reverted version, as they should.#2150,#2188, the spanned and ffi-wasi per-consumer pins) pass unchanged.Measurements
Release builds of
main(b7abb65, after #2898) and this branch, in separate worktrees and target dirs. The ledgers are N accounts, each with an open, a transaction, a pad and a later balance, spread over 1,500 dates, plus a pad-free ledger of 100,000 transactions over 50 accounts. BQL isSELECT account, sum(position) GROUP BY account.Instructions (cachegrind, deterministic). Main at 40k was not run under valgrind; extrapolating its 4x per doubling gives about 140G and 165G.
On main, each doubling multiplies the count by about 4. On the branch it doubles. The pad-free ledger and
check(which never used the merge) are unchanged.Wall clock. Best of 5 interleaved runs (2 where a run exceeded 10 s), in seconds. The machine was shared with other builds (load average 25 to 57), so later rows are noisy; trust the instruction counts above for ratios.
Byte-identical output
On every ledger in the tree (382
.beancount/.beanfiles outsidetests/compatibility/files) and in the compat corpus (731 files), main and branch outputs were compared withcmpon stdout+stderr+exit code forrledger report <file> balances,rledger query <file> "SELECT * FROM #postings"andrledger check <file>. Result: 3,339 of 3,339 identical. The generated 10k and 20k pad ledgers (20k emits 40,000 padding postings in#postings) are also identical on all three commands.Checks run locally
cargo test --workspace --all-features --no-fail-fast,cargo clippy --workspace --all-features --all-targets -- -D warnings,RUSTDOCFLAGS=-Dwarnings cargo doc --workspace --no-deps,cargo fmt --all --check, and the STRICTcorpus_baselineandcst_baselineall pass. No cache bump: output does not change.Closes #2904
🤖 Generated with Claude Code