diff options
| author | Martin Hořeňovský | 2025-09-23 11:32:20 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2025-09-27 13:24:44 +0200 |
| commit | a2e41916f2cc0adbd27fc3d9cb89e9615fea1ad8 (patch) | |
| tree | d84e247ae75e2119effb428c4a367a35c236a267 /src/catch2/internal/catch_context.cpp | |
| parent | 0e772cc0d29e7518f84a1ad11aa179167a6ab6bc (diff) | |
| download | catch3-a2e41916f2cc0adbd27fc3d9cb89e9615fea1ad8.tar.gz | |
Keep the main Context instance as static value, not pointer
This allows us to remove the lazy init checks, improving the inlining
potential when retrieving current context, thus slightly improving
the performance of assertions.
**runtime difference**
| --------- | Debug | Release |
|:----------|------:|--------:|
| Slow path | 1.01 | 0.98 |
| Fast path | 1.02 | 1.02 |
There is small slowdown in case of Release build + assertions taking
the slow path, but
1) going through the slow path is rare
2) Given the code change, I believe this to be artifact of the
optimizer in the old GCC version I am using locally.
Diffstat (limited to 'src/catch2/internal/catch_context.cpp')
| -rw-r--r-- | src/catch2/internal/catch_context.cpp | 15 |
1 files changed, 2 insertions, 13 deletions
diff --git a/src/catch2/internal/catch_context.cpp b/src/catch2/internal/catch_context.cpp index 8acf1eda..5389de31 100644 --- a/src/catch2/internal/catch_context.cpp +++ b/src/catch2/internal/catch_context.cpp @@ -6,25 +6,14 @@ // SPDX-License-Identifier: BSL-1.0 #include <catch2/internal/catch_context.hpp> -#include <catch2/internal/catch_noncopyable.hpp> #include <catch2/internal/catch_random_number_generator.hpp> namespace Catch { - Context* Context::currentContext = nullptr; - - void cleanUpContext() { - delete Context::currentContext; - Context::currentContext = nullptr; - } - void Context::createContext() { - currentContext = new Context(); - } + Context Context::currentContext; Context& getCurrentMutableContext() { - if ( !Context::currentContext ) { Context::createContext(); } - // NOLINTNEXTLINE(clang-analyzer-core.uninitialized.UndefReturn) - return *Context::currentContext; + return Context::currentContext; } SimplePcg32& sharedRng() { |