Skip to content

perf(spectrum): windowed model-locate skips find_boundary gallop edge-checks - #58

Merged
nh13 merged 1 commit into
mainfrom
perf/windowed-locate-lcp
Jun 24, 2026
Merged

perf(spectrum): windowed model-locate skips find_boundary gallop edge-checks#58
nh13 merged 1 commit into
mainfrom
perf/windowed-locate-lcp

Conversation

@nh13

@nh13 nh13 commented Jun 22, 2026

Copy link
Copy Markdown

What

The forward model-launch locate (forward_maximal_len_seeded with seed_win = None) runs find_boundary, which pays 2 gallop edge-check probes on top of the bsearch to stay robust to a boundary that falls outside the model window. bwa-meme's cold locate (CURR_SEARCH_METHOD==1) binary-searches its model window directly.

This adds forward_boundary_windowed, which does the same: it bsearches the model window [win_lo, win_hi) first. By monotonicity of the ref-vs-query comparator over the SA, an insertion point strictly inside the window IS the global boundary (both neighbors were probed by the bsearch), so it returns the byte-identical (ip, lcp_lo, lcp_hi) with zero edge probes and no separate lcp_at calls — the window bsearch's ip-1/ip probes ARE the neighbor LCPs. Only an ip landing AT a window edge falls back to the robust gallop.

