Skip to content

[PERF] MetaInfoRegistry: share the lock between lookups - #10594

Merged
timosachsenberg merged 4 commits into
developfrom
perf/metainforegistry-shared-lock
Oct 16, 2026
Merged

timosachsenberg merged 4 commits into
developfrom
perf/metainforegistry-shared-lock

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Description

Every access to a meta value by name (MetaInfoInterface::setMetaValue, getMetaValue, metaValueExists, ...) looks the name up in the global MetaInfoRegistry. Every registry method ran inside one OpenMP critical section, so threads that only read the registry excluded each other.

This PR replaces the critical section with a std::shared_mutex:

  • Lookups take the lock shared. This includes registerName() for a name that is already registered, which is the common case.
  • Only registering a new name or changing a description or unit takes the lock exclusively. registerName() checks under the shared lock first, then takes the exclusive lock and checks again before inserting, because another thread may have registered the name in between.
  • Exceptions for unregistered names and indices were thrown from inside the critical section, which OpenMP does not allow. They are now thrown after the lock guard has released the lock.
  • The lock is a file-local object in MetaInfoRegistry.cpp that guards all registries, as the named critical section did. MetaInfoRegistry.h is unchanged, so the files that include it do not get <shared_mutex>, and the class layout stays the same. The lock is created on first use and never destroyed, so registries stay usable while objects with static storage duration are destroyed.

The public API is unchanged, and the pyOpenMS bindings need no update.

Measured with OpenDIA on an Orbitrap Astral DIA run (600k precursors, 32 threads, no chromatogram output). 30% of the thread samples in extraction and scoring were waiting for this critical section, mostly under MRMFeatureFinderScoring::addScoreMetaValues_. With the shared lock:

  • CPU time of that stage: 39 min instead of 82 min
  • Wall time of that stage: 3:11 min instead of 3:52 min
  • Results are unchanged.

This change is also part of #10559, the OpenDIA runtime and memory PR, with the same MetaInfoRegistry code. It is proposed separately here because it affects every multithreaded tool that sets or reads meta values by name, not only OpenDIA. A follow-up in #10559 that made lookups lock-free with thread-local copies of the name table has been reverted there: it saved about 5 s per run but added considerable complexity to this central class. Both PRs add the same CHANGELOG entry at the same place, so whichever merges second needs a trivial CHANGELOG conflict resolution.

Checklist

  • Make sure that you are listed in the AUTHORS file
  • Add relevant changes and new features to the CHANGELOG file
  • I have commented my code, particularly in hard-to-understand areas
  • New and existing unit tests pass locally with my changes. The full ctest suite was run on [FIX] rescoring in OpenDIA: 2-3x faster, half the peak memory #10559 with the first version of this change, where the lock was a member. For this branch, only a compile check (GCC, Clang) and a 16-thread ThreadSanitizer stress test of the registry were run: concurrent registration and lookup, copying and assigning registries meanwhile, unknown names, and the exception path. CI runs the full suite.
  • Updated or added python bindings for changed or new classes (Tick if no updates were necessary.)

🤖 Generated with Claude Code

https://claude.ai/code/session_01Sr8hFGChVGwX29y7mDXezS

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability when metadata is accessed or updated concurrently. Existing lookup results and errors for unregistered values remain unchanged.

timosachsenberg and others added 2 commits October 16, 2026 05:44
Every access to a meta value by name (MetaInfoInterface::setMetaValue,
getMetaValue, metaValueExists, ...) looks the name up in the global
MetaInfoRegistry, and every registry method ran inside one OpenMP critical
section. Threads that only read the registry therefore excluded each other.

Use a std::shared_mutex instead: lookups (including registerName() for a
name that is registered already, the common case) take it shared, and only
registering a new name or changing a description/unit takes it exclusively.
Exceptions for unregistered names and indices are now thrown with the lock
released by its guard instead of from inside a critical section.

Measured with OpenDIA on an Orbitrap Astral DIA run (600k precursors,
32 threads, no chromatogram output): 30% of the thread samples in the
extraction/scoring stage were waiting for this critical section, mostly
below MRMFeatureFinderScoring::addScoreMetaValues_. With the shared lock
the CPU time of that stage drops from 82 min to 39 min and its wall time
from 3:52 min to 3:11 min; results are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@cbielow cbielow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

in general, a good thing. Just wondering if

  1. is there a performance increase in real world scenarios?
  2. do we pay with compile time?

MetaInfoRegistry.h is included by nearly every OpenMS file (through
MetaInfo.h), so do not make it include <shared_mutex> or change the
class layout: the reader-writer lock is now a file-local object in
MetaInfoRegistry.cpp that guards all registries, as the named OpenMP
critical section did before. Only MetaInfo's global registry is used
in practice, so lookups are as concurrent as with a per-registry lock,
and assignment needs only one lock.

The lock is created on first use and never destroyed, so registries
stay usable while objects with static storage duration are destroyed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Sr8hFGChVGwX29y7mDXezS
@cbielow

cbielow commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

nice speedup!
good to merge IMHO

@OpenMS OpenMS deleted a comment from coderabbitai Bot Oct 16, 2026
@timosachsenberg

Copy link
Copy Markdown
Contributor Author

thanks for the review will merge once ci passes

@timosachsenberg
timosachsenberg merged commit 8ca81a0 into develop Oct 16, 2026
32 checks passed
@timosachsenberg
timosachsenberg deleted the perf/metainforegistry-shared-lock branch October 16, 2026 09:22
timosachsenberg pushed a commit that referenced this pull request Oct 16, 2026
#10595, which this branch was stacked on, was squash-merged into develop,
so the files both changed conflicted. develop has exactly #10595's final
content plus #10594 (MetaInfoRegistry); the resolution keeps this branch's
versions and adds #10594's CHANGELOG entry. The diff against develop is
the one commit of this PR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012GBvJZN8neGpv9RoUshwFL
timosachsenberg pushed a commit that referenced this pull request Oct 16, 2026
CHANGELOG conflict with #10594: both entries are kept, with this branch's
wording of the Percolator PEP fix (ProSE's -rescore flag is removed here).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012GBvJZN8neGpv9RoUshwFL
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.

3 participants