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
This commit is contained in:
Martin Hořeňovský committed 2025-12-02 14:19:21 +01:00
1 parent 32eac2d1bb
commit f80956a43a
3 files changed
+40 -25

No files matched your search

+9 -5
View File
@@ -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