Skip to content

Commit

Permalink
Replace uses of std::move and std::forward with macros
Browse files Browse the repository at this point in the history
This improves the SelfTest build times by about 3% (measured
with Clang 10 on random Linux box I had lying around).
  • Loading branch information
horenmar committed Aug 16, 2021
1 parent 03ce304 commit d2ee710
Show file tree
Hide file tree
Showing 41 changed files with 156 additions and 129 deletions.
6 changes: 3 additions & 3 deletions src/catch2/benchmark/catch_benchmark.hpp
Expand Up @@ -15,7 +15,7 @@
#include <catch2/internal/catch_context.hpp>
#include <catch2/interfaces/catch_interfaces_reporter.hpp>
#include <catch2/internal/catch_unique_name.hpp>

#include <catch2/internal/catch_move_and_forward.hpp>
#include <catch2/benchmark/catch_chronometer.hpp>
#include <catch2/benchmark/catch_clock.hpp>
#include <catch2/benchmark/catch_environment.hpp>
Expand All @@ -36,11 +36,11 @@ namespace Catch {
namespace Benchmark {
struct Benchmark {
Benchmark(std::string&& benchmarkName)
: name(std::move(benchmarkName)) {}
: name(CATCH_MOVE(benchmarkName)) {}

template <class FUN>
Benchmark(std::string&& benchmarkName , FUN &&func)
: fun(std::move(func)), name(std::move(benchmarkName)) {}
: fun(CATCH_MOVE(func)), name(CATCH_MOVE(benchmarkName)) {}

template <typename Clock>
ExecutionPlan<FloatDuration<Clock>> prepare(const IConfig &cfg, Environment<FloatDuration<Clock>> env) const {
Expand Down
3 changes: 2 additions & 1 deletion src/catch2/benchmark/catch_chronometer.hpp
Expand Up @@ -14,6 +14,7 @@
#include <catch2/benchmark/catch_optimizer.hpp>
#include <catch2/benchmark/detail/catch_complete_invoke.hpp>
#include <catch2/internal/catch_meta.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>

namespace Catch {
namespace Benchmark {
Expand Down Expand Up @@ -42,7 +43,7 @@ namespace Catch {
struct Chronometer {
public:
template <typename Fun>
void measure(Fun&& fun) { measure(std::forward<Fun>(fun), is_callable<Fun(int)>()); }
void measure(Fun&& fun) { measure(CATCH_FORWARD(fun), is_callable<Fun(int)>()); }

int runs() const { return repeats; }

Expand Down
7 changes: 4 additions & 3 deletions src/catch2/benchmark/catch_constructor.hpp
Expand Up @@ -10,8 +10,9 @@
#ifndef CATCH_CONSTRUCTOR_HPP_INCLUDED
#define CATCH_CONSTRUCTOR_HPP_INCLUDED

#include <catch2/internal/catch_move_and_forward.hpp>

#include <type_traits>
#include <utility>

namespace Catch {
namespace Benchmark {
Expand All @@ -30,15 +31,15 @@ namespace Catch {

ObjectStorage(ObjectStorage&& other)
{
new(&data) T(std::move(other.stored_object()));
new(&data) T(CATCH_MOVE(other.stored_object()));
}

~ObjectStorage() { destruct_on_exit<T>(); }

template <typename... Args>
void construct(Args&&... args)
{
new (&data) T(std::forward<Args>(args)...);
new (&data) T(CATCH_FORWARD(args)...);
}

template <bool AllowManualDestruction = !Destruct>
Expand Down
7 changes: 4 additions & 3 deletions src/catch2/benchmark/catch_optimizer.hpp
Expand Up @@ -14,8 +14,9 @@
# include <atomic> // atomic_thread_fence
#endif

#include <catch2/internal/catch_move_and_forward.hpp>

#include <type_traits>
#include <utility>

namespace Catch {
namespace Benchmark {
Expand Down Expand Up @@ -57,12 +58,12 @@ namespace Catch {

template <typename Fn, typename... Args>
inline auto invoke_deoptimized(Fn&& fn, Args&&... args) -> typename std::enable_if<!std::is_same<void, decltype(fn(args...))>::value>::type {
deoptimize_value(std::forward<Fn>(fn) (std::forward<Args...>(args...)));
deoptimize_value(CATCH_FORWARD(fn) (CATCH_FORWARD(args)...));
}

template <typename Fn, typename... Args>
inline auto invoke_deoptimized(Fn&& fn, Args&&... args) -> typename std::enable_if<std::is_same<void, decltype(fn(args...))>::value>::type {
std::forward<Fn>(fn) (std::forward<Args...>(args...));
CATCH_FORWARD(fn) (CATCH_FORWARD(args)...);
}
} // namespace Benchmark
} // namespace Catch
Expand Down
4 changes: 2 additions & 2 deletions src/catch2/benchmark/catch_sample_analysis.hpp
Expand Up @@ -13,10 +13,10 @@
#include <catch2/benchmark/catch_clock.hpp>
#include <catch2/benchmark/catch_estimate.hpp>
#include <catch2/benchmark/catch_outlier_classification.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>

#include <algorithm>
#include <vector>
#include <string>
#include <iterator>

namespace Catch {
Expand All @@ -35,7 +35,7 @@ namespace Catch {
samples2.reserve(samples.size());
std::transform(samples.begin(), samples.end(), std::back_inserter(samples2), [](Duration d) { return Duration2(d); });
return {
std::move(samples2),
CATCH_MOVE(samples2),
mean,
standard_deviation,
outliers,
Expand Down
5 changes: 3 additions & 2 deletions src/catch2/benchmark/detail/catch_analyse.hpp
Expand Up @@ -15,6 +15,7 @@
#include <catch2/benchmark/catch_sample_analysis.hpp>
#include <catch2/benchmark/detail/catch_stats.hpp>
#include <catch2/interfaces/catch_interfaces_config.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>

#include <algorithm>
#include <iterator>
Expand Down Expand Up @@ -45,7 +46,7 @@ namespace Catch {
samples2.reserve(samples.size());
std::transform(samples.begin(), samples.end(), std::back_inserter(samples2), [](double d) { return Duration(d); });
return {
std::move(samples2),
CATCH_MOVE(samples2),
wrap_estimate(analysis.mean),
wrap_estimate(analysis.standard_deviation),
outliers,
Expand All @@ -64,7 +65,7 @@ namespace Catch {
mean /= i;

return {
std::move(samples),
CATCH_MOVE(samples),
Estimate<Duration>{mean, mean, mean, 0.0},
Estimate<Duration>{Duration(0), Duration(0), Duration(0), 0.0},
OutlierClassification{},
Expand Down
10 changes: 5 additions & 5 deletions src/catch2/benchmark/detail/catch_benchmark_function.hpp
Expand Up @@ -14,8 +14,8 @@
#include <catch2/benchmark/detail/catch_complete_invoke.hpp>
#include <catch2/internal/catch_meta.hpp>
#include <catch2/internal/catch_unique_ptr.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>

#include <cassert>
#include <type_traits>
#include <utility>

Expand Down Expand Up @@ -48,7 +48,7 @@ namespace Catch {
};
template <typename Fun>
struct model : public callable {
model(Fun&& fun_) : fun(std::move(fun_)) {}
model(Fun&& fun_) : fun(CATCH_MOVE(fun_)) {}
model(Fun const& fun_) : fun(fun_) {}

model<Fun>* clone() const override { return new model<Fun>(*this); }
Expand Down Expand Up @@ -78,17 +78,17 @@ namespace Catch {
template <typename Fun,
typename std::enable_if<!is_related<Fun, BenchmarkFunction>::value, int>::type = 0>
BenchmarkFunction(Fun&& fun)
: f(new model<typename std::decay<Fun>::type>(std::forward<Fun>(fun))) {}
: f(new model<typename std::decay<Fun>::type>(CATCH_FORWARD(fun))) {}

BenchmarkFunction( BenchmarkFunction&& that ) noexcept:
f( std::move( that.f ) ) {}
f( CATCH_MOVE( that.f ) ) {}

BenchmarkFunction(BenchmarkFunction const& that)
: f(that.f->clone()) {}

BenchmarkFunction&
operator=( BenchmarkFunction&& that ) noexcept {
f = std::move( that.f );
f = CATCH_MOVE( that.f );
return *this;
}

Expand Down
9 changes: 5 additions & 4 deletions src/catch2/benchmark/detail/catch_complete_invoke.hpp
Expand Up @@ -14,6 +14,7 @@
#include <catch2/internal/catch_meta.hpp>
#include <catch2/interfaces/catch_interfaces_capture.hpp>
#include <catch2/interfaces/catch_interfaces_registry_hub.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>

#include <type_traits>
#include <utility>
Expand All @@ -33,29 +34,29 @@ namespace Catch {
struct CompleteInvoker {
template <typename Fun, typename... Args>
static Result invoke(Fun&& fun, Args&&... args) {
return std::forward<Fun>(fun)(std::forward<Args>(args)...);
return CATCH_FORWARD(fun)(CATCH_FORWARD(args)...);
}
};
template <>
struct CompleteInvoker<void> {
template <typename Fun, typename... Args>
static CompleteType_t<void> invoke(Fun&& fun, Args&&... args) {
std::forward<Fun>(fun)(std::forward<Args>(args)...);
CATCH_FORWARD(fun)(CATCH_FORWARD(args)...);
return {};
}
};

// invoke and not return void :(
template <typename Fun, typename... Args>
CompleteType_t<FunctionReturnType<Fun, Args...>> complete_invoke(Fun&& fun, Args&&... args) {
return CompleteInvoker<FunctionReturnType<Fun, Args...>>::invoke(std::forward<Fun>(fun), std::forward<Args>(args)...);
return CompleteInvoker<FunctionReturnType<Fun, Args...>>::invoke(CATCH_FORWARD(fun), CATCH_FORWARD(args)...);
}

} // namespace Detail

template <typename Fun>
Detail::CompleteType_t<FunctionReturnType<Fun>> user_code(Fun&& fun) {
return Detail::complete_invoke(std::forward<Fun>(fun));
return Detail::complete_invoke(CATCH_FORWARD(fun));
}
} // namespace Benchmark
} // namespace Catch
Expand Down
7 changes: 3 additions & 4 deletions src/catch2/benchmark/detail/catch_measure.hpp
Expand Up @@ -13,19 +13,18 @@
#include <catch2/benchmark/catch_clock.hpp>
#include <catch2/benchmark/detail/catch_complete_invoke.hpp>
#include <catch2/benchmark/detail/catch_timing.hpp>

#include <utility>
#include <catch2/internal/catch_move_and_forward.hpp>

namespace Catch {
namespace Benchmark {
namespace Detail {
template <typename Clock, typename Fun, typename... Args>
TimingOf<Clock, Fun, Args...> measure(Fun&& fun, Args&&... args) {
auto start = Clock::now();
auto&& r = Detail::complete_invoke(fun, std::forward<Args>(args)...);
auto&& r = Detail::complete_invoke(fun, CATCH_FORWARD(args)...);
auto end = Clock::now();
auto delta = end - start;
return { delta, std::forward<decltype(r)>(r), 1 };
return { delta, CATCH_FORWARD(r), 1 };
}
} // namespace Detail
} // namespace Benchmark
Expand Down
4 changes: 2 additions & 2 deletions src/catch2/benchmark/detail/catch_repeat.hpp
Expand Up @@ -11,7 +11,7 @@
#define CATCH_REPEAT_HPP_INCLUDED

#include <type_traits>
#include <utility>
#include <catch2/internal/catch_move_and_forward.hpp>

namespace Catch {
namespace Benchmark {
Expand All @@ -27,7 +27,7 @@ namespace Catch {
};
template <typename Fun>
repeater<typename std::decay<Fun>::type> repeat(Fun&& fun) {
return { std::forward<Fun>(fun) };
return { CATCH_FORWARD(fun) };
}
} // namespace Detail
} // namespace Benchmark
Expand Down
6 changes: 3 additions & 3 deletions src/catch2/benchmark/detail/catch_run_for_at_least.hpp
Expand Up @@ -16,8 +16,8 @@
#include <catch2/benchmark/detail/catch_complete_invoke.hpp>
#include <catch2/benchmark/detail/catch_timing.hpp>
#include <catch2/internal/catch_meta.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>

#include <utility>
#include <type_traits>

namespace Catch {
Expand All @@ -32,7 +32,7 @@ namespace Catch {
Detail::ChronometerModel<Clock> meter;
auto&& result = Detail::complete_invoke(fun, Chronometer(meter, iters));

return { meter.elapsed(), std::move(result), iters };
return { meter.elapsed(), CATCH_MOVE(result), iters };
}

template <typename Clock, typename Fun>
Expand All @@ -52,7 +52,7 @@ namespace Catch {
auto&& Timing = measure_one<Clock>(fun, iters, is_callable<Fun(Chronometer)>());

if (Timing.elapsed >= how_long) {
return { Timing.elapsed, std::move(Timing.result), iters };
return { Timing.elapsed, CATCH_MOVE(Timing.result), iters };
}
iters *= 2;
}
Expand Down
3 changes: 2 additions & 1 deletion src/catch2/catch_message.cpp
Expand Up @@ -9,6 +9,7 @@
#include <catch2/interfaces/catch_interfaces_capture.hpp>
#include <catch2/internal/catch_uncaught_exceptions.hpp>
#include <catch2/internal/catch_enforce.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>

#include <cassert>
#include <stack>
Expand All @@ -25,7 +26,7 @@ namespace Catch {
}

ScopedMessage::ScopedMessage( ScopedMessage&& old ) noexcept:
m_info( std::move( old.m_info ) ) {
m_info( CATCH_MOVE( old.m_info ) ) {
old.m_moved = true;
}

Expand Down
9 changes: 5 additions & 4 deletions src/catch2/catch_registry_hub.cpp
Expand Up @@ -19,6 +19,7 @@
#include <catch2/catch_test_case_info.hpp>
#include <catch2/internal/catch_noncopyable.hpp>
#include <catch2/interfaces/catch_interfaces_reporter_factory.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>

namespace Catch {

Expand Down Expand Up @@ -48,16 +49,16 @@ namespace Catch {

public: // IMutableRegistryHub
void registerReporter( std::string const& name, IReporterFactoryPtr factory ) override {
m_reporterRegistry.registerReporter( name, std::move(factory) );
m_reporterRegistry.registerReporter( name, CATCH_MOVE(factory) );
}
void registerListener( IReporterFactoryPtr factory ) override {
m_reporterRegistry.registerListener( std::move(factory) );
m_reporterRegistry.registerListener( CATCH_MOVE(factory) );
}
void registerTest( Detail::unique_ptr<TestCaseInfo>&& testInfo, Detail::unique_ptr<ITestInvoker>&& invoker ) override {
m_testCaseRegistry.registerTest( std::move(testInfo), std::move(invoker) );
m_testCaseRegistry.registerTest( CATCH_MOVE(testInfo), CATCH_MOVE(invoker) );
}
void registerTranslator( Detail::unique_ptr<IExceptionTranslator>&& translator ) override {
m_exceptionTranslatorRegistry.registerTranslator( std::move(translator) );
m_exceptionTranslatorRegistry.registerTranslator( CATCH_MOVE(translator) );
}
void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) override {
m_tagAliasRegistry.add( alias, tag, lineInfo );
Expand Down
2 changes: 1 addition & 1 deletion src/catch2/catch_section_info.hpp
Expand Up @@ -23,7 +23,7 @@ namespace Catch {
// still use the `-c` flag comfortably.
SectionInfo( SourceLineInfo const& _lineInfo, std::string _name,
const char* const = nullptr ):
name(std::move(_name)),
name(CATCH_MOVE(_name)),
lineInfo(_lineInfo)
{}

Expand Down
5 changes: 3 additions & 2 deletions src/catch2/catch_session.cpp
Expand Up @@ -21,6 +21,7 @@
#include <catch2/reporters/catch_reporter_listening.hpp>
#include <catch2/interfaces/catch_interfaces_reporter_registry.hpp>
#include <catch2/interfaces/catch_interfaces_reporter_factory.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>

#include <algorithm>
#include <iomanip>
Expand Down Expand Up @@ -57,7 +58,7 @@ namespace Catch {
explicit TestGroup(IStreamingReporterPtr&& reporter, Config const* config):
m_reporter(reporter.get()),
m_config{config},
m_context{config, std::move(reporter)} {
m_context{config, CATCH_MOVE(reporter)} {

auto const& allTestCases = getAllTestCasesSorted(*m_config);
m_matches = m_config->testSpec().matchesByFilter(allTestCases, *m_config);
Expand Down Expand Up @@ -277,7 +278,7 @@ namespace Catch {
return 0;
}

TestGroup tests { std::move(reporter), m_config.get() };
TestGroup tests { CATCH_MOVE(reporter), m_config.get() };
auto const totals = tests.execute();

if( m_config->warnAboutNoTests() && totals.error == -1 )
Expand Down

0 comments on commit d2ee710

Please sign in to comment.