Skip to content

Fix MSVC optimize pragma re-enable after keep_memory - #3212

Merged
horenmar merged 2 commits into
catchorg:develfrom
amedlin:fix/msvc-benchmark-optimize-pragma
Oct 6, 2026
Merged

horenmar merged 2 commits into
catchorg:develfrom
amedlin:fix/msvc-benchmark-optimize-pragma

Conversation

@amedlin

@amedlin amedlin commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

On MSVC, catch_optimizer.hpp wraps keep_memory in #pragma optimize("", off) so the volatile self-assignment is not removed. The matching #pragma optimize("", on) was guarded by #if defined(_MSVC_VER), which is never defined (the correct macro is _MSC_VER).

Because MSVC's optimize pragma applies to subsequent functions in the translation unit, including any Catch2 benchmark header left the rest of that TU ? including user benchmark lambdas ? compiled with optimizations disabled. Timings then measured unoptimized code.

This changes the re-enable guard to _MSC_VER in both catch_optimizer.hpp and the amalgamated header.

GitHub Issues

Fixes #3211

The keep_memory helper correctly disables MSVC optimizations with
#if defined(_MSC_VER), but the matching pragma optimize on
was guarded by the undefined _MSVC_VER macro. That left optimization
disabled for the rest of any translation unit that included the
benchmark headers, so measured code was compiled unoptimized on MSVC.
@amedlin
amedlin force-pushed the fix/msvc-benchmark-optimize-pragma branch from 517c56b to 355ae22 Compare October 5, 2026 04:53
@codecov

codecov Bot commented Oct 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.18%. Comparing base (45cd398) to head (355ae22).

Additional details and impacted files
@@            Coverage Diff             @@
##            devel    #3212      +/-   ##
==========================================
+ Coverage   91.14%   91.18%   +0.03%     
==========================================
  Files         205      205              
  Lines        9100     9100              
==========================================
+ Hits         8294     8297       +3     
+ Misses        806      803       -3     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@horenmar horenmar added the BugFix label Oct 5, 2026
Comment thread extras/catch_amalgamated.hpp Resolved
Reverted change, as PR review indication that amalgamated files should only change during release.

Co-authored-by: Martin Hořeňovský <martin.horenovsky@gmail.com>
@horenmar

horenmar commented Oct 6, 2026

Copy link
Copy Markdown
Member

Thanks

@horenmar
horenmar merged commit 6adbd53 into catchorg:devel Oct 6, 2026
85 of 86 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong macro use under MSVC

2 participants