diff options
| author | Martin Hořeňovský | 2020-05-25 09:45:24 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2020-05-31 15:20:24 +0200 |
| commit | 1d1ccf8f3c0c9e121b16afd4fafb850d14ef9fb0 (patch) | |
| tree | 0a292cf929c75743993d42453312e08888ef7c0a /src | |
| parent | 41bbaa6d573a598fe0796fd0e391ed57eae62e30 (diff) | |
| download | catch3-1d1ccf8f3c0c9e121b16afd4fafb850d14ef9fb0.tar.gz | |
Replace all uses of std::unique_ptr with Catch::Detail::unique_ptr
Doing some benchmarking with ClangBuildAnalyzer suggests that
compiling Catch2's `SelfTest` spends 10% of the time instantiating
`std::unique_ptr` for some interface types required for registering
and running tests.
The lesser compilation overhead of `Catch::Detail::unique_ptr` should
significantly reduce that time.
The compiled implementation was also changed to use the custom impl,
to avoid having to convert between using `std::unique_ptr` and
`Catch::Detail::unique_ptr`. This will likely also improve the compile
times of the implementation, but that is less important than improving
compilation times of the user's TUs with tests.
Diffstat (limited to 'src')
33 files changed, 133 insertions, 120 deletions
diff --git a/src/catch2/benchmark/detail/catch_benchmark_function.hpp b/src/catch2/benchmark/detail/catch_benchmark_function.hpp index 03ef22a7..ebeb7fa0 100644 --- a/src/catch2/benchmark/detail/catch_benchmark_function.hpp +++ b/src/catch2/benchmark/detail/catch_benchmark_function.hpp @@ -14,11 +14,11 @@ #include <catch2/benchmark/catch_chronometer.hpp> #include <catch2/benchmark/detail/catch_complete_invoke.hpp> #include <catch2/internal/catch_meta.hpp> +#include <catch2/internal/catch_unique_ptr.hpp> #include <cassert> #include <type_traits> #include <utility> -#include <memory> namespace Catch { namespace Benchmark { @@ -100,7 +100,7 @@ namespace Catch { void operator()(Chronometer meter) const { f->call(meter); } private: - std::unique_ptr<callable> f; + Catch::Detail::unique_ptr<callable> f; }; } // namespace Detail } // namespace Benchmark diff --git a/src/catch2/catch_config.hpp b/src/catch2/catch_config.hpp index 55cff0ee..dd8767dc 100644 --- a/src/catch2/catch_config.hpp +++ b/src/catch2/catch_config.hpp @@ -10,8 +10,8 @@ #include <catch2/catch_test_spec.hpp> #include <catch2/interfaces/catch_interfaces_config.hpp> +#include <catch2/internal/catch_unique_ptr.hpp> -#include <memory> #include <vector> #include <string> @@ -116,7 +116,7 @@ namespace Catch { IStream const* openStream(); ConfigData m_data; - std::unique_ptr<IStream const> m_stream; + Detail::unique_ptr<IStream const> m_stream; TestSpec m_testSpec; bool m_hasTestFilters = false; }; diff --git a/src/catch2/catch_registry_hub.cpp b/src/catch2/catch_registry_hub.cpp index c6a19f09..ed2d068a 100644 --- a/src/catch2/catch_registry_hub.cpp +++ b/src/catch2/catch_registry_hub.cpp @@ -49,7 +49,7 @@ namespace Catch { void registerListener( IReporterFactoryPtr factory ) override { m_reporterRegistry.registerListener( std::move(factory) ); } - void registerTest( std::unique_ptr<TestCaseInfo>&& testInfo, std::unique_ptr<ITestInvoker>&& invoker ) override { + void registerTest( Detail::unique_ptr<TestCaseInfo>&& testInfo, Detail::unique_ptr<ITestInvoker>&& invoker ) override { m_testCaseRegistry.registerTest( std::move(testInfo), std::move(invoker) ); } void registerTranslator( const IExceptionTranslator* translator ) override { diff --git a/src/catch2/catch_reporter_registrars.hpp b/src/catch2/catch_reporter_registrars.hpp index 0b4c19f2..8ad600ea 100644 --- a/src/catch2/catch_reporter_registrars.hpp +++ b/src/catch2/catch_reporter_registrars.hpp @@ -10,6 +10,7 @@ #define TWOBLUECUBES_CATCH_REPORTER_REGISTRARS_HPP_INCLUDED #include <catch2/interfaces/catch_interfaces_registry_hub.hpp> +#include <catch2/internal/catch_unique_ptr.hpp> namespace Catch { @@ -17,7 +18,7 @@ namespace Catch { class ReporterFactory : public IReporterFactory { IStreamingReporterPtr create( ReporterConfig const& config ) const override { - return std::make_unique<T>( config ); + return Detail::make_unique<T>( config ); } std::string getDescription() const override { @@ -30,7 +31,7 @@ namespace Catch { class ReporterRegistrar { public: explicit ReporterRegistrar( std::string const& name ) { - getMutableRegistryHub().registerReporter( name, std::make_unique<ReporterFactory<T>>() ); + getMutableRegistryHub().registerReporter( name, Detail::make_unique<ReporterFactory<T>>() ); } }; @@ -40,7 +41,7 @@ namespace Catch { class ListenerFactory : public IReporterFactory { IStreamingReporterPtr create( ReporterConfig const& config ) const override { - return std::make_unique<T>(config); + return Detail::make_unique<T>(config); } std::string getDescription() const override { return std::string(); @@ -50,7 +51,7 @@ namespace Catch { public: ListenerRegistrar() { - getMutableRegistryHub().registerListener( std::make_unique<ListenerFactory>() ); + getMutableRegistryHub().registerListener( Detail::make_unique<ListenerFactory>() ); } }; } diff --git a/src/catch2/catch_session.cpp b/src/catch2/catch_session.cpp index 351deb8d..3abd8410 100644 --- a/src/catch2/catch_session.cpp +++ b/src/catch2/catch_session.cpp @@ -44,12 +44,12 @@ namespace Catch { return createReporter(config->getReporterName(), config); } - // On older platforms, returning std::unique_ptr<ListeningReporter> - // when the return type is std::unique_ptr<IStreamingReporter> + // On older platforms, returning unique_ptr<ListeningReporter> + // when the return type is unique_ptr<IStreamingReporter> // doesn't compile without a std::move call. However, this causes // a warning on newer platforms. Thus, we have to work around // it a bit and downcast the pointer manually. - auto ret = std::unique_ptr<IStreamingReporter>(new ListeningReporter); + auto ret = Detail::unique_ptr<IStreamingReporter>(new ListeningReporter); auto& multi = static_cast<ListeningReporter&>(*ret); auto const& listeners = Catch::getRegistryHub().getReporterRegistry().getListeners(); for (auto const& listener : listeners) { @@ -252,7 +252,7 @@ namespace Catch { } Config& Session::config() { if( !m_config ) - m_config = std::make_unique<Config>( m_configData ); + m_config = Detail::make_unique<Config>( m_configData ); return *m_config; } diff --git a/src/catch2/catch_session.hpp b/src/catch2/catch_session.hpp index 54fe37dd..52bf9f34 100644 --- a/src/catch2/catch_session.hpp +++ b/src/catch2/catch_session.hpp @@ -11,8 +11,7 @@ #include <catch2/internal/catch_commandline.hpp> #include <catch2/catch_config.hpp> #include <catch2/internal/catch_text.hpp> - -#include <memory> +#include <catch2/internal/catch_unique_ptr.hpp> namespace Catch { @@ -53,7 +52,7 @@ namespace Catch { clara::Parser m_cli; ConfigData m_configData; - std::unique_ptr<Config> m_config; + Detail::unique_ptr<Config> m_config; bool m_startupExceptions = false; }; diff --git a/src/catch2/catch_test_case_info.cpp b/src/catch2/catch_test_case_info.cpp index 7be5b861..a4bcb11c 100644 --- a/src/catch2/catch_test_case_info.cpp +++ b/src/catch2/catch_test_case_info.cpp @@ -105,11 +105,11 @@ namespace Catch { } } - std::unique_ptr<TestCaseInfo> + Detail::unique_ptr<TestCaseInfo> makeTestCaseInfo(std::string const& _className, NameAndTags const& nameAndTags, SourceLineInfo const& _lineInfo ) { - return std::make_unique<TestCaseInfo>(_className, nameAndTags, _lineInfo); + return Detail::unique_ptr<TestCaseInfo>(new TestCaseInfo(_className, nameAndTags, _lineInfo)); } TestCaseInfo::TestCaseInfo(std::string const& _className, diff --git a/src/catch2/catch_test_case_info.hpp b/src/catch2/catch_test_case_info.hpp index f586ce92..991cca61 100644 --- a/src/catch2/catch_test_case_info.hpp +++ b/src/catch2/catch_test_case_info.hpp @@ -11,10 +11,11 @@ #include <catch2/internal/catch_common.hpp> #include <catch2/internal/catch_stringref.hpp> #include <catch2/internal/catch_test_registry.hpp> +#include <catch2/internal/catch_unique_ptr.hpp> + #include <string> #include <vector> -#include <memory> #ifdef __clang__ #pragma clang diagnostic push @@ -90,7 +91,7 @@ namespace Catch { bool operator < ( TestCaseHandle const& rhs ) const; }; - std::unique_ptr<TestCaseInfo> makeTestCaseInfo( std::string const& className, + Detail::unique_ptr<TestCaseInfo> makeTestCaseInfo( std::string const& className, NameAndTags const& nameAndTags, SourceLineInfo const& lineInfo ); } diff --git a/src/catch2/catch_test_spec.cpp b/src/catch2/catch_test_spec.cpp index 87cf8cae..88966ec1 100644 --- a/src/catch2/catch_test_spec.cpp +++ b/src/catch2/catch_test_spec.cpp @@ -13,7 +13,6 @@ #include <algorithm> #include <string> #include <vector> -#include <memory> namespace Catch { diff --git a/src/catch2/catch_test_spec.hpp b/src/catch2/catch_test_spec.hpp index 3a767c74..7bb86512 100644 --- a/src/catch2/catch_test_spec.hpp +++ b/src/catch2/catch_test_spec.hpp @@ -13,11 +13,11 @@ #pragma clang diagnostic ignored "-Wpadded" #endif +#include <catch2/internal/catch_unique_ptr.hpp> #include <catch2/internal/catch_wildcard_pattern.hpp> #include <string> #include <vector> -#include <memory> namespace Catch { @@ -54,8 +54,8 @@ namespace Catch { }; struct Filter { - std::vector<std::unique_ptr<Pattern>> m_required; - std::vector<std::unique_ptr<Pattern>> m_forbidden; + std::vector<Detail::unique_ptr<Pattern>> m_required; + std::vector<Detail::unique_ptr<Pattern>> m_forbidden; bool matches( TestCaseInfo const& testCase ) const; std::string name() const; diff --git a/src/catch2/generators/catch_generators.hpp b/src/catch2/generators/catch_generators.hpp index e8c2a8fd..2e8fad14 100644 --- a/src/catch2/generators/catch_generators.hpp +++ b/src/catch2/generators/catch_generators.hpp @@ -10,7 +10,6 @@ #include <catch2/interfaces/catch_interfaces_generatortracker.hpp> #include <catch2/internal/catch_common.hpp> -#include <memory> #include <vector> #include <tuple> #include <utility> @@ -43,11 +42,35 @@ namespace Detail { using type = T; }; + template <typename T> + using GeneratorPtr = Catch::Detail::unique_ptr<IGenerator<T>>; + + template <typename T> + class GeneratorWrapper final { + GeneratorPtr<T> m_generator; + public: + //! Takes ownership of the passed pointer. + GeneratorWrapper(IGenerator<T>* generator): + m_generator(generator) {} + GeneratorWrapper(GeneratorPtr<T> generator): + m_generator(std::move(generator)) {} + + T const& get() const { + return m_generator->get(); + } + bool next() { + return m_generator->next(); + } + }; + + template<typename T> class SingleValueGenerator final : public IGenerator<T> { T m_value; public: - SingleValueGenerator(T&& value) : m_value(std::move(value)) {} + SingleValueGenerator(T&& value): + m_value(std::forward<T>(value)) + {} T const& get() const override { return m_value; @@ -77,27 +100,12 @@ namespace Detail { }; template <typename T> - class GeneratorWrapper final { - std::unique_ptr<IGenerator<T>> m_generator; - public: - GeneratorWrapper(std::unique_ptr<IGenerator<T>> generator): - m_generator(std::move(generator)) - {} - T const& get() const { - return m_generator->get(); - } - bool next() { - return m_generator->next(); - } - }; - - template <typename T> GeneratorWrapper<T> value(T&& value) { - return GeneratorWrapper<T>(std::make_unique<SingleValueGenerator<T>>(std::forward<T>(value))); + return GeneratorWrapper<T>(Catch::Detail::make_unique<SingleValueGenerator<T>>(std::forward<T>(value))); } template <typename T> GeneratorWrapper<T> values(std::initializer_list<T> values) { - return GeneratorWrapper<T>(std::make_unique<FixedValuesGenerator<T>>(values)); + return GeneratorWrapper<T>(Catch::Detail::make_unique<FixedValuesGenerator<T>>(values)); } template<typename T> @@ -182,7 +190,7 @@ namespace Detail { IGeneratorTracker& tracker = acquireGeneratorTracker( lineInfo ); if (!tracker.hasGenerator()) { - tracker.setGenerator(std::make_unique<Generators<UnderlyingType>>(generatorExpression())); + tracker.setGenerator(Catch::Detail::make_unique<Generators<UnderlyingType>>(generatorExpression())); } auto const& generator = static_cast<IGenerator<UnderlyingType> const&>( *tracker.getGenerator() ); diff --git a/src/catch2/generators/catch_generators_adapters.hpp b/src/catch2/generators/catch_generators_adapters.hpp index 774df819..d4ea46e3 100644 --- a/src/catch2/generators/catch_generators_adapters.hpp +++ b/src/catch2/generators/catch_generators_adapters.hpp @@ -46,7 +46,7 @@ namespace Generators { template <typename T> GeneratorWrapper<T> take(size_t target, GeneratorWrapper<T>&& generator) { - return GeneratorWrapper<T>(std::make_unique<TakeGenerator<T>>(target, std::move(generator))); + return GeneratorWrapper<T>(Catch::Detail::make_unique<TakeGenerator<T>>(target, std::move(generator))); } @@ -87,7 +87,7 @@ namespace Generators { template <typename T, typename Predicate> GeneratorWrapper<T> filter(Predicate&& pred, GeneratorWrapper<T>&& generator) { - return GeneratorWrapper<T>(std::unique_ptr<IGenerator<T>>(std::make_unique<FilterGenerator<T, Predicate>>(std::forward<Predicate>(pred), std::move(generator)))); + return GeneratorWrapper<T>(Catch::Detail::make_unique<FilterGenerator<T, Predicate>>(std::forward<Predicate>(pred), std::move(generator))); } template <typename T> @@ -143,7 +143,7 @@ namespace Generators { template <typename T> GeneratorWrapper<T> repeat(size_t repeats, GeneratorWrapper<T>&& generator) { - return GeneratorWrapper<T>(std::make_unique<RepeatGenerator<T>>(repeats, std::move(generator))); + return GeneratorWrapper<T>(Catch::Detail::make_unique<RepeatGenerator<T>>(repeats, std::move(generator))); } template <typename T, typename U, typename Func> @@ -176,14 +176,14 @@ namespace Generators { template <typename Func, typename U, typename T = FunctionReturnType<Func, U>> GeneratorWrapper<T> map(Func&& function, GeneratorWrapper<U>&& generator) { return GeneratorWrapper<T>( - std::make_unique<MapGenerator<T, U, Func>>(std::forward<Func>(function), std::move(generator)) + Catch::Detail::make_unique<MapGenerator<T, U, Func>>(std::forward<Func>(function), std::move(generator)) ); } template <typename T, typename U, typename Func> GeneratorWrapper<T> map(Func&& function, GeneratorWrapper<U>&& generator) { return GeneratorWrapper<T>( - std::make_unique<MapGenerator<T, U, Func>>(std::forward<Func>(function), std::move(generator)) + Catch::Detail::make_unique<MapGenerator<T, U, Func>>(std::forward<Func>(function), std::move(generator)) ); } @@ -226,7 +226,7 @@ namespace Generators { template <typename T> GeneratorWrapper<std::vector<T>> chunk(size_t size, GeneratorWrapper<T>&& generator) { return GeneratorWrapper<std::vector<T>>( - std::make_unique<ChunkGenerator<T>>(size, std::move(generator)) + Catch::Detail::make_unique<ChunkGenerator<T>>(size, std::move(generator)) ); } diff --git a/src/catch2/generators/catch_generators_random.hpp b/src/catch2/generators/catch_generators_random.hpp index 03cb57c1..eb6de023 100644 --- a/src/catch2/generators/catch_generators_random.hpp +++ b/src/catch2/generators/catch_generators_random.hpp @@ -67,7 +67,7 @@ std::enable_if_t<std::is_integral<T>::value && !std::is_same<T, bool>::value, GeneratorWrapper<T>> random(T a, T b) { return GeneratorWrapper<T>( - std::make_unique<RandomIntegerGenerator<T>>(a, b) + Catch::Detail::make_unique<RandomIntegerGenerator<T>>(a, b) ); } @@ -76,7 +76,7 @@ std::enable_if_t<std::is_floating_point<T>::value, GeneratorWrapper<T>> random(T a, T b) { return GeneratorWrapper<T>( - std::make_unique<RandomFloatingGenerator<T>>(a, b) + Catch::Detail::make_unique<RandomFloatingGenerator<T>>(a, b) ); } diff --git a/src/catch2/generators/catch_generators_range.hpp b/src/catch2/generators/catch_generators_range.hpp index e2c5c522..ffb80377 100644 --- a/src/catch2/generators/catch_generators_range.hpp +++ b/src/catch2/generators/catch_generators_range.hpp @@ -52,13 +52,13 @@ public: template <typename T> GeneratorWrapper<T> range(T const& start, T const& end, T const& step) { static_assert(std::is_arithmetic<T>::value && !std::is_same<T, bool>::value, "Type must be numeric"); - return GeneratorWrapper<T>(std::make_unique<RangeGenerator<T>>(start, end, step)); + return GeneratorWrapper<T>(Catch::Detail::make_unique<RangeGenerator<T>>(start, end, step)); } template <typename T> GeneratorWrapper<T> range(T const& start, T const& end) { static_assert(std::is_integral<T>::value && !std::is_same<T, bool>::value, "Type must be an integer"); - return GeneratorWrapper<T>(std::make_unique<RangeGenerator<T>>(start, end)); + return GeneratorWrapper<T>(Catch::Detail::make_unique<RangeGenerator<T>>(start, end)); } @@ -92,13 +92,13 @@ template <typename InputIterator, typename InputSentinel, typename ResultType = typename std::iterator_traits<InputIterator>::value_type> GeneratorWrapper<ResultType> from_range(InputIterator from, InputSentinel to) { - return GeneratorWrapper<ResultType>(std::make_unique<IteratorGenerator<ResultType>>(from, to)); + return GeneratorWrapper<ResultType>(Catch::Detail::make_unique<IteratorGenerator<ResultType>>(from, to)); } template <typename Container, typename ResultType = typename Container::value_type> GeneratorWrapper<ResultType> from_range(Container const& cnt) { - return GeneratorWrapper<ResultType>(std::make_unique<IteratorGenerator<ResultType>>(cnt.begin(), cnt.end())); + return GeneratorWrapper<ResultType>(Catch::Detail::make_unique<IteratorGenerator<ResultType>>(cnt.begin(), cnt.end())); } diff --git a/src/catch2/interfaces/catch_interfaces_exception.hpp b/src/catch2/interfaces/catch_interfaces_exception.hpp index e9fb9ef7..31b6dc20 100644 --- a/src/catch2/interfaces/catch_interfaces_exception.hpp +++ b/src/catch2/interfaces/catch_interfaces_exception.hpp @@ -10,6 +10,7 @@ #include <catch2/interfaces/catch_interfaces_registry_hub.hpp> #include <catch2/internal/catch_compiler_capabilities.hpp> +#include <catch2/internal/catch_unique_ptr.hpp> #include <string> #include <vector> @@ -18,7 +19,7 @@ namespace Catch { using exceptionTranslateFunction = std::string(*)(); struct IExceptionTranslator; - using ExceptionTranslators = std::vector<std::unique_ptr<IExceptionTranslator const>>; + using ExceptionTranslators = std::vector<Detail::unique_ptr<IExceptionTranslator const>>; struct IExceptionTranslator { virtual ~IExceptionTranslator(); diff --git a/src/catch2/interfaces/catch_interfaces_generatortracker.hpp b/src/catch2/interfaces/catch_interfaces_generatortracker.hpp index fa08be0c..88615ef5 100644 --- a/src/catch2/interfaces/catch_interfaces_generatortracker.hpp +++ b/src/catch2/interfaces/catch_interfaces_generatortracker.hpp @@ -8,7 +8,7 @@ #ifndef TWOBLUECUBES_CATCH_INTERFACES_GENERATORTRACKER_INCLUDED #define TWOBLUECUBES_CATCH_INTERFACES_GENERATORTRACKER_INCLUDED -#include <memory> +#include <catch2/internal/catch_unique_ptr.hpp> namespace Catch { @@ -29,7 +29,7 @@ namespace Catch { // can be retrieved). virtual bool next() = 0; }; - using GeneratorBasePtr = std::unique_ptr<GeneratorUntypedBase>; + using GeneratorBasePtr = Catch::Detail::unique_ptr<GeneratorUntypedBase>; } // namespace Generators diff --git a/src/catch2/interfaces/catch_interfaces_registry_hub.hpp b/src/catch2/interfaces/catch_interfaces_registry_hub.hpp index e292763d..3e99d215 100644 --- a/src/catch2/interfaces/catch_interfaces_registry_hub.hpp +++ b/src/catch2/interfaces/catch_interfaces_registry_hub.hpp @@ -9,9 +9,9 @@ #define TWOBLUECUBES_CATCH_INTERFACES_REGISTRY_HUB_H_INCLUDED #include <catch2/internal/catch_common.hpp> +#include <catch2/internal/catch_unique_ptr.hpp> #include <string> -#include <memory> namespace Catch { @@ -28,7 +28,7 @@ namespace Catch { class StartupExceptionRegistry; - using IReporterFactoryPtr = std::unique_ptr<IReporterFactory>; + using IReporterFactoryPtr = Detail::unique_ptr<IReporterFactory>; struct IRegistryHub { virtual ~IRegistryHub(); @@ -46,7 +46,7 @@ namespace Catch { virtual ~IMutableRegistryHub(); virtual void registerReporter( std::string const& name, IReporterFactoryPtr factory ) = 0; virtual void registerListener( IReporterFactoryPtr factory ) = 0; - virtual void registerTest(std::unique_ptr<TestCaseInfo>&& testInfo, std::unique_ptr<ITestInvoker>&& invoker) = 0; + virtual void registerTest(Detail::unique_ptr<TestCaseInfo>&& testInfo, Detail::unique_ptr<ITestInvoker>&& invoker) = 0; virtual void registerTranslator( const IExceptionTranslator* translator ) = 0; virtual void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) = 0; virtual void registerStartupException() noexcept = 0; |