From 9915f7250dcfc2c7bbd9e86c83b29438df39d856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Fri, 21 Aug 2026 17:37:35 +0200 Subject: [PATCH] Preallocate test case vectors in TestRegistry Preallocating to some reasonable and small number of tests avoids much of the geometric-reallocation threashing at low sizes, without taking up too much memory for tiny test binaries. --- .../internal/catch_test_case_registry_impl.cpp | 12 ++++++++++++ .../internal/catch_test_case_registry_impl.hpp | 1 + 2 files changed, 13 insertions(+) diff --git a/src/catch2/internal/catch_test_case_registry_impl.cpp b/src/catch2/internal/catch_test_case_registry_impl.cpp index e77e7bce..d23056bc 100644 --- a/src/catch2/internal/catch_test_case_registry_impl.cpp +++ b/src/catch2/internal/catch_test_case_registry_impl.cpp @@ -22,6 +22,9 @@ namespace Catch { namespace { + // Picked small-ish number at random + static size_t kInitialTestCount = 120; + static void enforceNoDuplicateTestCases( std::vector const& tests ) { auto testInfoCmp = []( TestCaseInfo const* lhs, @@ -123,6 +126,15 @@ namespace Catch { return getRegistryHub().getTestCaseRegistry().getAllTestsSorted( config ); } + + TestRegistry::TestRegistry() { + // We pre-reserve some reasonable number of tests to avoid the + // initial geometric growth churning during test registration. + m_handles.reserve( kInitialTestCount ); + m_viewed_test_infos.reserve( kInitialTestCount ); + m_owned_test_infos.reserve( kInitialTestCount ); + m_invokers.reserve( kInitialTestCount ); + } TestRegistry::~TestRegistry() = default; void TestRegistry::registerTest(Detail::unique_ptr testInfo, Detail::unique_ptr testInvoker) { diff --git a/src/catch2/internal/catch_test_case_registry_impl.hpp b/src/catch2/internal/catch_test_case_registry_impl.hpp index 52c16732..1f883109 100644 --- a/src/catch2/internal/catch_test_case_registry_impl.hpp +++ b/src/catch2/internal/catch_test_case_registry_impl.hpp @@ -36,6 +36,7 @@ namespace Catch { std::vector const& getAllTests() const override; std::vector const& getAllTestsSorted( IConfig const& config ) const override; + TestRegistry(); ~TestRegistry() override; // = default private: