Skip to content

ci(ratchet): forbid std SipHash collections in hot-path modules (#1237) - #1364

Merged
robcohen merged 3 commits into
mainfrom
chore/1237-hot-path-collections
Jun 15, 2026
Merged

robcohen merged 3 commits into
mainfrom
chore/1237-hot-path-collections

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

Second of the #1237 static-analysis ratchets: forbid std SipHash HashMap/HashSet in hot-path modules, so the FxHasher adoption (#1076) can't silently regress.

Like #1363, this is a grep ratchet (no nightly toolchain), using the marker-based opt-in pattern of check-unsafe-invariant.sh.

How it works

A module opts in with a marker near the top:

// ratchet: fxhash-only

scripts/check-hot-path-collections.sh finds every marked file and fails if its non-test code uses std::collections::HashMap/HashSet. It excludes #[cfg(test)] modules and leaves BTreeMap (deterministic/sorted) alone. Escape hatch: // ratchet-allow: std-collections <reason>.

Verified: passes clean, catches a real (non-test) violation injected into a marked file, and correctly ignores both test modules and unmarked files.

Changes

  • scripts/check-hot-path-collections.sh + a Hot-path Collections CI job (anchored after plugin-test-quality to avoid colliding with ci(ratchet): forbid std::sync Mutex/RwLock in library code (#1237) #1363's sync-primitives insertion).
  • Marked five genuinely-hot, already-clean modules: booking book.rs, core inventory/mod.rs, loader process.rs (the booking pass), validate balance.rs, query executor/types.rs.
  • Converted the one std collection sitting in a hot path — book.rs's local booked_indices dedup set — from std::collections::HashSet to FxHashSet (drop-in: only insert/contains/into_iter).

Modules that use std maps deliberately are left unmarked: query aggregation.rs (GROUP BY map with an explicit key_order for determinism) and the ShiftSpans trait impl for HashMap<_, _, S>. The ratchet is opt-in and extensible.

Test plan

  • nix develop --command cargo test -p rustledger-booking — passes
  • nix develop --command cargo clippy -p rustledger-booking -p rustledger-core -p rustledger-query -p rustledger-validate -p rustledger-loader --all-features --all-targets -- -D warnings — clean
  • ./scripts/check-hot-path-collections.sh — passes; injected violation rejected; unmarked/test usages ignored

Part of #1237. Independent of #1363 (different script + CI anchor; only book.rs overlaps, in disjoint regions).

@codecov

codecov Bot commented Jun 15, 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

github-actions Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 43ms ± 3.8ms 25.3 MB
beancount 1063.8ms ± 12.4ms 50.7 MB
Comparison 24.7x faster 2.0x less

vs main branch (2026-06-15)

Metric main this PR Change
Time 36.1ms 43ms +19.1% 🔴
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

Adds a new CI “ratchet” to prevent performance regressions by forbidding std::collections::HashMap/HashSet (SipHash-backed) inside explicitly marked hot-path Rust modules, reinforcing the workspace’s FxHasher adoption.

Changes:

  • Introduces scripts/check-hot-path-collections.sh and wires it into CI as a dedicated “Hot-path Collections” job.
  • Adds // ratchet: fxhash-only markers to selected hot-path modules to opt them into the check.
  • Converts a hot-path dedup set in booking from std::collections::HashSet to rustc_hash::FxHashSet.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
scripts/check-hot-path-collections.sh New grep-based ratchet script to scan marked modules for forbidden std collections.
.github/workflows/ci.yml Adds a CI job to run the new ratchet script on PRs that should build.
crates/rustledger-booking/src/book.rs Switches a local dedup set to FxHashSet and opts the module into the ratchet.
crates/rustledger-core/src/inventory/mod.rs Opts this hot-path module into the ratchet via marker comment.
crates/rustledger-loader/src/process.rs Opts this hot-path module into the ratchet via marker comment.
crates/rustledger-query/src/executor/types.rs Opts this hot-path module into the ratchet via marker comment.
crates/rustledger-validate/src/validators/balance.rs Opts this hot-path module into the ratchet via marker comment.

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

Comment thread scripts/check-hot-path-collections.sh Resolved
Comment thread scripts/check-hot-path-collections.sh Resolved
Comment thread crates/rustledger-booking/src/book.rs Resolved
@github-actions

github-actions Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 792
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 3 commits June 15, 2026 09:15
Second of the #1237 ratchets, again as a grep ratchet (no nightly toolchain),
using the marker-based opt-in pattern of `check-unsafe-invariant.sh`.

FxHashMap/FxHashSet were adopted on hot paths in #1076 because std's SipHash
default is much slower for the small-int/short-string keys these paths use.
Nothing prevented a new `std::collections::HashMap` from regressing that.

- `scripts/check-hot-path-collections.sh`: finds files marked
  `// ratchet: fxhash-only`, and fails if their non-test code uses
  `std::collections::HashMap`/`HashSet`. Excludes `#[cfg(test)]` modules and
  leaves `BTreeMap` (deterministic, sorted) untouched. Escape hatch:
  `// ratchet-allow: std-collections <reason>`. Verified it passes clean,
  catches a real (non-test) violation in a marked file, and ignores both
  test modules and unmarked files.
- "Hot-path Collections" CI job (anchored after plugin-test-quality to avoid
  colliding with the sync-primitives job in #1363).
- Marked five genuinely-hot, already-clean modules: booking `book.rs`, core
  `inventory/mod.rs`, loader `process.rs` (the booking pass), validate
  `balance.rs`, query `executor/types.rs`.
- Converted the one std collection in a hot path — `book.rs`'s local
  `booked_indices` dedup set — from `std::collections::HashSet` to
  `FxHashSet` (drop-in: insert/contains/into_iter only).

Modules that use std maps deliberately (e.g. query `aggregation.rs`'s
GROUP BY map with its explicit `key_order`, and the `ShiftSpans` trait impl
for `HashMap<_,_,S>`) are simply left unmarked; the ratchet is opt-in and
extensible.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Deep-review fixes:

- BUG (toothless ratchet): the `hot-path-collections` job was not in the
  `build` aggregator's `needs` list or its `results` string, so a red
  ratchet did NOT block merge (`build` is the only required check). Added it
  to both.
- Harden the test-module stripper: it stopped at the FIRST `#[cfg(test)]`,
  so a test-only `use`/`fn` near the top of a marked file would silently
  disable scanning of all real code below it. Now it only truncates at the
  trailing `#[cfg(test)] mod`, preserving scanning of non-test code while
  still ignoring the test module. Verified both behaviors.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Review nit on the FxHashSet conversion: `FxHashSet::default()` dropped the
`with_capacity(txn.postings.len())` preallocation the std version had —
a (small) rehash/growth regression in a hot path, ironic for a hot-path
ratchet PR. Restored via `with_capacity_and_hasher`.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@robcohen
robcohen force-pushed the chore/1237-hot-path-collections branch from 5e459af to 2926be6 Compare June 15, 2026 09:17
@robcohen
robcohen enabled auto-merge (rebase) June 15, 2026 09:17
@robcohen
robcohen merged commit 71192e6 into main Jun 15, 2026
52 checks passed
@robcohen
robcohen deleted the chore/1237-hot-path-collections branch June 15, 2026 09:23
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.

2 participants