Repository navigation
Up to 40% faster mzML reading - #8074
Conversation
…or ion mobility) - 20% faster on MSVC - 40% faster on GCC
## Walkthrough
The changes remove the `addSpectrumMetaData_` method from `MzMLHandler`, refactor and optimize metadata array handling within `populateSpectraWithData_`, and introduce a compound assignment operator to `MetaInfo` for bulk metadata merging. Additionally, a new flag is added to the FileFilter tool to remove metadata arrays from spectra, and enum types in `BinaryData` are now explicitly named.
## Changes
| File(s) | Change Summary |
|------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------|
| `src/openms/include/OpenMS/FORMAT/HANDLERS/MzMLHandler.h`,<br>`src/openms/source/FORMAT/HANDLERS/MzMLHandler.cpp` | Removed `addSpectrumMetaData_` method; refactored and optimized metadata handling in `populateSpectraWithData_`. |
| `src/openms/include/OpenMS/FORMAT/HANDLERS/MzMLHandlerHelper.h` | Renamed anonymous enums in `BinaryData` struct to named enums `PRECISION` and `DATA_TYPE`. |
| `src/openms/include/OpenMS/METADATA/MetaInfo.h` | Added `operator+=` for merging `MetaInfo` instances. |
| `src/openms/source/METADATA/MetaInfoInterface.cpp` | Simplified `addMetaValues` to use the new `MetaInfo::operator+=` for bulk meta value aggregation. |
| `src/topp/FileFilter.cpp` | Added flag to remove metadata arrays from spectra; updated documentation and processing logic accordingly. |
| `src/openms/include/OpenMS/METADATA/MetaInfoInterface.h` | Enhanced documentation comment for `addMetaValues` method clarifying overwrite behavior. |
| `src/tests/class_tests/openms/source/MetaInfoInterface_test.cpp` | Added test coverage for `MetaInfoInterface::addMetaValues` verifying merge and overwrite behavior. |
| `src/tests/class_tests/openms/source/MetaInfo_test.cpp` | Added test coverage for `MetaInfo::operator+=` verifying merging and overwriting of meta values. |
| `CHANGELOG` | Updated changelog to note 20-40% speed improvement for loading mzML files with additional metadata arrays. |
## Sequence Diagram(s)
```mermaid
sequenceDiagram
participant User
participant FileFilter
participant Spectrum
User->>FileFilter: Run with --peak_options:remove_metadataarrays
FileFilter->>Spectrum: For each spectrum, remove float, int, string arrays
Spectrum-->>FileFilter: Spectra with only m/z and intensity arrays
FileFilter-->>User: Output filtered mzML fileSuggested labels
Suggested reviewers
Poem
|
There was a problem hiding this comment.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
src/openms/include/OpenMS/FORMAT/HANDLERS/MzMLHandler.h(0 hunks)src/openms/include/OpenMS/FORMAT/HANDLERS/MzMLHandlerHelper.h(1 hunks)src/openms/include/OpenMS/METADATA/MetaInfo.h(1 hunks)src/openms/source/FORMAT/HANDLERS/MzMLHandler.cpp(2 hunks)src/openms/source/METADATA/MetaInfoInterface.cpp(1 hunks)src/topp/FileFilter.cpp(3 hunks)
💤 Files with no reviewable changes (1)
- src/openms/include/OpenMS/FORMAT/HANDLERS/MzMLHandler.h
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/openms/include/OpenMS/METADATA/MetaInfo.h (1)
src/openms/source/METADATA/MetaInfoInterface.cpp (8)
rhs(46-68)rhs(46-46)rhs(71-84)rhs(71-71)rhs(94-117)rhs(94-94)rhs(119-122)rhs(119-119)
⏰ Context from checks skipped due to timeout of 90000ms (5)
- GitHub Check: build-and-test (windows-2022, cl.exe, 14.36)
- GitHub Check: build-and-test (ubuntu-22.04, clang++, 15)
- GitHub Check: build-and-test (ubuntu-22.04, g++, 11)
- GitHub Check: build-and-test (macos-14, xcode, 15.0.1)
- GitHub Check: build-and-test (macos-13, xcode, 14.2)
🔇 Additional comments (9)
src/openms/include/OpenMS/FORMAT/HANDLERS/MzMLHandlerHelper.h (1)
47-58: Excellent improvement to type safety and code clarity.Converting anonymous enums to named enums (
PRECISIONandDATA_TYPE) is a great improvement that provides:
- Better type safety when passing these values as function parameters
- Improved code readability and maintainability
- Clear scoping for enum values
- Enables better IDE support and debugging
This aligns with modern C++ best practices.
src/topp/FileFilter.cpp (3)
64-68: Well-documented feature addition.The documentation clearly explains the new filtering capabilities including the metadata array removal feature. The formatting and placement within the existing documentation structure is consistent and appropriate.
309-309: Clear and descriptive flag registration.The flag name
remove_metadataarraysis descriptive and the help text clearly explains what data is preserved vs. removed. This follows the established naming and documentation patterns.
729-738: Efficient and correct implementation.The implementation correctly:
- Checks the flag before processing
- Clears only metadata arrays (float, string, integer) while preserving m/z and intensity data
- Operates on each spectrum in the experiment
- Is placed appropriately after other filtering operations
The use of
clear()is efficient for removing all elements from the vectors.src/openms/source/FORMAT/HANDLERS/MzMLHandler.cpp (5)
325-327: Good optimization: Bulk metadata mergingThe use of
addMetaValues()for bulk metadata transfer from binary arrays to the spectrum is more efficient than individual meta value operations.
338-390: Excellent optimization: Pre-processing metadata arraysThe introduction of the
MetaArrayInfostruct and pre-identification of metadata arrays is a well-designed optimization that:
- Avoids repeated type checks during data copying
- Pre-allocates space in spectrum arrays preventing reallocation
- Efficiently maps input arrays to spectrum arrays
The code correctly handles all three data types (float, integer, string) and properly copies the
MetaInfoDescription.
392-405: Well-optimized fast path for common caseThis optimization for the most common scenario (64/32 bit precision, no metadata, no filtering) provides maximum performance by:
- Using const iterators for efficient access
- Avoiding any metadata handling overhead
- Direct peak construction without intermediate steps
Good performance-oriented design.
407-477: Efficient bulk copy implementation for metadataThe bulk copy approach when filtering is not required is well-implemented:
- Separates peak copying from metadata copying for better cache locality
- Uses
insert()with iterators for efficient bulk operations- Correctly handles bounds checking with
std::min()at line 450- Properly handles all three metadata types with appropriate precision conversions
The code maintains data integrity while maximizing performance.
479-513: Correct handling of filtered peaks with metadataThe implementation for the filtered case properly:
- Adds metadata only for peaks that pass the filter criteria
- Includes bounds checking at lines 495, 501, and 505 to prevent array access violations
- Handles precision conversions correctly for both float and integer metadata
- Maintains consistency between peak data and associated metadata
This ensures data integrity when filtering is applied.
| void MetaInfoInterface::addMetaValues(const MetaInfoInterface& from) | ||
| { | ||
| std::vector<String> keys; | ||
| from.getKeys(keys); | ||
| for (String& key : keys) | ||
| if (from.meta_ == nullptr) | ||
| { | ||
| this->setMetaValue(key, from.getMetaValue(key)); | ||
| return; // nothing to copy | ||
| } | ||
| if (meta_ == nullptr) | ||
| { | ||
| meta_ = new MetaInfo(); | ||
| } | ||
| *meta_ += *from.meta_; | ||
| } |
There was a problem hiding this comment.
💡 Verification agent
🧩 Analysis chain
Great optimization but verify duplicate key behavior.
The refactoring from per-key copying to bulk aggregation using operator+= is an excellent performance improvement. The lazy initialization and early return for null checks are also well implemented.
However, please verify that the new behavior with duplicate keys matches the previous implementation. The original code would have overwritten duplicate keys, while the new operator+= might preserve existing ones (depending on its implementation).
Run this script to verify the behavior:
🏁 Script executed:
#!/bin/bash
# Description: Check for existing tests of addMetaValues to understand expected duplicate key behavior
# Search for tests of addMetaValues method
rg -A 10 -B 5 "addMetaValues" --type cpp
# Look for any documentation about expected behavior
rg -A 5 -B 5 "duplicate.*key|key.*duplicate" --type cppLength of output: 47635
🏁 Script executed:
#!/bin/bash
# Locate the MetaInfo class definition
rg -A5 -B5 "class MetaInfo" --type h --type cpp
# Locate the operator+= implementation for MetaInfo
rg -A5 -B5 "MetaInfo::operator\\+=\\b" --type cpp
# As a fallback, search for any operator+= taking MetaInfo arguments
rg -A5 -B5 "operator\\+=.*MetaInfo" --type h --type cppLength of output: 7479
#!/bin/bash
# Display the MetaInfo class definition including index_to_value_
rg -A200 -B0 "class MetaInfo" --type h src/openms/include/OpenMS/METADATA/MetaInfo.h
# Show where index_to_value_ is declared
rg -A3 -B3 "index_to_value_" src/openms/include/OpenMS/METADATA/MetaInfo.hClarify duplicate‐key semantics in addMetaValues
The switch to bulk aggregation via
*meta_ += *from.meta_;
greatly improves performance, but MetaInfo::operator+= is defined as:
MetaInfo& operator+=(const MetaInfo& rhs)
{
index_to_value_.insert(rhs.index_to_value_.begin(), rhs.index_to_value_.end());
return *this;
}std::map::insert (or equivalent) will skip any keys already in the destination, whereas the previous per-key loop would have overwritten them. Please ensure this change is intended:
• If you want source values to override on key conflict, update operator+= to assign rather than insert, for example:
for (const auto& kv : rhs.index_to_value_)
{
index_to_value_[kv.first] = kv.second;
}• Add a unit test for addMetaValues where destination and source share keys to lock in the desired behavior.
Locations to update/tests to add:
- src/openms/include/OpenMS/METADATA/MetaInfo.h →
operator+= - src/openms/source/METADATA/MetaInfoInterface.cpp →
addMetaValues - New test file (e.g.
src/tests/MetaInfo_merge_test.cpp)
🤖 Prompt for AI Agents
In src/openms/source/METADATA/MetaInfoInterface.cpp lines 250-261, the change to
use operator+= for merging meta_ maps alters duplicate key handling because
std::map::insert skips existing keys instead of overwriting them. To fix this,
modify MetaInfo::operator+= in src/openms/include/OpenMS/METADATA/MetaInfo.h to
assign values for each key from rhs to ensure source keys overwrite destination
keys. Also, add a unit test in a new file like src/tests/MetaInfo_merge_test.cpp
to verify addMetaValues correctly overwrites duplicate keys as intended.
…to mzml_meta_read # Conflicts: # src/openms/include/OpenMS/METADATA/MetaInfo.h # src/openms/source/FORMAT/HANDLERS/MzMLHandler.cpp
| /// If an entry with the same index/name already exists, it will be overwritten. | ||
| MetaInfo& operator+=(const MetaInfo& rhs) | ||
| { | ||
| // index_to_value_.insert(rhs.index_to_value_.begin(), rhs.index_to_value_.end()); // does not overwrite existing data |
There was a problem hiding this comment.
Should be faster to get the underlying sorted vector , do merge sort, unique and create a flat map from it again.
Or just use a global function and insert the other way round.
There was a problem hiding this comment.
This is actually only a side product of this PR.
I wanted to avoid calling meta.getKeys() which is horribly inefficient, so I removed this layer.
The new code should be a lot more efficient already, although your approach is probably even faster.
For loading of mzML it does not show up as significant in perf benchmarks, so it's a bit out of scope for this PR, but might be very useful for other tools which are heavy on metadata.
|
ready for review/merge |
Description
Faster reading of mzML when additional binary data is present (e.g. for ion mobility)
- 20% faster on MSVC (318.4 MiB/s --> 399.8 MiB/s)
- 40% faster on GCC (230.7 MiB/s --> 372.2 MiB/s)
Details:
avoids checking a meta-value for every(!) data point and introduces a new case when filtering by int/mz is not required (which is usually the case), allowing to copy whole arrays instead of individual items.
Checklist
How can I get additional information on failed tests during CI
Click to expand
If your PR is failing you can check outIf you click in the column that lists the failed tests you will get detailed error messages.
Advanced commands (admins / reviewer only)
Click to expand
/reformat(experimental) applies the clang-format style changes as additional commit. Note: your branch must have a different name (e.g., yourrepo:feature/XYZ) than the receiving branch (e.g., OpenMS:develop). Otherwise, reformat fails to push.rebuild jenkinswill retrigger Jenkins-based CI buildsSummary by CodeRabbit
New Features
Documentation
Performance Improvements
Refactor
Tests