This is the un-upstreamed remnant of the stale feat/v0.2-2x-spectrum branch's 10374cc, re-expressed against current main's find_boundary + separate-lcp_at shape (the old fused forward_boundary_with_lcp it patched no longer exists). The companion efd9fcf (#[inline] on compare_query_vs_suffix_2x_from) is already in main, so it is not included.

Repeat-safe cap (new vs the original)

A/B benching surfaced a regression the original commit (measured on unique reads only) never saw: for wide model windows — an uncertain model, e.g. high-occ repeats — the boundary frequently falls outside the window, where a full window bsearch is wasted work vs find_boundary's 2-probe edge-check. A WINDOW_FAST_PATH_CAP (64) sends wide windows straight to the gallop, so the fast path is taken only for tight, confident windows. The cap only selects which path computes the identical boundary, so the result is byte-identical for any window.

Measured (probe_audit, synthetic 4M-bp ref, mean SA probes/call)

corpus baseline this PR Δ
forward model_launch / unique 13.9 10.0 −28%
forward model_launch / repeat (high-occ) 42.0 42.0 neutral (cap)
est_hint_interval / unique (untouched) 8.0 8.0

Without the cap, the repeat corpus regressed to 54.0 (+29%); the cap restores it to baseline while keeping the unique win.

Byte-identity

  • seed_win = Some(..) (warm-start / hinted) callers keep the gallop unchanged.
  • prmi lib + integration suites pass. The lone release-mode should_panic failure (doubled_text_rejects_out_of_range_base) is the pre-existing debug_assert artifact (passes in debug), present on every revision — not introduced here.
  • Primary gates: mem_search_equals_maximal_forward_step, mem_search_warmstart_equals_cold, forward_spectrum_equals_oracle, backward oracles — all green.

Review

Dual-reviewed pre-push: CodeRabbit CLI (coderabbit review --agent) + the local CodeRabbitAI-style skill. CLI flagged two #[inline] consistency findings (matching find_boundary/ref_less/lcp_at), both applied.

Summary by CodeRabbit

  • Performance
    • Added a faster bounded boundary-search path for maximal-match computations when the provided model window can be trusted, reducing unnecessary probing.
  • Correctness / Reliability
    • Improved window-validation logic to safely fall back to the previous robust approach when trust cannot be established, keeping results consistent.
    • Updated seeded maximal-match behavior to support both trusted (window-derived) and untrusted window handling.
  • Tests
    • Added tests to confirm the fast path matches the existing galloping-based results across edge and adversarial window placements, and to verify parity between seeded and non-seeded variants.

@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 19661756-86df-4087-8f2d-f45be47dd0a5

📥 Commits

Reviewing files that changed from the base of the PR and between e8fd6f4 and 180d443.

📒 Files selected for processing (1)
  • prmi/src/index/spectrum.rs

Walkthrough

Adds forward_boundary_windowed, a window-size-gated binary search over trusted model windows that tracks neighbor LCP candidates in-flight, eliminating two post-boundary probes when the insertion point lands strictly inside the window. forward_maximal_len_seeded is updated to use this fast path only for model-lookup-derived windows; caller-provided windows retain the original find_boundary + explicit neighbor LCP sequence. Test validates byte-identity between fast path and fallback, and parity between trusted and untrusted entry points.

Changes

Windowed forward-boundary fast path

Layer / File(s) Summary
forward_boundary_windowed and fallback
prmi/src/index/spectrum.rs
Adds forward_boundary_windowed implementing a window-size-gated in-window binary search that tracks best_lt and best_false probes, returning (ip, lcp(ip-1), lcp(ip)) with no extra edge probes when ip is strictly interior. Adds forward_boundary_windowed_fallback for out-of-gate cases: delegates to find_boundary(0, sa_num) then issues two explicit lcp_at neighbor probes.
forward_maximal_len_seeded trusted/untrusted split and validation
prmi/src/index/spectrum.rs
Splits forward_maximal_len_seeded on whether seed_win is from the internal model lookup. Trusted path calls forward_boundary_windowed and directly reuses its neighbor LCPs; untrusted path keeps find_boundary + lcp_at(ip-1) + lcp_at(ip). Adds test forward_boundary_windowed_equals_gallop asserting byte-identity across window edge cases and parity between forward_maximal_len and forward_maximal_len_seeded when given the model window as a hint.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related issues

  • fg-labs/prmi#36: LCP-accelerates find_boundary itself through skip-aware comparators and edge-LCP tracking, complementing this PR's windowed fast path that specializes find_boundary calls for trusted model windows.

Possibly related PRs

  • fg-labs/prmi#23: Modifies the same forward_maximal_len* and neighbor LCP probe logic in spectrum.rs that this PR's windowed fast path replaces in the trusted branch.
  • fg-labs/prmi#26: Alters forward_maximal_len_seeded and forward boundary search to recover neighbor LCPs from the SA insertion point, which this PR's trusted fast path specializes.
🚥 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 accurately describes the main optimization: a windowed fast path for model-locate that skips find_boundary's gallop edge-checks when the insertion point lands strictly inside the window.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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/windowed-locate-lcp

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.

@nh13

nh13 commented Jun 22, 2026

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@nh13

nh13 commented Jun 23, 2026

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@prmi/src/index/spectrum.rs`:
- Around line 3535-3536: The condition at line 3535 performs arithmetic on `lo0`
with `lo0 + 2`, which can overflow if `lo0` is near the maximum u64 value.
Instead, compute the window width once as `hi0 - lo0` (which is safe since the
invariant ensures `hi0 >= lo0`), store it in a variable, and then use this width
for both the comparison with 2 and the comparison with WINDOW_FAST_PATH_CAP.
Replace the condition `hi0 <= lo0 + 2` with a comparison of the computed width
(e.g., `width <= 2`), eliminating the wrapping risk from direct arithmetic on
`lo0`.
- Around line 3490-3571: Add tests to verify the correctness of the new windowed
boundary search logic. Create test cases in the spectrum_oracle.rs test suite
that exercise the fast path of forward_boundary_windowed when the boundary falls
strictly inside the window (lo0, hi0), the fallback path when the window is too
small (<=2) or exceeds WINDOW_FAST_PATH_CAP (>64), and verify that
forward_maximal_len_seeded produces byte-identical results when called with
seed_win=None (trusted window) versus seed_win=Some(...) (hinted window). Use
deterministic or proptest cases to run both paths on the same input and assert
output equality, ensuring the reused neighbor LCPs and fallback edge recovery
paths are correct.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 2e9c273e-c7cd-4413-8db8-51c31dc2e0e7

📥 Commits

Reviewing files that changed from the base of the PR and between 8cb61cd and 0c1534c.

📒 Files selected for processing (1)
  • prmi/src/index/spectrum.rs

Comment thread prmi/src/index/spectrum.rs
Comment thread prmi/src/index/spectrum.rs Outdated
@nh13
nh13 force-pushed the perf/windowed-locate-lcp branch from 0c1534c to e8fd6f4 Compare June 23, 2026 04:42
@nh13

nh13 commented Jun 23, 2026

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@nh13

nh13 commented Jun 23, 2026

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@prmi/src/index/spectrum.rs`:
- Around line 5683-5694: The strictly-inside window defined in the windows array
at index 0 (with the comment about strictly-inside) is not actually strict when
ip is at boundary values like 0 or sa_num. To fix this, add a reference-lifted
query that sets up test conditions where ip is positioned in the interior of the
search space, and then add an assertion that verifies at least one window
satisfies the strict interior property: win_lo < ip < win_hi with a window width
between 3 and 64 inclusive. This ensures the fast path is actually being
exercised in the test rather than only testing fallback behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 11e0aebe-d78e-4b59-b387-cca01b0802fb

📥 Commits

Reviewing files that changed from the base of the PR and between 0c1534c and e8fd6f4.

📒 Files selected for processing (1)
  • prmi/src/index/spectrum.rs

Comment thread prmi/src/index/spectrum.rs
…-checks

The forward model-launch locate runs find_boundary, which pays 2 gallop
edge-check probes on top of the bsearch to stay robust to a boundary that
falls outside the model window. bwa-meme's cold locate (CURR_SEARCH_METHOD==1)
binary-searches its model window directly. forward_boundary_windowed does the
same: it bsearches the model window [win_lo, win_hi) first; by monotonicity of
the ref-vs-query comparator over the SA, an insertion point STRICTLY inside the
window IS the global boundary (both neighbors were probed by the bsearch), so it
returns byte-identical (ip, lcp_lo, lcp_hi) with zero edge probes and no separate
lcp_at calls (the window bsearch's ip-1/ip probes ARE the neighbor LCPs). Only an
ip landing AT a window edge falls back to the robust gallop.

Wide windows (uncertain model / high-occ repeats) frequently put the boundary
OUTSIDE the window, where a full window bsearch is wasted work vs find_boundary's
2-probe edge check; a WINDOW_FAST_PATH_CAP (64) sends those straight to the
gallop so the fast path is taken only for tight, confident windows. The cap only
selects which path computes the identical boundary, so the result is byte-
identical for any window. Applied to the model-window locate (seed_win=None)
only; hinted/seeded callers (warm-start) keep the gallop.

Measured (probe_audit, synthetic 4M-bp ref, mean SA probes/call):
  forward model_launch / unique          13.9 -> 10.0  (-28%)
  forward model_launch / repeat (high-occ)42.0 -> 42.0  (neutral, cap)
  est_hint_interval / unique (untouched)   8.0 ->  8.0
Byte-identical: prmi lib + integration suites pass (the lone release-mode
should_panic failure is the pre-existing debug_assert artifact, passes in debug).
@nh13
nh13 force-pushed the perf/windowed-locate-lcp branch from e8fd6f4 to 180d443 Compare June 23, 2026 20:04
@nh13

nh13 commented Jun 23, 2026

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@nh13

nh13 commented Jun 23, 2026

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@nh13
nh13 merged commit 49b514a into main Jun 24, 2026
4 checks passed
@nh13
nh13 deleted the perf/windowed-locate-lcp branch June 24, 2026 00:16
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