Skip to content

fix(search): make the out-of-core candidate backing retrieve the same set as in-RAM (#65) - #66

Merged
ypriverol merged 1 commit into
mainfrom
fix/mmap-ram-equivalence
Sep 7, 2026
Merged

ypriverol merged 1 commit into
mainfrom
fix/mmap-ram-equivalence

Conversation

@ypriverol

@ypriverol ypriverol commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #65. Two independent defects, both confined to the Mmap arm; the in-RAM path is untouched and its output is byte-identical before and after (verified on three modification configurations).

Fixed modifications were subtracted from the base-mass lookup. base_record_multiplicity recovered a base peptide's mass by subtracting every modification delta, but the on-disk index stores masses with fixed modifications still folded in — it zeroes only max_variable_mods_per_peptide. So any peptide carrying Carbamidomethyl was looked up 57.021 Da too light, matched no record, and count.max(1) silently returned 1, dropping the duplicate copies the RAM enumeration keeps. Now only non-fixed deltas are subtracted. The evidence is clean: on the instrumented divergent scan, all 18 differing rows were RAM-only and every one contained a cysteine.

Copy ordering did not match the enumeration. enumerate_protein walks a protein twice, at offset 0 and again at offset 1 for N-terminal-Met cleavage, so RAM's order is all pass-0 spans followed by all pass-1 spans, with copies separated. The disk path pushed copies back to back and sorted on (protein, start_offset) only, which cannot distinguish the passes. That perturbed an order-dependent per-spectrum float accumulator and changed RawScoreCal. Copies now carry their pass index and sort (protein, pass, start_offset, length).

Acceptance

configuration in-RAM out-of-core identical
default mods 77,097 77,097 yes
fixed mods only 75,739 75,739 yes
one variable mod 77,129 77,129 yes

Before the fix these were 77,097/77,526, 75,739/76,290 and 77,129/77,425. The in-RAM column is unchanged in all three.

Regression test

crates/andes/tests/mmap_ram_pin_equivalence.rs, three tests over the real fixtures, #[ignore]d because they take ~246 s in release:

cargo test -p andes --release --test mmap_ram_pin_equivalence -- --ignored

They were checked to be non-vacuous by re-running against the unfixed code, where all three fail. The four pre-existing equivalence tests pass while the bug is live, which is why a fixture-scale test was needed.

Not addressed here, and still open: --candidate-index mmap is silently downgraded to in-RAM when precursor-calibration reuse is active, which is why the reproduction needs --precursor-cal off. The new test documents that so the gate cannot pass vacuously.

Summary by CodeRabbit

  • Bug Fixes

    • Improved consistency between RAM and memory-mapped search results.
    • Corrected candidate ordering and modification handling to prevent order-dependent scoring differences.
    • Preserved expected candidate multiplicity while applying fixed and variable modifications correctly.
  • Tests

    • Added regression coverage comparing PIN output from RAM and memory-mapped indexes across common modification configurations.

… RAM (#65)

`--candidate-index mmap` and `ram` returned different PIN row sets
(77,526 vs 77,097 on the E. coli fixture) despite being documented and
tested as copy-for-copy identical. Two independent defects, both in the
out-of-core path only; the RAM path is unchanged.

1. `base_record_multiplicity` subtracted EVERY mod delta to recover the
   base-peptide mass, including FIXED mods. The on-disk index is built by
   `base_peptide_records`, which zeroes only
   `max_variable_mods_per_peptide` and keeps fixed mods folded into
   residue mass. So for any peptide bearing a fixed mod the lookup mass
   was off by the fixed total (57.02 Da for a single Cys), the mass
   window matched no record, and the multiplicity collapsed to the
   `max(1)` floor -- dropping every duplicate copy the RAM enumeration
   keeps. Only variable deltas are removed now.

2. The replicated copies were pushed back to back and ordered by
   `(protein_index, start_offset)`. RAM's global order inside a protein
   is `[whole seq_offset=0 walk] ++ [whole seq_offset=1 Met-cleaved
   walk]`, so the copies are separated by the rest of the protein's
   spans. The interleaving left the multiset right but the ORDER wrong,
   which moved the order-dependent per-spectrum float accumulator behind
   the PIN `RawScoreCal` column. Copies are now tagged with their pass
   index and sorted `(protein, pass, start_offset, length)`; a span at
   offset 1 carrying `is_protein_n_term` is pass 1 by construction
   (the flag is set iff the span starts at `seq_offset`).

Base records are also sorted with `length` in the key so equal-start
spans keep the enumeration's end-ascending order.

Verified byte-identical (`cmp` exit 0) for default mods, `NumMods=0`
fixed-only, and one variable mod. New `#[ignore]`d gate
`crates/andes/tests/mmap_ram_pin_equivalence.rs` drives the real
fixtures and fails on the unfixed code for all three configurations.
@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 7, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Team

Run ID: 6e201dce-7508-4be2-8b9e-c55c089da7f5

📥 Commits

Reviewing files that changed from the base of the PR and between 1254cb7 and cf884bf.

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

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


📝 Walkthrough

Walkthrough

The change aligns mmap candidate ordering and multiplicity lookup with RAM behavior. It adds ignored integration tests that compare generated PIN files across RAM and mmap searches using real E. coli fixtures and multiple modification configurations.

Changes

Mmap/RAM candidate equivalence

Layer / File(s) Summary
Deterministic candidate ordering
crates/search/src/candidate_gen.rs
Nominal-window base records now sort by protein, start offset, length, and flags.
Mmap replication and multiplicity lookup
crates/search/src/match_engine.rs
Mmap candidate copies preserve enumeration-pass order before decoy relabeling. Multiplicity lookup subtracts only variable modification masses.
PIN equivalence regression tests
crates/andes/tests/mmap_ram_pin_equivalence.rs
Ignored integration tests run RAM and mmap searches with default, fixed carbamidomethylation, and fixed-plus-variable oxidation modifications. The tests compare PIN headers, row counts, and sorted rows.

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

Merge Risk: ⚪ Minimal · up to cf884

This change makes mmap candidate generation match in-memory results for ordering and fixed-modification multiplicity. The documented regression coverage supports expected equivalent search output, with no remaining merge-blocking risk identified.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 9 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 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: making mmap candidate retrieval match the in-RAM path. It also identifies the relevant issue.
Linked Issues check ✅ Passed The changes address issue #65 by aligning mmap candidate ordering, correcting fixed-modification mass handling, and adding realistic regression tests for default, fixed-only, and variable-modification…
Out of Scope Changes check ✅ Passed All changes are directly related to issue #65. The candidate-generation fix, mmap matching changes, and regression tests support candidate and output equivalence between mmap and in-RAM implementation…
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/mmap-ram-equivalence

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
ypriverol merged commit faa8b10 into main Sep 7, 2026
5 checks passed
ypriverol added a commit that referenced this pull request Sep 7, 2026
…r spectrum

The out-of-core candidate path was 3-15x slower than in-RAM, and the whole gap sat
inside the search loop: on the ecoli fixture `[PHASE stream_search]` was 30.9s
out-of-core against 8.0s in-RAM, with index load negligible.

The in-RAM path enumerates candidates ONCE for the run and each spectrum reads a
bucket range. The out-of-core path re-did the base-record fetch, mod expansion,
multiplicity replication, decoy relabel and sort for EVERY (spectrum, charge) unit.
Measured on that fixture: 49,066 search units cover only 1,657 distinct nominal
windows, a 29.6x mean reuse, and every unit shares its window with another. So ~97%
of that work was repeated.

Each chunk now collects the distinct windows of its units, expands each exactly once
(in parallel), and the scoring loop reads the prepared slice. The hot path takes no
lock and memory is bounded to one chunk's distinct windows, with a cap beyond which
expansion falls back to the direct path.

The window key is the nominal bounds only: the calibration shift is
`params.precursor_mass_shift_ppm`, a run-level constant, so it cannot vary between
units of a chunk and does not belong in the key.

  stream_search, 3 runs each, same host/command:
    no cache  30.58 / 30.12 / 32.03 s   mean 30.9
    cached    13.82 / 16.63 / 18.31 s   mean 16.3   -> ~1.9x, ranges disjoint
    in-RAM reference                    8.0

Pure caching: the out-of-core PIN stays byte-identical to the in-RAM PIN in all three
modification configurations (default 77,097 rows; fixed-only 75,739; one variable mod
77,129), which is the equivalence PR #66 established.
ypriverol added a commit that referenced this pull request Sep 7, 2026
…r spectrum (#67)

The out-of-core candidate path was 3-15x slower than in-RAM, and the whole gap sat
inside the search loop: on the ecoli fixture `[PHASE stream_search]` was 30.9s
out-of-core against 8.0s in-RAM, with index load negligible.

The in-RAM path enumerates candidates ONCE for the run and each spectrum reads a
bucket range. The out-of-core path re-did the base-record fetch, mod expansion,
multiplicity replication, decoy relabel and sort for EVERY (spectrum, charge) unit.
Measured on that fixture: 49,066 search units cover only 1,657 distinct nominal
windows, a 29.6x mean reuse, and every unit shares its window with another. So ~97%
of that work was repeated.

Each chunk now collects the distinct windows of its units, expands each exactly once
(in parallel), and the scoring loop reads the prepared slice. The hot path takes no
lock and memory is bounded to one chunk's distinct windows, with a cap beyond which
expansion falls back to the direct path.

The window key is the nominal bounds only: the calibration shift is
`params.precursor_mass_shift_ppm`, a run-level constant, so it cannot vary between
units of a chunk and does not belong in the key.

  stream_search, 3 runs each, same host/command:
    no cache  30.58 / 30.12 / 32.03 s   mean 30.9
    cached    13.82 / 16.63 / 18.31 s   mean 16.3   -> ~1.9x, ranges disjoint
    in-RAM reference                    8.0

Pure caching: the out-of-core PIN stays byte-identical to the in-RAM PIN in all three
modification configurations (default 77,097 rows; fixed-only 75,739; one variable mod
77,129), which is the equivalence PR #66 established.
@ypriverol
ypriverol deleted the fix/mmap-ram-equivalence branch September 7, 2026 13:24
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.

Out-of-core candidate index disagrees with in-RAM: 17.9% of scans get a different best-scoring peptide

1 participant