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.
This commit is contained in:
Martin Hořeňovský
2026-08-22 20:42:23 +02:00
parent f9dfb10315
commit 9915f7250d
2 changed files with 13 additions and 0 deletions
@@ -22,6 +22,9 @@
namespace Catch {
namespace {
// Picked small-ish number at random
static size_t kInitialTestCount = 120;
static void enforceNoDuplicateTestCases(
std::vector<TestCaseHandle> 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<TestCaseInfo> testInfo, Detail::unique_ptr<ITestInvoker> testInvoker) {
@@ -36,6 +36,7 @@ namespace Catch {
std::vector<TestCaseHandle> const& getAllTests() const override;
std::vector<TestCaseHandle> const& getAllTestsSorted( IConfig const& config ) const override;
TestRegistry();
~TestRegistry() override; // = default
private: