Skip to content

[FIX] DistanceMatrix::setValue keeps the cached minimum correct (#9488 DATA-24) - #9610

Merged
timosachsenberg merged 1 commit into
OpenMS:developfrom
timosachsenberg:fix/9488-distancematrix-stale-min
Jun 18, 2026
Merged

timosachsenberg merged 1 commit into
OpenMS:developfrom
timosachsenberg:fix/9488-distancematrix-stale-min

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

Part of #9488 (POLS high-severity checklist — CONCEPT / DATASTRUCTURES / KERNEL).

[DATA-24] DistanceMatrix::setValue — stale cached minimum

setValue() keeps a cached minimum coordinate (min_element_) up to date on the fast path. The guard for that fast path used &&:

if (i != min_element_.first && j != min_element_.second)   // before

so a cell that shares exactly one index with the current min coordinate (e.g. same column) skipped the fast path and fell into the else branch, which never refreshes min_element_ when the new value is smaller. Writing a new minimum into such a cell left getMinElementCoordinates() pointing at a stale (larger) cell.

Fix: one-character change to || — i.e. "take the fast path whenever (i,j) is not the min cell". Writing into any non-min cell can only introduce a new (smaller) minimum, never invalidate the existing one, so the fast path is always correct there; the else branch now fires only when (i,j) is the min cell (where a recompute may be needed).

ABI: source-compatible (inline template body only).
Test: DistanceMatrix_test — writing a smaller value into a cell sharing one index with the min now updates the cached min coordinate.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Fixed a bug in distance matrix operations where minimum value coordinates could become stale and fail to update correctly under certain conditions, potentially affecting downstream computations.
  • Tests

    • Added regression test to verify distance matrix minimum value tracking updates properly in all scenarios.

…MS#9488 DATA-24)

Part of OpenMS#9488 (POLS high-severity checklist — **CONCEPT / DATASTRUCTURES / KERNEL**).

### [DATA-24] `DistanceMatrix::setValue` — stale cached minimum
`setValue()` keeps a cached minimum coordinate (`min_element_`) up to date on the fast path. The guard for that fast path used `&&`:

```cpp
if (i != min_element_.first && j != min_element_.second)   // before
```

so a cell that shares **exactly one** index with the current min coordinate (e.g. same column) skipped the fast path and fell into the `else` branch, which never refreshes `min_element_` when the new value is *smaller*. Writing a new minimum into such a cell left `getMinElementCoordinates()` pointing at a stale (larger) cell.

**Fix:** one-character change to `||` — i.e. "take the fast path whenever `(i,j)` is **not** the min cell". Writing into any non-min cell can only introduce a new (smaller) minimum, never invalidate the existing one, so the fast path is always correct there; the `else` branch now fires only when `(i,j)` *is* the min cell (where a recompute may be needed).

**ABI:** source-compatible (inline template body only).
**Test:** `DistanceMatrix_test` — writing a smaller value into a cell sharing one index with the min now updates the cached min coordinate.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 798f860f-7360-4201-a1cd-ac421c43a0c5

📥 Commits

Reviewing files that changed from the base of the PR and between 763e907 and 55cae1f.

📒 Files selected for processing (2)
  • src/openms/include/OpenMS/DATASTRUCTURES/DistanceMatrix.h
  • src/tests/class_tests/openms/source/DistanceMatrix_test.cpp

📝 Walkthrough

Walkthrough

DistanceMatrix::setValue had a bug where the cached minimum (min_element_) could go stale when a new smaller value was written into a cell sharing exactly one index with the cached minimum. The fix changes the fast-path guard condition from && to ||, and a regression test verifies the corrected behavior.

Changes

DistanceMatrix cached-minimum fix

Layer / File(s) Summary
Fix && → || condition and regression test
src/openms/include/OpenMS/DATASTRUCTURES/DistanceMatrix.h, src/tests/class_tests/openms/source/DistanceMatrix_test.cpp
The fast-path guard in setValue is changed from && to || so the cached minimum is invalidated when either coordinate of the updated cell matches the cached minimum. Inline comments explain the stale-cache edge case. A regression test writes 0.1 into cell (2,4) (sharing one index with the current minimum), asserts the cached minimum updates to 0.1, then restores (2,4) to 2 and confirms the cached minimum returns to 1.0.

🐇 A matrix had a secret flaw,
Two indices checked with logical && raw,
One index matched but the min stayed old,
Now || fixes the bug as foretold.
The cache refreshes, the tests all cheer—
No stale minimum shall ever appear! 🎉

🚥 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 fix: correcting the cached minimum tracking in DistanceMatrix::setValue, which is the primary change in both the header and test files.
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 unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

Copy link
Copy Markdown
Contributor Author

Adversarial review result: no blocking behavioral issue found in this pass.

Required source cleanup before merge: the added implementation/test comments include tracker references. Please remove those references from source comments and keep the comments focused on the cached-minimum invariant. I did not run the tests locally.

@timosachsenberg
timosachsenberg merged commit f5f0f64 into OpenMS:develop Jun 18, 2026
41 checks passed
timosachsenberg added a commit that referenced this pull request Jun 19, 2026
Add entries for all substantive non-test PRs merged since the last
changelog update:

- #9623 ThermoRawFile full mzML metadata extraction + polarity fix
- #9602 New FileInfo library class usable from C++ and pyOpenMS
- #9521 IMAGING multi-region support (add missing PR reference)
- #9620 Deisotoper::isToleranceSupported + ProSE/SSE crash fix on low-res data
- #9610 DistanceMatrix::setValue cached-minimum guard fix
- #9497 DistanceMatrix::operator==, ClusteringGrid, File path guard fixes
- #9583 LogStreamBuf thread-safety fix
- #9514 CI Windows migration to Visual Studio 2026


Claude-Session: https://claude.ai/code/session_01YLg6pWyjyTxpWAU6mgrDzC

Co-authored-by: Claude <[email protected]>
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