From 2295d2c8ccc0dba4b35adb95ef8946a05996df87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Sat, 23 Dec 2023 16:02:37 +0100 Subject: [PATCH] Store Opt/Arg hint and description as StringRef There is no good reason for these to be std::strings, as these are just (optional) constants for nice user output. This ends up reducing the allocations significantly. When measuring allocations when running no tests, the changes are `tests/SelfTest` 9213 -> 7705 `tests/ExtraTests/NoTests` 3723 -> 2215 --- src/catch2/internal/catch_clara.cpp | 2 +- src/catch2/internal/catch_clara.hpp | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/catch2/internal/catch_clara.cpp b/src/catch2/internal/catch_clara.cpp index c9bc7695..4745eb89 100644 --- a/src/catch2/internal/catch_clara.cpp +++ b/src/catch2/internal/catch_clara.cpp @@ -185,7 +185,7 @@ namespace Catch { } if (!m_hint.empty()) oss << " <" << m_hint << '>'; - return { { oss.str(), m_description } }; + return { { oss.str(), static_cast(m_description) } }; } bool Opt::isMatch(std::string const& optToken) const { diff --git a/src/catch2/internal/catch_clara.hpp b/src/catch2/internal/catch_clara.hpp index 9117b65e..d8c31c68 100644 --- a/src/catch2/internal/catch_clara.hpp +++ b/src/catch2/internal/catch_clara.hpp @@ -29,6 +29,7 @@ # endif #endif +#include #include #include #include @@ -465,8 +466,8 @@ namespace Catch { protected: Optionality m_optionality = Optionality::Optional; std::shared_ptr m_ref; - std::string m_hint; - std::string m_description; + StringRef m_hint; + StringRef m_description; explicit ParserRefImpl( std::shared_ptr const& ref ): m_ref( ref ) {} @@ -475,25 +476,25 @@ namespace Catch { template ParserRefImpl( accept_many_t, LambdaT const& ref, - std::string const& hint ): + StringRef hint ): m_ref( std::make_shared>( ref ) ), m_hint( hint ) {} template ::value>> - ParserRefImpl( T& ref, std::string const& hint ): + ParserRefImpl( T& ref, StringRef hint ): m_ref( std::make_shared>( ref ) ), m_hint( hint ) {} template ::value>> - ParserRefImpl( LambdaT const& ref, std::string const& hint ): + ParserRefImpl( LambdaT const& ref, StringRef hint ): m_ref( std::make_shared>( ref ) ), m_hint( hint ) {} - auto operator()( std::string const& description ) -> DerivedT& { + auto operator()( StringRef description ) -> DerivedT& { m_description = description; return static_cast( *this ); } @@ -519,7 +520,7 @@ namespace Catch { return 1; } - std::string const& hint() const { return m_hint; } + StringRef hint() const { return m_hint; } }; } // namespace detail @@ -552,17 +553,17 @@ namespace Catch { template ::value>> - Opt( LambdaT const& ref, std::string const& hint ): + Opt( LambdaT const& ref, StringRef hint ): ParserRefImpl( ref, hint ) {} template - Opt( accept_many_t, LambdaT const& ref, std::string const& hint ): + Opt( accept_many_t, LambdaT const& ref, StringRef hint ): ParserRefImpl( accept_many, ref, hint ) {} template ::value>> - Opt( T& ref, std::string const& hint ): + Opt( T& ref, StringRef hint ): ParserRefImpl( ref, hint ) {} auto operator[](std::string const& optName) -> Opt& {