Skip to content

perf(returns): compute all groups' self-containment in one transaction pass - #1845

Merged
robcohen merged 4 commits into
mainfrom
perf/returns-selfcontain-fold
Jul 22, 2026
Merged

robcohen merged 4 commits into
mainfrom
perf/returns-selfcontain-fold

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

The final #1832 item. The --by-group self-containment warning check called first_shared_inscope_account once per group, each re-walking the whole directive stream — O(groups × transactions) stream passes (classification only, no booking). Replaces it with shared_inscope_accounts, which resolves every group's first shared in-scope account in one pass over the transactions (per-group cursors, with an early exit once a group resolves).

Correctness

Warning text and order are unchanged — this only reorders the scan. The per-group first_shared_inscope_account is kept as a #[cfg(test)] reference oracle, and shared_inscope_accounts_matches_per_group pins batch == per-group on a pooled-cash fixture (two non-self-contained groups sharing Assets:Broker:Cash + one self-contained group funded from outside the scope). The existing self-contained / prefix-overlap integration tests (which assert the exact stderr warnings) pass unchanged.

Scope

Closes #1832 — with this, the whole returns-perf arc is done:

Part of #1814.

🤖 Generated with Claude Code

…n pass

The --by-group self-containment warning check called first_shared_inscope_account
once per group, each re-walking the whole directive stream — O(groups × txns)
stream passes (classify only, no booking). Replace it with shared_inscope_accounts,
which resolves every group's first shared in-scope account in ONE pass over the
transactions (per-group cursors with an early-exit once a group resolves).

The per-group first_shared_inscope_account is kept as the #[cfg(test)] reference
oracle; shared_inscope_accounts_matches_per_group pins the batch == per-group across
a pooled-cash fixture (two non-self-contained groups + one self-contained). Warning
text and order are unchanged; the self-contained/overlap integration tests pass.

Closes #1832.
Copilot AI review requested due to automatic review settings July 22, 2026 00:30
@robcohen
robcohen requested a review from a team as a code owner July 22, 2026 00:30

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

This PR optimizes the report returns --by-group self-containment warning by replacing the per-group full directive-stream rescan with a batched scan that determines each group’s first shared in-scope account in a single pass over the transaction directives. This fits into the broader returns-reporting performance work by removing an avoidable N× multiplier when many groups are present.

Changes:

  • Replaced per-group first_shared_inscope_account scans with a batched shared_inscope_accounts transaction pass to resolve all groups’ warnings together.
  • Kept the original per-group implementation as a #[cfg(test)] reference oracle.
  • Added a drift-guard test asserting the batched results match the per-group oracle per scope.

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

@github-actions

github-actions Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 34.7ms ± 0.6ms 23.7 MB
beancount 754.4ms ± 3.3ms 50.7 MB
Comparison 21.7x faster 2.1x less

vs main branch (2026-07-21)

Metric main this PR Change
Time 33.5ms 34.7ms +3.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

@codecov

codecov Bot commented Jul 22, 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

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 812
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

Deep review noted the fixture did not exercise the order-sensitive path. Add a
Mixed group whose FIRST touching transaction is self-contained (funded from an
outside bank) so it must resolve at a LATER transaction that shares pooled cash;
that resolving txn has two qualifying postings, pinning first-match selection.
Both paths a future batch-vs-reference divergence could break are now covered.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013gJH7uBBF9Xw9et9QFRcsU
Copilot AI review requested due to automatic review settings July 22, 2026 01:03

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Copilot review: once unresolved hits 0 mid-transaction, the remaining groups are
already resolved, so break the inner loop rather than skipping them one by one.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013gJH7uBBF9Xw9et9QFRcsU
Copilot AI review requested due to automatic review settings July 22, 2026 01:32

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

…e_accounts

Copilot review: the group loop scanned txn.postings twice (any() for touches, then
find() for the shared account). Collapse to a single pass that tracks both, with
identical semantics (first-match posting order, resolve only when the group is
touched). Drift guard against the per-group reference still holds.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013gJH7uBBF9Xw9et9QFRcsU
Copilot AI review requested due to automatic review settings July 22, 2026 01:57

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@robcohen
robcohen merged commit 1247da8 into main Jul 22, 2026
49 checks passed
@robcohen
robcohen deleted the perf/returns-selfcontain-fold branch July 22, 2026 02:20
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.

returns: single-pass realization for twr + shared per-group extraction (currently ~O(groups × flows × directives))

2 participants