MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / BenchmarkFunction

Class BenchmarkFunction

Source/ThirdParty/catch2/catch.hpp:6701–6762  ·  view source on GitHub ↗

We need to reinvent std::function because every piece of code that might add overhead in a measurement context needs to have consistent performance characteristics so that we can account for it in the measurement. Implementations of std::function with optimizations that aren't always applicable, like small buffer optimizations, are not uncommon. This is effectively an implementation of std::functi

Source from the content-addressed store, hash-verified

6699 /// This is effectively an implementation of std::function without any such optimizations;
6700 /// it may be slow, but it is consistently slow.
6701 struct BenchmarkFunction {
6702 private:
6703 struct callable {
6704 virtual void call(Chronometer meter) const = 0;
6705 virtual callable* clone() const = 0;
6706 virtual ~callable() = default;
6707 };
6708 template <typename Fun>
6709 struct model : public callable {
6710 model(Fun&& fun) : fun(std::move(fun)) {}
6711 model(Fun const& fun) : fun(fun) {}
6712
6713 model<Fun>* clone() const override { return new model<Fun>(*this); }
6714
6715 void call(Chronometer meter) const override {
6716 call(meter, is_callable<Fun(Chronometer)>());
6717 }
6718 void call(Chronometer meter, std::true_type) const {
6719 fun(meter);
6720 }
6721 void call(Chronometer meter, std::false_type) const {
6722 meter.measure(fun);
6723 }
6724
6725 Fun fun;
6726 };
6727
6728 struct do_nothing { void operator()() const {} };
6729
6730 template <typename T>
6731 BenchmarkFunction(model<T>* c) : f(c) {}
6732
6733 public:
6734 BenchmarkFunction()
6735 : f(new model<do_nothing>{ {} }) {}
6736
6737 template <typename Fun,
6738 typename std::enable_if<!is_related<Fun, BenchmarkFunction>::value, int>::type = 0>
6739 BenchmarkFunction(Fun&& fun)
6740 : f(new model<typename std::decay<Fun>::type>(std::forward<Fun>(fun))) {}
6741
6742 BenchmarkFunction(BenchmarkFunction&& that)
6743 : f(std::move(that.f)) {}
6744
6745 BenchmarkFunction(BenchmarkFunction const& that)
6746 : f(that.f->clone()) {}
6747
6748 BenchmarkFunction& operator=(BenchmarkFunction&& that) {
6749 f = std::move(that.f);
6750 return *this;
6751 }
6752
6753 BenchmarkFunction& operator=(BenchmarkFunction const& that) {
6754 f.reset(that.f->clone());
6755 return *this;
6756 }
6757
6758 void operator()(Chronometer meter) const { f->call(meter); }

Callers 1

catch.hppFile · 0.85

Calls 2

resetMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected