Repository navigation
Add zero-copy NumPy view for MSChromatogram.get_peaks_struct() - #8825
timosachsenberg merged 2 commits into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds Changes
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). 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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/pyOpenMS/tests/unittests/test_mschromatogram_struct_view.py`:
- Line 21: The test unpacks chrom.get_peaks() into rt_copy and int_copy but only
int_copy is used; replace the unused variable rt_copy with a throwaway name (_)
in the unpacking (i.e., change the assignment where chrom.get_peaks() is called)
so the test uses "_" and int_copy instead of rt_copy and int_copy to silence the
lint warning and make intent clear.
ℹ️ Review info
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/pyOpenMS/bindings/bind_chromatogram.cppsrc/pyOpenMS/tests/unittests/test_mschromatogram_struct_view.py
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
|
awesome |
Summary
This PR adds a zero-copy NumPy accessor for chromatogram peak data:
MSChromatogram.get_peaks_struct()
Instead of allocating and copying RT and intensity arrays (as done by get_peaks()), this method exposes a direct NumPy view over the underlying std::vector memory using nanobind.
This eliminates the O(n) copy cost when accessing large chromatograms from Python.
Motivation
For large chromatograms (e.g. SWATH / DIA data), copying peak data into separate NumPy arrays introduces measurable overhead.
The new method:
• Avoids memory allocation
• Avoids copying
• Returns immediately with a view
• Preserves lifetime safety using reference_internal
The existing get_peaks() remains unchanged for backward compatibility.
Benchmark performed on macOS (Apple Silicon), Python 3.14, Release build.
for n in [100, 10_000, 100_000, 1_000_000]:
benchmark(n)
Peaks: 100
get_peaks (copy): 0.000002 s
get_peaks_struct (zero-copy): 0.000001 s
Peaks: 10,000
get_peaks (copy): 0.000009 s
get_peaks_struct (zero-copy): 0.000001 s
Peaks: 100,000
get_peaks (copy): 0.000117 s
get_peaks_struct (zero-copy): 0.000001 s
Peaks: 1,000,000
get_peaks (copy): 0.002011 s
get_peaks_struct (zero-copy): 0.000001 s
Memory
For 1M peaks:
Copy arrays memory: 16,000,000 bytes
Zero-copy array memory: 16,000,000 bytes
Safety
Uses nb::rv_policy::reference_internal
Lifetime safety verified via dedicated test
No segfaults under forced GC
Full pyopenms_tests suite passes
100% tests passed, 0 tests failed
Backward Compatibility
No changes to existing API
get_peaks() remains unchanged
This is an additive improvement
Notes
This implementation mirrors the zero-copy approach already used for peak containers and keeps the Python binding minimal while preserving performance.
Summary by CodeRabbit
New Features
Tests