Repository navigation
[FEATURE] ThermoRawFile: read-ahead workers also read peaks for an MS-level-only selection - #10925
Conversation
|
No actionable comments were generated in the recent review. Walkthrough
Change: Refactor Merge Risk:
|
timosachsenberg
left a comment
There was a problem hiding this comment.
Independently reviewed the four-file diff at 7bebaff, the transform() selection/call path, ThermoRawScanReader::header() / convert() fallback and the bridge's RawFile::read_scan() / SpectrumSink reuse semantics. The per-scan has_peaks handling is consistent with the selection, including scan-range and chunk boundaries; the bridge resets reused output on the second read.
Own execution: compiled and ran an isolated C++20, multithreaded mock of the changed ReadAhead::run()/take() logic (g++, -std=c++20 -O2 -pthread). PASS: 2,866 ordered scan checks across five scenarios (MS1/MS2 selection, prefetch threshold, unfiltered behavior, multiple chunks and non-1 first scan), plus an injected bridge exception propagated at the expected chunk. This validates the isolated scheduling/selection logic only, not the real OpenMS/Thermo bridge integration. I could not run ThermoRawFile_test here: no OpenMS checkout, vendor RAW test fixture or bridge/runtime installed, and GitHub DNS/network access is unavailable in the execution environment.
No substantiated regression found in the changed path.
timosachsenberg
left a comment
There was a problem hiding this comment.
AI-generated initial review
This is an automated, AI-generated first review. It looks only for high- and medium-priority issues (no nits) and checks whether the PR has tests and measurements. Please double-check anything before acting on it; a maintainer will follow up. I reviewed 7bebaff0 against develop (601776c8).
CodeRabbit left only its summary. The earlier independent review tested the scheduling logic in an isolated mock. I built OpenMS with the Thermo bridge and ran the real code.
What I ran:
- Build: this PR merged into current
develop(d3221c79), from source. Release, Linux, GCC 13.3, 4 cores,OPENMS_USE_VCPKG=OFFwith system packages (Arrow 24.0.0 and libxml2 2.15.4 built from source). Thermo RAW was on (openms-thermo-bridge v1.0.0, .NET 8 SDK) withENABLE_THERMO_RAW_TESTS=ON. #10926 and #10927 were in the same tree for their own reviews; they touch other code. - Tests:
ThermoRawFile_test,OnDiscThermoRawExperiment_testandThermoRawFileMetadata_testpass, including the extra cases listed under "Tests". - Mutation check: I dropped the workers' peak read for the selected levels (
ThermoRawScanReader.cpp:683), keeping thehas_peaksflag set.ThermoRawFile_testthen fails. So the new checks really go through the new path, and not only through the consumer's fallback read. - Timing on real data: see "Measurements". Output was identical between
developand the PR, and between 1 and 4 threads.
Verdict:
- High priority: none.
- Medium priority: none.
Checked and fine
- Correctness doesn't depend on the workers' selection: every scan carries its own
has_peaksflag from the worker toheader(), andconvert()reads the peaks itself when the flag is false.- If the workers judge a scan differently from
ThermoRawScanSelection::selected(), the only cost is a missed prefetch. One example: a scan whosems_level()is ≤ 0, whichheader()maps to 0. - Scans the workers read with peaks but that aren't selected only cost extra work.
- If the workers judge a scan differently from
level_only: it excludes RT and precursor filters andmetadata_only.first_scanandlast_scanare covered bypeaks_fromand the read-ahead'slast.load()is unchanged: so the test compares the newtransform()path with the old one.- Memory: with profile data, the workers now keep decoded peaks in the read-ahead buffer for level-filtered transforms too. On the file below, peak RSS for an MS1-profile transform rose from 510–565 MB to 714–888 MB. That is within what an unfiltered profile transform already uses with the same buffer (733–834 MB on the same file, unchanged code). So the existing bound holds; it's just reached now with a level filter too.
For information (not a defect of this PR)
- CI doesn't run these tests:
- The new checks are in
ThermoRawFile_testinside#ifdef THERMO_RAW_TEST_DATA, and the CI presets setENABLE_THERMO_RAW_TESTS=OFF(CMakePresets.json:110). - So the green
build-and-testjobs compile this part of the test but don't run it. Your local run and mine are the only runs so far. - Turning the test data on in one Linux job would cover this file and the other Thermo tests in CI. It's a 9.7 MB download from archive.openms.de.
- The new checks are in
- Possible follow-up (one read instead of two, and RT/precursor filters too):
transform()already visits every header inscanHeaders()(to count the selected scans) before it callsreadAhead().- Recording
select.selected(info)there, e.g. as astd::vector<char>over the scan range, and handing that toreadAhead()would let the workers read each selected scan in one call, without the header-only read first. - It would also work for any header-based filter, not only MS levels.
Tests
-
You added:
transform()equalsload()for MS1 and MS2 selections, with 1 and 4 OpenMP threads, centroided and profile. -
Added locally, all pass; you may want to take them over. At 4 threads, inside your loop over
{ms2, ms1}, I added:- a level selection with a scan range (
first_scan 51,last_scan 451):peaks_fromis then above the first scan, and the read-ahead ends before the last scan; - a level selection with
charge_dataandnoise_data.
After the loop I added
ms_levels {1, 2}, and{3}, a level that doesn't occur in the file.Code for
ThermoRawFile_test.cpp: inserted after l. 656 (testTransform(profile);); the first}is the existing end of your loop// level selection combined with a scan range (peaks_from = first_scan > first scan of the file, // read-ahead ends before the last scan) and with the charge and noise arrays ThermoRawFile::Options range = levels; range.first_scan = 51; range.last_scan = 451; testTransform(range); ThermoRawFile::Options arrays = levels; arrays.charge_data = true; arrays.noise_data = true; testTransform(arrays); } // several levels at once, and a level that does not occur { ThermoRawFile::Options both = ms1; both.ms_levels = {1, 2}; testTransform(both); ThermoRawFile::Options absent = ms1; absent.ms_levels = {3}; testTransform(absent); }
- a level selection with a scan range (
Measurements
-
You provided: a downstream workflow on a 3.1 GB Astral file (MS2 only, 8 threads): 8:06 → 7:18 wall time, byte-identical outputs. That's real data, good.
-
Added here:
ThermoRawFile::transform()into a consumer that only counts. That isolates the reader from the rest of a workflow.- File: PXD000001
TMT_Erwinia_1uLSike_Top10HCD_isol2_45stepped_60min_01.raw(LTQ Orbitrap Velos, 210 MB, 7,534 scans: 1,431 MS1 and 6,103 MS2). - Method: same build with
develop's and the PR's Thermo sources swapped, 3 interleaved runs each, file in the page cache. The table shows the median, with the range in brackets. Times include opening the file and the header pass.
selection threads developPR change MS2, centroided 4 1.15 s (1.10–1.20) 0.85 s (0.84–1.07) −26 % MS1, centroided 4 0.89 s (0.89–0.95) 0.75 s (0.70–0.88) −16 % MS1, profile 4 2.57 s (2.51–2.74) 1.82 s (1.71–2.14) −29 % all levels, centroided (path unchanged) 4 0.87 s (0.83–0.95) 0.86 s (0.86–0.86) – MS2, centroided (no read-ahead) 1 1.23 s (1.23–1.34) 1.20 s (1.16–1.31) – - Same output: spectrum and peak counts, and a checksum over all m/z and intensity values, are identical between
developand the PR, and between 1 and 4 threads. - Consistent with your number: this confirms the gain on a second instrument type and also for MS1-only and profile selections.
- File: PXD000001
Description
- The PR has no AI-use section. The repository's PR template asks for one (and for the AI-policy confirmation), so please add it, even if the answer is "None".
Generated by Claude Code
timosachsenberg
left a comment
There was a problem hiding this comment.
AI-generated review: lower-priority findings
This is an automated, AI-generated second pass, at a lower threshold than the earlier review on this PR: it lists optional improvements and nits as well. Nothing here changes the spectra and nothing blocks the merge. It is a static read of 7bebaff0 and of openms-thermo-bridge v1.0.0; nothing was built or run for it. Please double-check before acting on it.
Some points extend what the earlier review already noted (missed prefetch for an MS order of 0 or below, load() unchanged, one read instead of two, more test cases); the inline comments say where.
One point is not on a changed line:
- MS2-only selections still decode the parent MS1 scans on the consumer thread. The precursor intensity estimate goes through
parentSpectrum()->raw->spectrum_data()(ThermoRawScanReader.cpp:755), because level 1 is not read ahead and unselected scans never enter theParentCacheoftransform(). This is not introduced by the PR, but it limits the gain forms_levels = {2}on DDA data. A possible follow-up: the workers also read the peaks of parent-level scans and hand them to the parent lookup.
Generated by Claude Code
| accessor.read_scan(scan, out, header_options); | ||
| if (wantsPeaks(scan, out.header.ms_level())) | ||
| { | ||
| accessor.read_scan(scan, out, full_options); |
There was a problem hiding this comment.
Selected scans are read twice, both times with the trailer text (efficiency)
header_options and full_options both carry trailer_text = true, which the bridge documents as slow. On this path every selected scan therefore costs scan stats, scan event, filter string and GetTrailerExtraInformation twice. Before the PR the second read was the peakReadOptions() read of convert(), which has no trailer text, so the vendor work per selected scan goes up while it moves to the workers.
A small fix: read the peaks with peakReadOptions() into a per-worker scratch tb::Spectrum and swap its arrays into out. convertScan() only uses the arrays of data, and the consumer then gets exactly what convert() read before the PR. The selection bitmap from scanHeaders() that the earlier review suggested gets it down to one read, but only for transform().
Asking accessor.ms_level(scan) before the read is not an option: RawFile::ms_level() returns the raw MS order, and check_int_result() takes -1/-2/-3 for error codes, so it throws for precursor-ion, neutral-loss and neutral-gain scans.
Generated by Claude Code
There was a problem hiding this comment.
Fixed in abc2eb2. The worker reads the peaks of a selected scan with peakReadOptions() (no trailer text, the same read convert() did before) into a scratch tb::Spectrum and swaps the six arrays in. The header read keeps the trailer text, which header() needs. On the 3.1 GB Astral file (MS2, 4 threads) this took the median from 19.0 s to 16.7 s (develop 25.4 s); the table is in the description. The one-read version via a mask from scanHeaders() is listed as a follow-up.
| } | ||
| // Level-selected peaks: the header decides; the peaks of a selected scan are read in a second call. | ||
| accessor.read_scan(scan, out, header_options); | ||
| if (wantsPeaks(scan, out.header.ms_level())) |
There was a problem hiding this comment.
wantsPeaks() gets the raw MS order, the selection uses the clamped level (low)
header.ms_level() is -1/-2/-3 for precursor-ion, neutral-loss and neutral-gain scans, and header() maps every value of 0 or below to ScanInfo::ms_level == 0. With ms_levels = {0} on a triple-quad file those scans are selected, but the workers never read their peaks. As the earlier review says, this only costs a missed prefetch and the output is correct. A negative entry in ms_levels has the opposite effect: peaks are decoded and never used.
| if (wantsPeaks(scan, out.header.ms_level())) | |
| if (wantsPeaks(scan, std::max(out.header.ms_level(), 0))) |
Generated by Claude Code
There was a problem hiding this comment.
Done: the worker now uses std::max(spectrum.header.ms_level(), 0), the same clamp as header().
| bool complete = last == reader.lastScan(); // all headers read, so the TIC can be built from them | ||
| const bool prefetch_peaks = ! header_filtered && ! options_.metadata_only; | ||
| reader.readAhead(last, prefetch_peaks ? options_.first_scan : std::numeric_limits<int>::max()); | ||
| // Selected by MS level only: the read-ahead workers also read the peaks of scans at those levels. |
There was a problem hiding this comment.
load() keeps the old behaviour (scope)
Only transform() gets the level prefetch. load() (lines 89-91), and with it FileHandler::loadExperiment() with an MS-level filter, still passes INT_MAX for any header filter, so its calling thread decodes every selected scan as before.
If that is intended for now, fine. Otherwise the decision could live in one helper on ThermoRawScanSelection that both loops use. The new test relies on load() taking the old path (it compares transform() with load()), so it would then need a single-threaded reference instead.
Generated by Claude Code
There was a problem hiding this comment.
Intended for this PR: load() keeps the consumer-thread read, and the tests use it as the reference for transform(). I listed the shared helper for load() (with a single-threaded reference in the tests) as a follow-up in the description.
| const bool level_only = header_filtered && ! options_.ms_levels.empty() && ! select.precursorFiltered() | ||
| && options_.rt_min == -std::numeric_limits<double>::infinity() | ||
| && options_.rt_max == std::numeric_limits<double>::infinity() && ! options_.metadata_only; |
There was a problem hiding this comment.
A level filter with an RT range falls back to the old path (scope)
level_only is false as soon as rt_min or rt_max is set, although the worker has the retention time in the same header as the MS level. ms_levels = {2} with an RT window is a common setup and gets no prefetch.
Passing the selection instead of a level list, and checking select.level() and select.rt() in the worker, would cover it. The bitmap follow-up from the earlier review would cover precursor m/z filters too.
Generated by Claude Code
There was a problem hiding this comment.
Agreed this is the common case. I kept it out of this PR so the change stays small; the fallback is now covered by a test (level selection with an RT range at 4 threads). The per-scan selection mask from scanHeaders(), which covers RT and precursor filters and gives one read per scan, is listed as a follow-up.
| // which must give the single-threaded result (where the consumer thread reads every scan itself) | ||
| { | ||
| const int threads = omp_get_max_threads(); | ||
| for (const ThermoRawFile::Options& levels : {ms2, ms1}) |
There was a problem hiding this comment.
Test coverage
- Not exercised: a level filter together with
first_scan/last_scan(thescan < peaks_frombranch with a non-empty level list),charge_data/noise_datathrough the two-step read, a list of several levels, and a level filter with an RT range (the fallback). The earlier review has code for the first three. - Profile mode runs with 4 threads only.
- The mutation check in the earlier review shows that these checks go through the peaks the workers read. The opposite regression is not caught: if
level_onlywere never true, orwantsPeaks()always false, every check still passes, because the fallback gives the same spectra. I see no way to test that through the public API; noting it only.
Generated by Claude Code
There was a problem hiding this comment.
Added in abc2eb2: level selection with first_scan 51 / last_scan 451, with charge_data and noise_data, with an RT range (fallback), ms_levels {1, 2} and {3}; profile now runs at 1 and 4 threads. I repeated your mutation check: without the worker peak read (flag still set) ThermoRawFile_test fails. Agreed that the opposite regression (prefetch never used) is not visible through the public API; the timing in the description covers it for now.
| if (level_only) { reader.readAhead(last, options_.first_scan, options_.ms_levels); } | ||
| else { reader.readAhead(last, prefetch_peaks ? options_.first_scan : std::numeric_limits<int>::max()); } |
There was a problem hiding this comment.
The two readAhead() calls are one call (simplification)
readAhead(last, first_scan, {}) is the unfiltered call, and ms_levels is empty whenever prefetch_peaks is true, so one call does the same:
| if (level_only) { reader.readAhead(last, options_.first_scan, options_.ms_levels); } | |
| else { reader.readAhead(last, prefetch_peaks ? options_.first_scan : std::numeric_limits<int>::max()); } | |
| reader.readAhead(last, (prefetch_peaks || level_only) ? options_.first_scan : std::numeric_limits<int>::max(), options_.ms_levels); |
Two smaller things in level_only above: header_filtered && is implied by ! options_.ms_levels.empty(), and the RT part repeats headerFiltered() with a different comparison (== instead of > / <). An rtFiltered() helper on ThermoRawScanSelection, used by headerFiltered() as well, would keep the two from drifting apart.
Generated by Claude Code
There was a problem hiding this comment.
Done: one readAhead() call, level_only no longer repeats header_filtered, and a new ThermoRawScanSelection::rtFiltered() is used by both headerFiltered() and transform().
| if (peak_levels.empty() || scan < peaks_from) | ||
| { | ||
| with_peaks[scan - begin] = scan >= peaks_from; | ||
| accessor.read_scan(scan, out, with_peaks[scan - begin] ? full_options : header_options); | ||
| continue; | ||
| } |
There was a problem hiding this comment.
Two read paths for one decision (nit)
The loop sets with_peaks in two places and has three read_scan() call sites, and wantsPeaks() tests scan >= peaks_from and peak_levels.empty() again, which the calling branch has already decided.
One flow reads easier: bool peaks = scan >= peaks_from;, then, for a non-empty level list, read the header and set peaks from the level, and store the flag in one place.
Generated by Claude Code
There was a problem hiding this comment.
Done: one flow. with_peaks starts as scan >= peaks_from, a level list may turn it off after the header read, and the flag is stored in one place. wantsPeaks() is gone.
| profile.centroid = false; | ||
| testTransform(profile); | ||
| } | ||
| omp_set_num_threads(threads); |
There was a problem hiding this comment.
Thread count is not restored if a check throws; the comment promises more than the code does (nit)
- If
transform()orload()throws,START_SECTIONcatches it and the rest of the test runs with 4 (or 1) threads. A small RAII guard avoids that. The existing read-ahead section has the same pattern. - Each
testTransform()comparestransform()withload()at the same thread count. The 4-thread result is never compared with a single-threaded reference, as the comment says. That works as long asload()takes the old path (see the comment onload()).
Generated by Claude Code
There was a problem hiding this comment.
Done: a small RAII guard restores the OpenMP thread count, and the comment now says that each transform() is compared with load(), which keeps the consumer-thread read.
| @param[in] peak_levels If not empty, only scans of these MS levels (from @p peaks_from on) are read with peaks; | ||
| the header is read first to learn the level. header() reports which scans carry peaks. |
There was a problem hiding this comment.
header() does not report this (doc)
header() returns a ScanInfo without such a field. The flag is internal and only convert() uses it.
| @param[in] peak_levels If not empty, only scans of these MS levels (from @p peaks_from on) are read with peaks; | |
| the header is read first to learn the level. header() reports which scans carry peaks. | |
| @param[in] peak_levels If not empty, only scans of these MS levels (from @p peaks_from on) are read with peaks; | |
| the header is read first to learn the level. convert() reads the peaks of the other scans itself. |
Generated by Claude Code
| std::mutex mutex; | ||
| std::condition_variable produced, consumed; | ||
| std::map<int, std::pair<std::vector<tb::Spectrum>, std::exception_ptr>> ready; ///< chunk -> scans | ||
| std::map<int, std::tuple<std::vector<tb::Spectrum>, std::vector<char>, std::exception_ptr>> ready; ///< chunk -> scans, peaks read |
There was a problem hiding this comment.
<tuple> is not included (nit)
std::tuple, std::make_tuple and std::get<N> now only come in through <map>, <mutex> and <functional>. #include <tuple> is enough. A small struct with named members (scans, peaks, error) would also read better than std::get<0..2>.
Generated by Claude Code
There was a problem hiding this comment.
Replaced the tuple with a named Chunk struct (scans, has_peaks, error), so <tuple> is no longer needed.
…-level-only selection
Before, any header filter (also ms_levels alone) turned peak prefetch off in
ThermoRawScanReader::readAhead, so the consumer thread of transform() decoded
every selected scan itself. readAhead() now takes the levels whose peaks the
workers read after the header, and take() reports per scan whether peaks came
along. Spectra are unchanged.
Test: ThermoRawFile_test checks transform() == load() for ms_levels {1} and {2}
with 1 and 4 OpenMP threads, centroided and profile.
Co-authored-by: Claude <[email protected]>
- Workers read the peaks of a level-selected scan without the trailer text (peakReadOptions, as convert()
reads them) into a scratch spectrum and swap the arrays in; previously the second read repeated the slow
trailer text.
- The worker clamps the MS order like header() (levels <= 0 map to 0), so ms_levels {0} prefetches too.
- One readAhead() call in transform(); rtFiltered() helper shared with headerFiltered(); named Chunk struct
instead of a tuple; readAhead() doc says convert() reads the other peaks.
- Tests: level selection with a scan range, with charge/noise arrays, with an RT range (fallback), levels
{1,2} and an absent level {3}; profile at 1 and 4 threads; OpenMP thread count restored by a guard.
Mutation check: dropping the worker peak read makes ThermoRawFile_test fail.
Co-authored-by: Claude <[email protected]>
7bebaff to
abc2eb2
Compare
|
Thanks for the thorough reviews and the measurements. I pushed abc2eb2 (branch rebased onto current develop; the first commit only gains its
Kept out as follow-ups (listed in the description): the selection mask from |
Patch for the follow-up: prefetch in
|
timosachsenberg
left a comment
There was a problem hiding this comment.
Approving abc2eb24. This approval is AI-generated and posted at the maintainer's request, on the condition that CI passes.
- First commit: the rebased
e8e64d74has the same patch as7bebaff0, which was reviewed before. - Second commit: I read its diff in full. It resolves the review comments: the peak read without trailer text, the level clamp as in
header(), onereadAhead()call withrtFiltered(), the namedChunkstruct, the doc sentence, the thread-count guard and the added test cases. I found no new issue in it. - CI: all build-and-test, wheel and header-set jobs pass on
abc2eb24. - Thermo tests: CI does not run them (
ENABLE_THERMO_RAW_TESTS=OFF), soThermoRawFile_testrests on your local run and the mutation check you report. Nothing was built or run for this approval. - Follow-ups: prefetch in
load(), the selection mask (RT and precursor filters) and parent-MS1 prefetch stay out of this PR, as listed in the description. A patch for the first two is in the comment above.
Generated by Claude Code
Description
Problem
ThermoRawFile::transformwith a scan selection by MS level only (Options::ms_levels, no RT or precursor m/z filter) switched peak prefetch off inThermoRawScanReader::readAhead. The read-ahead workers then only read headers, and the consumer thread decoded the peaks of every selected scan itself.Change
readAhead()takes the MS levels whose peaks should be prefetched. A worker reads the header first. For a scan at a selected level it then reads the peaks without trailer text (peakReadOptions(), the same readconvert()does) and swaps them into the scan.has_peaksflag.convert()reads the peaks itself whenever the workers did not, so output does not depend on the workers' choice.header()does: levels of 0 or below map to 0.transform()uses this.load()and level filters combined with an RT or precursor range keep the previous header-only read-ahead.Output spectra are unchanged.
Measured effect
ThermoRawFile::transform()into a consumer that only counts. The test file is a 3.1 GB Astral DIA.raw(292,883 MS2 and 2,928 MS1 scans), in the page cache. Laptop with 20 cores, while other jobs were running. Each version was run 3 times, alternating; the table shows the median, with the range in brackets.developTests
ThermoRawFile_testcomparestransform()withload()(which keeps the consumer-thread read) for:ms_levels{1} and {2}, centroided and profile, at 1 and 4 OpenMP threads;first_scan51,last_scan451), with charge and noise arrays, and with an RT range (the header-only fallback);ms_levels{1, 2}, and {3}, a level that does not occur in the file.A guard restores the OpenMP thread count even if a check throws.
Mutation check: with the workers' peak read removed (flag still set),
ThermoRawFile_testfails.ctest -R Thermo(ThermoRawFile_test, OnDiscThermoRawExperiment_test, ThermoRawFileMetadata_test) passes locally (Linux, GCC 13, Release,ENABLE_THERMO_RAW_TESTS=ON). The CI presets setENABLE_THERMO_RAW_TESTS=OFF, so CI compiles these checks but does not run them.Not in this PR (possible follow-ups from review)
select.selected(info)result from thescanHeaders()pass toreadAhead()as a per-scan mask.load(). The tests here then need a single-threaded reference instead ofload().Checklist
AI use (see the OpenMS AI Policy)
Co-authored-by:trailer, and the description above explains the design intent and rationaleAn AI coding agent (Claude) wrote the change, the tests and the measurements. It came out of profiling a DIA workflow in which MS2-only streaming of Thermo files was a bottleneck. The design keeps a per-scan peak flag, and
convert()falls back to reading the peaks itself, so the workers' choice cannot change the output.Summary by CodeRabbit