Skip to content

fix(pyOpenMS): patch nanobind __forceinline on MSVC to fix compile-time blowup - #8933

Merged
timosachsenberg merged 3 commits into
developfrom
fix/nanobind-msvc-forceinline
Mar 18, 2026
Merged

timosachsenberg merged 3 commits into
developfrom
fix/nanobind-msvc-forceinline

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Mar 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Patches nanobind's __forceinline → inline in nb_defs.h after FetchContent, MSVC only
  • Fixes O(n²) optimizer blowup that causes multi-hour compile times on large binding files
  • No measurable impact on binary size or runtime performance

Background

MSVC's optimizer has pathological O(n²) complexity when processing __forceinline directives on large translation units. nanobind v2.10.0 defines NB_INLINE as __forceinline on Windows. With our largest binding files (bind_misc.cpp at 5K lines, bind_kernel.cpp at 4K lines), this causes severe compile-time blowup when any optimization flag (/Os, /O1, /O2) is enabled.

Reported improvements from the same fix in other projects: 2h28m → 3m04s (see nanobind#791).

The patch only fires when nanobind is fetched via FetchContent and only on MSVC. It is idempotent (checks for __forceinline before patching).

Related

Test plan

  • Windows CI build completes without compile-time regression
  • pyOpenMS tests pass on Windows
  • Verify CMake configure log shows: Patched nanobind nb_defs.h: __forceinline → inline (MSVC compile-time fix)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved Windows build compatibility by patching native bindings to avoid MSVC compiler issues, reducing build failures on affected toolchains.
  • Documentation

    • Added a multi-stage migration plan to remove Qt from the core library and replace it with standard C++/Boost alternatives, outlining upcoming API and build-system transitions and expected impacts for future releases.

@coderabbitai

coderabbitai Bot commented Mar 18, 2026 •

Copy link
Copy Markdown
Contributor

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

A MSVC-only patch was added to src/pyOpenMS/CMakeLists.txt that, when NB_DIR is defined and the target file exists, reads NB_DIR/include/nanobind/nb_defs.h, replaces occurrences of __forceinline with inline, writes the file back, and emits a CMake status message.

Changes

Cohort / File(s) Summary
MSVC Nanobind Compatibility Patch
src/pyOpenMS/CMakeLists.txt
Adds an MSVC-guarded CMake snippet that checks NB_DIR, locates NB_DIR/include/nanobind/nb_defs.h, replaces __forceinline with inline, writes the modified file, and emits a status message. The snippet is inserted in two guarded locations.

Suggested reviewers

  • pjones
  • jpfeuffer

Poem

🐰
I hop through CMake, neat and quick,
Swapping forceinlines, one small trick.
For MSVC I mend the line,
A tiny patch — the build feels fine.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: patching nanobind's __forceinline on MSVC to fix compile-time issues.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/nanobind-msvc-forceinline
📝 Coding Plan
  • Generate coding plan for human review comments

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.

Tip

CodeRabbit can scan for known vulnerabilities in your dependencies using OSV Scanner.

OSV Scanner will automatically detect and report security vulnerabilities in your project's dependencies. No additional configuration is required.

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (1)
src/pyOpenMS/CMakeLists.txt (1)

154-165: Narrow the patch guard to FetchContent-only to avoid accidental writes to pre-installed nanobind.

Using nanobind_SOURCE_DIR alone is insufficient because this variable may be defined regardless of whether nanobind was fetched or found via find_package. When nanobind is installed (find_package path), the patch should not run, as it targets a development/build-time compilation fix specific to locally-fetched copies. Guard with nanobind_POPULATED to ensure the in-place file(WRITE ...) only runs after FetchContent_MakeAvailable(nanobind).

♻️ Proposed guard refinement
-  if(MSVC AND nanobind_SOURCE_DIR)
+  if(MSVC AND nanobind_POPULATED AND nanobind_SOURCE_DIR)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/pyOpenMS/CMakeLists.txt` around lines 154 - 165, The current patch uses
nanobind_SOURCE_DIR to decide whether to modify nb_defs.h, which can be set even
for an installed nanobind; change the guard to ensure this only runs for
FetchContent-provided nanobind by testing nanobind_POPULATED (set by
FetchContent_MakeAvailable) instead of or in addition to nanobind_SOURCE_DIR;
i.e., wrap the existing logic that reads/writes
"${nanobind_SOURCE_DIR}/include/nanobind/nb_defs.h" with if(nanobind_POPULATED)
so the file(WRITE ...) and patch message only execute when nanobind was
populated via FetchContent.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/pyOpenMS/CMakeLists.txt`:
- Around line 154-165: The current patch uses nanobind_SOURCE_DIR to decide
whether to modify nb_defs.h, which can be set even for an installed nanobind;
change the guard to ensure this only runs for FetchContent-provided nanobind by
testing nanobind_POPULATED (set by FetchContent_MakeAvailable) instead of or in
addition to nanobind_SOURCE_DIR; i.e., wrap the existing logic that reads/writes
"${nanobind_SOURCE_DIR}/include/nanobind/nb_defs.h" with if(nanobind_POPULATED)
so the file(WRITE ...) and patch message only execute when nanobind was
populated via FetchContent.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 59f12625-4694-4488-9aef-2585ebdde1b9

📥 Commits

Reviewing files that changed from the base of the PR and between d12d512 and 79b93cb.

📒 Files selected for processing (1)
  • src/pyOpenMS/CMakeLists.txt

timosachsenberg and others added 3 commits March 18, 2026 13:21
…ompile-time blowup

MSVC's optimizer exhibits O(n²) complexity when processing __forceinline
directives on large translation units. nanobind defines NB_INLINE as
__forceinline on Windows, causing compilation of large binding files
(e.g. bind_misc.cpp at 5K lines) to take hours instead of minutes.

Patch nb_defs.h after FetchContent to replace __forceinline with plain
inline. This has no measurable impact on binary size or runtime
performance (confirmed in nanobind discussions/791).

Refs: wjakob/nanobind#791
Refs: #8932

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
nanobind_SOURCE_DIR is only set when nanobind is fetched via FetchContent.
In the cibuildwheel Windows build, nanobind is installed via pip (build
requirement in pyproject.toml), so find_package(nanobind) succeeds and
FetchContent is never invoked — making the patch silently no-op.

NB_DIR is set by nanobind-config.cmake regardless of install method
(pip or FetchContent), so the patch now fires in all cases.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
7 incremental PRs with exact file paths, code snippets, and test commands.
Addresses all review feedback:
- boost::process is header-only (no cmake COMPONENTS)
- DateTime rewrite covers all methods including fromString parser
- Complete file list for PR 5a (~20 files)
- DBSuitability and InternalCalibration added to PR 4
- UpdateCheck.cpp handled entirely in PR 3
- pyOpenMS build verification in PR 6

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@timosachsenberg
timosachsenberg force-pushed the fix/nanobind-msvc-forceinline branch from 8e61d4b to ea9fd82 Compare March 18, 2026 12:21
@timosachsenberg
timosachsenberg enabled auto-merge (squash) March 18, 2026 12:22
@timosachsenberg
timosachsenberg merged commit 0bcae36 into develop Mar 18, 2026
14 checks passed
@timosachsenberg
timosachsenberg deleted the fix/nanobind-msvc-forceinline branch March 18, 2026 12:28
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