Repository navigation
perf(booking): Optimize inventory cloning and capacity hints - #756
Merged
Merged
Conversation
rust-skills: mem-vec-with-capacity, own-clone-excessive Applied rust-skills review recommendations: 1. book.rs:203-211 - Clone only needed inventories - Previously cloned ALL inventories for every transaction - Now clones only inventories for accounts in the transaction - Reduces cloning from O(all accounts) to O(txn accounts) 2. interpolate.rs:120 - Add Vec::with_capacity(2) - unassigned_missing typically has 1-2 postings 3. pad.rs:102-106 - Add capacity hints - inventories: directives.len().min(16) - pending_pads: 4 (typical max concurrent pads) - padding_transactions: directives.len().min(16) - errors: 4 (rare to have many pad errors) 4. lib.rs:69 - Add #[must_use] attribute - infer_cost_currency_from_postings returns important value Impact: Reduces memory allocations for large ledgers with many accounts. Typical transaction touches 2-4 accounts, but previous code cloned all accounts (could be 100+). Tests: All 83 tests pass (75 unit + 8 prop).
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
📊 Benchmark Results
vs main branch (2026-04-11)
What do these numbers mean?
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves booking-engine performance by reducing unnecessary cloning of inventories during transaction booking and by adding small with_capacity hints to common collections to reduce heap allocations.
Changes:
- Optimize
BookingEngine::bookto clone only inventories relevant to the current transaction’s accounts. - Add small
with_capacityhints in interpolation and pad processing to avoid predictable reallocations. - Add
#[must_use]to an internal helper returningOptionfor clearer API usage.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
crates/rustledger-booking/src/book.rs |
Limits working inventory cloning to accounts touched by the transaction (but currently re-clones per posting). |
crates/rustledger-booking/src/interpolate.rs |
Pre-allocates unassigned_missing with a small expected capacity. |
crates/rustledger-booking/src/pad.rs |
Adds initial capacity hints for inventories, pending pads, padding transactions, and errors. |
crates/rustledger-booking/src/lib.rs |
Marks infer_cost_currency_from_postings as #[must_use] to discourage ignoring the result. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
The working_inventories population loop cloned the same inventory on every iteration when `txn.postings` contained multiple postings for the same account (e.g., main sale + fee posting on `Assets:Stock`). Each iteration called `.insert(...)` with a fresh clone, silently overwriting the previous entry and erasing the very optimization this PR is about. Switch to `entry().or_insert_with(|| inv.clone())` so each unique account is cloned exactly once regardless of how many postings reference it. Added a comment so the next person to touch this loop understands why the entry API is load-bearing here. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
robcohen
enabled auto-merge
April 11, 2026 16:20
robcohen
added this pull request to the merge queue
Apr 11, 2026
Merged
robcohen
added a commit
that referenced
this pull request
Apr 11, 2026
Bumps the whole workspace from 0.11.0 to 0.12.0 (minor version bump under pre-1.0 semver because the cycle since 0.11.0 includes user-visible breaking changes that release-plz's auto-detection missed). release-plz auto-generated a v0.11.1 patch bump from the `fix:` / `feat:` commits since 0.11.0. That's too conservative. The cycle since 0.11.0 shipped the following breaking changes, all merged as `refactor:` / `fix:` without the `!` conventional-commit marker: - **`rustledger_booking::BookingError`** collapsed three variants (`InsufficientUnits`, `NoMatchingLot`, `AmbiguousMatch`) into a single `Inventory(AccountedBookingError)` variant (#751). Out-of-tree consumers pattern-matching on the old variants break. - **`rustledger_core::AccountedBookingError`** added as a new canonical user-facing type with `BookingError::with_account()` as the constructor (#751). Not a break on its own, but part of the unification. - **`rustledger_lsp::handlers::diagnostics::all_diagnostics`** signature changed to take an additional `other_buffer_overlays: &[(u16, &[Spanned<Directive>])]` parameter for multi-buffer LSP overlays (#760 / #764). - **`rustledger_lsp::handlers::diagnostics::validation_errors_to_diagnostics`** signature changed to take an owned `Vec<Spanned<Directive>>` instead of borrowed slices (#758). Optimizes a hot path but breaks the function's public signature. - **`rustledger_loader::LoadError::IncludeCycle`** Display format rewritten to match Python beancount's `"Duplicate filename parsed: ..."` wording (#767). Substring-matching consumers break. Under pre-1.0 semver, any breaking change should bump the minor version (0.x → 0.(x+1)). So 0.11.0 → 0.12.0 is correct. Highlights since 0.11.0: ### Breaking - Unified `BookingError` under a single `Inventory` variant with one canonical `Display` impl, preventing the wording drift that produced #748 (#751). - `LoadError::IncludeCycle` Display now begins with `"Duplicate filename parsed: "` to match beancount's wording for conformance (#767). - LSP `all_diagnostics` and `validation_errors_to_diagnostics` signatures changed to support the multi-buffer overlay and avoid double-cloning directive lists (#758 / #764). ### Features - LSP live overlay so in-memory buffer edits reflect in diagnostics before save, for both the current file (#685 / #758) and any other open buffer in a multi-file ledger (#760 / #764). - Parser `Vec::with_capacity` hints with DoS caps on `source.len()` heuristics (#754). - `working_inventories` cloning in the booking engine now only touches accounts actually referenced in the transaction, and dedupes repeated account references (#756). ### Fixes - `InsufficientUnits` booking error wording restored to `"Not enough units in ..."` so pta-standards `reduction-exceeds-inventory` conformance assertion passes (#748 / #749). - `noduplicates` plugin now hashes flag, tags, links, cost, and price in addition to date and narration — complete beancount `hash_entry` parity (#746 / #747). - Parser test suite: 14 fake-coverage tests replaced with real tests that exercise the functions they claim to cover (#766). - All Copilot inline review comments on #754-#763, #766, #767 addressed in fix commits. - Closed PRs #768 (dead incremental parser) and #769 (hallucinated simdutf8 API) — no code shipped from these. ### Internal - Forbid-unsafe-code invariant now has a dedicated CI check and pre-push hook (#770). - New pre-push `cargo check` hook to catch compile errors before PRs are opened (#770). This commit supersedes release-plz's auto-generated v0.11.1 proposal to produce the minor bump the breaking changes require. Co-Authored-By: Claude Opus 4.6 (1M context) <[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.
Summary
Optimizes inventory cloning in booking engine and adds capacity hints to reduce heap allocations.
Changes
Impact
rust-skills Rules Addressed
mem-vec-with-capacity✅ Fixedown-clone-excessive✅ Fixed (inventory cloning)api-must-use✅ Fixed