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

Class BenchmarkFunction

test/expected/catch.hpp:6645–6706  ·  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

6643 /// This is effectively an implementation of std::function without any such optimizations;
6644 /// it may be slow, but it is consistently slow.
6645 struct BenchmarkFunction {
6646 private:
6647 struct callable {
6648 virtual void call(Chronometer meter) const = 0;
6649 virtual callable* clone() const = 0;
6650 virtual ~callable() = default;
6651 };
6652 template <typename Fun>
6653 struct model : public callable {
6654 model(Fun&& fun) : fun(std::move(fun)) {}
6655 model(Fun const& fun) : fun(fun) {}
6656
6657 model<Fun>* clone() const override { return new model<Fun>(*this); }
6658
6659 void call(Chronometer meter) const override {
6660 call(meter, is_callable<Fun(Chronometer)>());
6661 }
6662 void call(Chronometer meter, std::true_type) const {
6663 fun(meter);
6664 }
6665 void call(Chronometer meter, std::false_type) const {
6666 meter.measure(fun);
6667 }
6668
6669 Fun fun;
6670 };
6671
6672 struct do_nothing { void operator()() const {} };
6673
6674 template <typename T>
6675 BenchmarkFunction(model<T>* c) : f(c) {}
6676
6677 public:
6678 BenchmarkFunction()
6679 : f(new model<do_nothing>{ {} }) {}
6680
6681 template <typename Fun,
6682 typename std::enable_if<!is_related<Fun, BenchmarkFunction>::value, int>::type = 0>
6683 BenchmarkFunction(Fun&& fun)
6684 : f(new model<typename std::decay<Fun>::type>(std::forward<Fun>(fun))) {}
6685
6686 BenchmarkFunction(BenchmarkFunction&& that)
6687 : f(std::move(that.f)) {}
6688
6689 BenchmarkFunction(BenchmarkFunction const& that)
6690 : f(that.f->clone()) {}
6691
6692 BenchmarkFunction& operator=(BenchmarkFunction&& that) {
6693 f = std::move(that.f);
6694 return *this;
6695 }
6696
6697 BenchmarkFunction& operator=(BenchmarkFunction const& that) {
6698 f.reset(that.f->clone());
6699 return *this;
6700 }
6701
6702 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