Skip to content

search: cache out-of-core expansions per base record instead of per window (#72) - #73

Merged
ypriverol merged 1 commit into
mainfrom
perf/mmap-base-record-cache
Sep 9, 2026
Merged

ypriverol merged 1 commit into
mainfrom
perf/mmap-base-record-cache

Conversation

@ypriverol

@ypriverol ypriverol commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #71 (base branch fix/precursor-cal-honours-mmap); addresses #72.

Problem

On a PTM-rich out-of-core search (PXD007653 phospho: Phospho S/T/Y + Ox-M + Acetyl, NumMods=4, mouse 1:1 entrapment DB) a native profile showed ~95% of CPU in peptidoform generation: expand_recursive 56%, the per-window expansion wrapper 23%, malloc/free 14%; scoring below 1.5%. Every spectrum re-expanded the base peptides of its windows, and consecutive windows overlap heavily, so the same base peptide was rebuilt for almost every spectrum that reached it. The per-chunk window cache from #67 could not help: it caches whole windows, and its 4M-candidate budget covers only ~80 phospho windows per 5,000-spectrum chunk.

Change

The per-chunk cache is keyed by base record instead of by window:

  • base_records_for_nominal_window fetches a window's base records (index range scans, cheap) with each record's copy count — the RAM enumeration multiplicity — taken from the scan instead of a per-candidate index query.
  • expand_base_record expands one record into all its peptidoforms, no window filter.
  • Per chunk, the distinct records of every window are fetched once, expanded once in parallel under the same candidate budget, and each spectrum assembles its candidate list from them.
  • The assembly reproduces the in-RAM multiset and order: record-level dedup (a record's copies share one mass, so a window holds all or none), the exact RAM nominal-bucket predicate applied per peptidoform against the union of the spectrum's windows, the pass-tagged stable sort, and the global collision-decoy relabel — all unchanged in effect.
  • lazy_candidates_for_nominal_window is now a composition of the two new functions and keeps its contract; the in-crate tests that pin it to the RAM enumeration still pass.

Evidence

measurement before after
phospho slice, 300 spectra, 16 threads (cluster) 118 s 53 s, same 2,878 rows
profile after: generation share ~95% 43% expand_recursive + 3% terminal variants; the scoring closure is now 25%
E. coli fixture, mmap, 4 threads 15.5 s 15.3 s, identical rows to RAM
cargo test --workspace 1,109 passed
ignored RAM/mmap equivalence gates (4, incl. calibration-on) all pass

Full 5,000-spectrum chunk timing at the default and a 16M budget is running on the cluster against the 7,100 s baseline; I will post it here.

What remains after this is the expansion itself (recursive clone per form); that is the next lever in #72.

Summary by CodeRabbit

  • Performance
    • Improved out-of-core candidate generation by reusing base-record expansions across spectra and nominal windows.
    • Preserved duplicate record multiplicity during candidate generation for more accurate results.
    • Added bounded per-chunk caching with fallback expansion when the cache limit is exceeded.
  • Bug Fixes
    • Improved handling of candidate expansion, nominal-window filtering, pass-index tagging, and collision decoys.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@coderabbitai

coderabbitai Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: aca07139-49da-46d0-ba33-0f8475caf925

📥 Commits

Reviewing files that changed from the base of the PR and between 9148960 and bfb76fe.

📒 Files selected for processing (3)
  • crates/search/src/candidate_gen.rs
  • crates/search/src/match_engine.rs
  • crates/search/src/search_params.rs

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The search candidate pipeline now separates base-record retrieval from peptidoform expansion. Mmap searches cache expansions by base-record identity, preserve record multiplicity, and filter expanded candidates against exact nominal windows.

Changes

Candidate expansion

Layer / File(s) Summary
Base-record identity and retrieval
crates/search/src/candidate_gen.rs
Adds BaseRecordKey, base_record_key, and base_records_for_nominal_window. Record copies are counted across nominal windows instead of being removed by set deduplication.
Base-record expansion and lazy generation
crates/search/src/candidate_gen.rs
Adds expand_base_record. lazy_candidates_for_nominal_window now retrieves base records, expands them, and applies nominal-mass filtering.
Mmap record caching and candidate assembly
crates/search/src/match_engine.rs, crates/search/src/search_params.rs
Replaces per-window candidate caching with per-record expansion caching. Mmap candidate assembly restores multiplicity, applies PASS tagging and decoy relabeling, and uses the updated cache documentation.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Merge Risk: ⚪ Minimal · up to bfb76

The caching refactor preserves candidate output while improving runtime, with no actionable merge-blocking risk identified.

Sequence Diagram(s)

sequenceDiagram
  participant Spectrum
  participant MmapCandidateIndex
  participant MmapRecordCache
  participant expand_base_record
  Spectrum->>MmapCandidateIndex: request base records for nominal windows
  MmapCandidateIndex-->>Spectrum: return records and multiplicities
  Spectrum->>MmapRecordCache: look up BaseRecordKey
  MmapRecordCache-->>Spectrum: return cached expansion or miss
  Spectrum->>expand_base_record: expand uncached base record
  expand_base_record-->>Spectrum: return peptidoforms
  Spectrum->>Spectrum: filter, replicate, sort, and relabel candidates
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: caching out-of-core candidate expansions per base record instead of per nominal window.
Docstring Coverage ✅ Passed Docstring coverage is 88.89% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 9 functions across 3 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/mmap-base-record-cache

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ypriverol

Copy link
Copy Markdown
Contributor Author

Full 5,000-spectrum chunk of the phospho search (16 threads, --precursor-cal off, cluster), this stack vs main:

build chunk wall peak RSS rows
main (#71) ~7,100 s – –
#73 + #74, default 4M budget 409 s 13.5 GB 94,171
#73 + #74, 16M budget 428 s 23.5 GB 94,171

About 17x on a whole chunk; the larger cache budget buys nothing here, so the default stays. Whole-file projection ≈ 2.3 h at 16 threads instead of ~40 h.

ypriverol added a commit that referenced this pull request Sep 8, 2026
… MaxQuant reference table (#75)

* benchmarks: phospho tier — dataset, mods, MaxQuant reader, reference table

First PTM-enriched benchmark. PXD007653 (Krahmer/Mann mouse liver EasyPhos, Q Exactive,
single-shot TiO2, label-free), one file: 20151014_QEp6_NaKr_SA_totalliver_control2_phospho.raw,
3.72 GB. Verified against the shipped model store's source ledger — it is NOT training data,
unlike the obvious pick PXD014525, which trained hcd_qexactive_tryp_phosphorylation.

make_truth.py gains a maxquant reader over msms.txt: decoys dropped (Reverse), PEP <= 0.01,
one row per (run, scan), filtered to the run actually searched. It keeps MaxQuant's modified
sequence in a seventh column so localisation can be compared later; emit() widens the header
only when a reader supplies it, so the glyco tables keep their six columns byte-for-byte.
The committed table holds 23,563 spectra, 17,072 of them phospho-bearing.

The depositors searched UP000000589, the same mouse proteome the 1:1 shuffled entrapment
database is built from, so the comparison is like-for-like apart from isoforms.

Numbers follow once the run lands; nothing is claimed here.

* benchmarks: mods file takes one residue per line; NumMods=4

The Phospho line specified STY as a single residue spec, which the parser rejects (it takes
one residue or `*`), so both arms failed at startup. Split into three lines. NumMods raised
3 -> 4 because 0.2% of the reference peptides carry four variable mods and none carry more
than four that we need; at 3 those would be unreachable by construction.

* benchmarks: commit the MaxQuant phospho reference table (PXD007653, one file)

* benchmarks: first PTM-enriched benchmark — PXD007653 phospho, one file, both arms

Default model 36,817–36,922 PSMs @1% (true FDP 1.22–1.32%), --protocol
phospho 36,780–36,884 (1.10–1.14%), ~26.7k phospho-bearing PSMs; 82.1% of
the MaxQuant reference scans covered with 98.3% bare-peptide agreement.
Binary d606d962 (main + #71 + #73 + #74), provenance and recipe recorded.
Site localisation is not scored and is listed as the open gap.
@ypriverol
ypriverol added this pull request to stack #81 September 9, 2026 19:08
Base automatically changed from fix/precursor-cal-honours-mmap to main September 9, 2026 19:09
…72)

On a PTM-rich search the out-of-core path spent ~95% of CPU generating
peptidoforms: every spectrum re-expanded the base peptides of its
windows, and consecutive windows overlap heavily. The per-chunk cache is
now keyed by base record: the distinct records of every window in the
chunk are fetched once (index scans), expanded once in parallel under the
same candidate budget, and each spectrum assembles its list from them.

The assembly reproduces the in-RAM multiset and order: records are
deduplicated at the record level (a record's copies share one mass, so a
window holds all or none of them), the RAM nominal-bucket predicate is
applied per peptidoform against the union of the spectrum's windows, the
multiplicity comes from the copy count in the index scan instead of a
per-candidate index query, and the pass-tagged stable sort is unchanged.
lazy_candidates_for_nominal_window is now a composition of the two new
functions and keeps its contract.
@ypriverol
ypriverol force-pushed the perf/mmap-base-record-cache branch from cea86dd to bfb76fe Compare September 9, 2026 19:09
@ypriverol
ypriverol merged commit 8cda8b3 into main Sep 9, 2026
4 of 5 checks passed
@ypriverol
ypriverol deleted the perf/mmap-base-record-cache branch October 15, 2026 16:05
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.

1 participant