Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/catch2/internal/catch_message_info.cpp
diff options
context:
space:
mode:
authorMartin Hořeňovský2025-12-01 22:23:51 +0100
committerMartin Hořeňovský2025-12-02 14:19:21 +0100
commitf80956a43a3e276686ed59531b41617282d75a0c (patch)
treee8fabe9e4c417962eb6aaae71397f9b306b5dad2 /src/catch2/internal/catch_message_info.cpp
parent32eac2d1bb63d6b58a32e341e22b9438f1bd041a (diff)
downloadcatch3-f80956a43a3e276686ed59531b41617282d75a0c.tar.gz
Use magic statics for non-trivial thread-local globals
This avoids calling the global's constructor on threads that will never interact with them. Calling the constructor can have surprising overhead, as e.g. MSVC's Debug mode `std::vector` will allocate in the default constructor. Closes #3050
Diffstat (limited to 'src/catch2/internal/catch_message_info.cpp')
-rw-r--r--src/catch2/internal/catch_message_info.cpp14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/catch2/internal/catch_message_info.cpp b/src/catch2/internal/catch_message_info.cpp
index 2352f36c..9f8690a4 100644
--- a/src/catch2/internal/catch_message_info.cpp
+++ b/src/catch2/internal/catch_message_info.cpp
@@ -7,20 +7,24 @@
// SPDX-License-Identifier: BSL-1.0
#include <catch2/internal/catch_message_info.hpp>
+#include <catch2/internal/catch_thread_local.hpp>
namespace Catch {
+ namespace {
+ // Messages are owned by their individual threads, so the counter should
+ // be thread-local as well. Alternative consideration: atomic counter,
+ // so threads don't share IDs and things are easier to debug.
+ static CATCH_INTERNAL_THREAD_LOCAL unsigned int messageIDCounter = 0;
+ }
+
MessageInfo::MessageInfo( StringRef _macroName,
SourceLineInfo const& _lineInfo,
ResultWas::OfType _type )
: macroName( _macroName ),
lineInfo( _lineInfo ),
type( _type ),
- sequence( ++globalCount )
+ sequence( ++messageIDCounter )
{}
- // Messages are owned by their individual threads, so the counter should be thread-local as well.
- // Alternative consideration: atomic, so threads don't share IDs and things are easier to debug.
- CATCH_INTERNAL_THREAD_LOCAL unsigned int MessageInfo::globalCount = 0;
-
} // end namespace Catch