Skip to content

perf(loader): run reintern_directives on fresh-parse path + drop dead SyncStringInterner - #1081

Merged
robcohen merged 3 commits into
mainfrom
perf/issue-1071-internedstr-lockdown
May 11, 2026
Merged

robcohen merged 3 commits into
mainfrom
perf/issue-1071-internedstr-lockdown

Conversation

@robcohen

Copy link
Copy Markdown
Member

Summary

Closes #1071.

Re-framed from the issue's original "API lockdown" proposal after the audit found:

  1. The actual bypass isn't a type-safety hole. InternedStr::new / From<&str> are used in ~30 constructor signatures (Posting::new(impl Into<InternedStr>, ...), etc.) and dozens of .into() call sites. A full lockdown would shatter the API surface for marginal benefit.
  2. The real problem is the missing reintern call on the fresh-parse path. The cache-hit path runs reintern_directives to deduplicate InternedStr across files (each file has its own per-file StringInterner, so identical account / currency / tag strings start out in distinct Arc<str> allocations). The fresh-parse path didn't run it, leaving every cross-file equality check falling through to byte comparison instead of Arc::ptr_eq.
  3. SyncStringInterner is dead code — zero call sites in the workspace.

Change

# What Why
1 New dedup module — reintern_directives + reintern_plain_directives extracted from feature-gated cache module into an always-on module. The walk has no rkyv dependency; gating it behind cache was incidental. Both functions remain pub and re-exported from lib.rs so external callers (rustledger's check command, rustledger-wasm) keep working.
2 Loader::load calls dedup::reintern_directives automatically at the end. Aligns fresh-parse path with cache-hit path: every consumer of LoadResult sees a deduplicated directive list.
3 Drop SyncStringInterner from rustledger-core::intern. Audit confirmed zero workspace consumers. Removing it lets us also drop the parking_lot dep from rustledger-core that was added in #1076 specifically for this type.

Why not the literal "API lockdown"?

The issue's original proposal was to make InternedStr::new pub(crate) and drop the From<&str> impls. After the audit:

  • Direct InternedStr::new / ::from sites: 9 (5 tests, 1 rkyv deserialize, 1 validator, 1 parser empty-tag fallback, 1 directive test).
  • impl Into<InternedStr> constructor signatures: ~30, depending on From<&str> being available.
  • Indirect .into() callers: dozens.

Even with the lockdown, plugin-emitted directives still go through .into() (no plugin-facing interner story), so the type-safety win would be hollow. Running reintern_directives automatically captures both legitimate non-interned construction (test fixtures, plugins) and the cross-file gap, which is what actually matters for the Arc::ptr_eq fast path.

Tests

  • New test_fresh_parse_deduplicates_internedstr_across_files — lock in the invariant via VirtualFileSystem: two files reference Assets:Bank, assert the resulting Open.account and Posting.account share one Arc via InternedStr::ptr_eq. This test fails pre-fix.
  • All existing reintern unit tests in cache.rs continue passing against the moved implementation (they now import from crate::dedup::reintern_directives).
  • Full workspace check + fmt + clippy clean.

Public API

  • pub fn reintern_directives and pub fn reintern_plain_directives are now exported from rustledger_loader unconditionally (previously gated behind cache feature). Any caller that imported them through rustledger_loader::reintern_directives keeps working.
  • SyncStringInterner removed from rustledger_core. Zero workspace consumers; this is a clean break for any out-of-tree dependent that happened to use it (unlikely given it was unused in our own code).

Test plan

  • cargo test -p rustledger-loader --all-features — 137 tests pass (new dedup test included)
  • cargo check --workspace --all-features --all-targets — clean
  • cargo fmt --all -- --check — clean
  • cargo clippy -p rustledger-core -p rustledger-loader --all-features --all-targets -- -D warnings — clean
  • CI (compatibility, regression, MSRV, fuzz)

🤖 Generated with Claude Code

… SyncStringInterner (closes #1071)

The cache-hit path already ran reintern_directives after deserializing
to deduplicate InternedStr across files; the fresh-parse path did
not. So multi-file ledgers parsed from source had identical account /
currency / tag strings landing in distinct Arc<str> allocations
(per-file StringInterner), defeating the Arc::ptr_eq fast path in
InternedStr::PartialEq and forcing every cross-file equality through
byte comparison.

Changes:

(1) Extract reintern_directives and reintern_plain_directives from
the feature-gated cache module into a new always-on dedup module.
The dedup walk has no rkyv/cache dependency; gating it behind the
cache feature was incidental. lib.rs now re-exports both functions
unconditionally.

(2) Call dedup::reintern_directives at the end of Loader::load, so
every consumer of LoadResult sees a deduplicated directive list
regardless of whether they came in via cache or fresh parse.

(3) Drop SyncStringInterner from rustledger-core::intern. The type
had zero call sites in the workspace; an audit for #1071 confirmed
it was dead code. Removing it also lets us drop the parking_lot
dependency from rustledger-core (added in #1076 specifically for
this type).

Tests:
* New test_fresh_parse_deduplicates_internedstr_across_files locks
  in the cross-file dedup invariant via VirtualFileSystem (two files
  referencing Assets:Bank; assert the resulting Posting and Open
  share an Arc via InternedStr::ptr_eq).
* Existing reintern unit tests in cache.rs continue to pass against
  the moved implementation.

Closes #1071

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner May 11, 2026 14:35
Copilot AI review requested due to automatic review settings May 11, 2026 14:35
@codecov

codecov Bot commented May 11, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.94709% with 19 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-loader/src/dedup.rs 85.49% 19 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented May 11, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 34.5ms ± 1.2ms 22 MB
beancount 931.5ms ± 10.4ms 50.3 MB
Comparison 27x faster 2.3x less

vs main branch (2026-05-11)

Metric main this PR Change
Time 34.2ms 34.5ms +0.9% ✅
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

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 improves cross-file InternedStr deduplication by ensuring the fresh-parse loader path runs the same reinterning pass as the cache-hit path, and removes unused thread-safe interner code from rustledger-core (dropping the parking_lot dependency there).

Changes:

  • Extracted reintern_directives / reintern_plain_directives into a new always-on dedup module and re-exported them from rustledger_loader.
  • Updated Loader::load to run dedup::reintern_directives on the fresh-parse path, aligning behavior with cache-hit flows.
  • Removed dead SyncStringInterner from rustledger-core and dropped parking_lot from rustledger-core dependencies; added a regression test for cross-file account dedup.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
crates/rustledger-loader/src/lib.rs Calls reinterning on fresh parse; adds regression test; updates re-exports.
crates/rustledger-loader/src/dedup.rs New always-on dedup module containing reinterning walkers.
crates/rustledger-loader/src/cache.rs Removes reinterning implementation from cache module; updates tests to import from dedup.
crates/rustledger-core/src/intern.rs Removes unused SyncStringInterner implementation and its test.
crates/rustledger-core/Cargo.toml Drops parking_lot dependency (no longer needed).
Cargo.lock Reflects dependency graph update after removing parking_lot from rustledger-core.

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

Comment thread crates/rustledger-loader/src/dedup.rs Resolved
Comment thread crates/rustledger-loader/src/dedup.rs Resolved
Comment thread crates/rustledger-loader/src/dedup.rs Resolved
Comment thread crates/rustledger-loader/src/lib.rs Resolved
Comment thread crates/rustledger-loader/src/dedup.rs Resolved
@github-actions

github-actions Bot commented May 11, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ Compatibility Test Results

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

robcohen and others added 2 commits May 11, 2026 10:41
Three concerns, all valid:

(1) Coverage gap. The pre-Copilot reintern_directive only walked
posting-level account/currency fields. It silently skipped:
  - Transaction.payee
  - Transaction.narration
  - Transaction.tags
  - Transaction.links
  - Document.tags
  - Document.links

Cross-file PartialEq on those fields fell through to byte compare.
Expanded the walk to cover all six. Extracted an intern_vec helper
for the Vec<InternedStr> cases.

(2) Double hash lookup. do_intern called contains() then intern() —
two probes per call. Added StringInterner::intern_with_status which
returns (InternedStr, was_new) from a single probe. Replaces the
existing two-call dance.

(3) Misleading docs. Module docs and Loader::load comment claimed
account/currency/tag coverage, but the implementation skipped the
tag/link/payee/narration cases. Updated both to reflect the actual
(now-correct) full coverage.

Plus a new regression test
test_fresh_parse_deduplicates_transaction_fields_across_files that
references the same payee + tag across two included files and
asserts ptr_eq on both — pre-Copilot fix this would have failed.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@robcohen
robcohen enabled auto-merge (squash) May 11, 2026 16:02
@robcohen
robcohen merged commit 9dd272e into main May 11, 2026
40 checks passed
@robcohen
robcohen deleted the perf/issue-1071-internedstr-lockdown branch May 11, 2026 16:08
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.

perf(core): lock down InternedStr bypass — From<&str> allocates without consulting an interner

2 participants