Test | Time | Note |
---|---|---|
functor | 191 ns | Baseline that's the best we could do: a hand-crafted functor |
sf32 | 312 ns | This is big enough to store 2 ints |
sf64 | 369 ns | |
sf128 | 346 ns | |
sf256 | 376 ns | |
sf512 | 503 ns | |
sf1024 | 569 ns | |
sf2048 | 870 ns |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
virtual void copy(void* memory) const { | |
new (memory) Model<F, ReturnType, Xs...>(f); | |
} | |
template<unsigned rhsSize, std::enable_if_t<(rhsSize <= size), bool> = 0> | |
SmallFun(SmallFun<ReturnType(Xs...), rhsSize> const& rhs) { | |
rhs.copy(memory); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<class ReturnType, class...Xs> | |
class SmallFun<ReturnType(Xs...)> { | |
char memory[SIZE]; | |
public: | |
template<class F> | |
SmallFun(F const& f) | |
: new (memory) Model<F, ReturnType, Xs...> { | |
assert( sizeof(Model<F, ReturnType, Xs...>) < SIZE ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
char memorypool[64]; | |
int* a = new (memorypool) int[4]; | |
int* b = new (memorypool + sizeof(int) * 4) int[4]; | |
assert( (void*)a[0] == (void*)memorypool[0] ); | |
assert( (void*)b[0] == (void*)memorypool[32] ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<class ReturnType, class...Xs> | |
class Function<ReturnType(Xs...)> { | |
std::shared_ptr<Concept<ReturnType,Xs...>> pimpl; | |
public: | |
Function() {} | |
template<class F> | |
Function(F const& f) | |
: pimpl(new SFModel<F, ReturnType, Xs...> ) // heap allocation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
auto lambda = [](int x) { return x; }; | |
using lambdaType = decltype(lambda); | |
SFConcept<int, int>* functor = new Model<lambdaType, int, int>(lambda); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<class F, class ReturnType, class...Xs> | |
struct Model final : Concept<ReturnType, Xs...> { | |
F f; | |
Model(F const& f) | |
: f(f) {} | |
virtual ReturnType operator()(Xs...xs) const { | |
return f(xs...); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<class ReturnType, class...Xs> | |
struct Concept { | |
virtual ReturnType operator()(Xs...) const = 0; | |
virtual ReturnType operator()(Xs...) = 0; | |
virtual ~Concept() {}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void stdFunction(benchmark::State& state) { | |
unsigned N = 100; | |
std::vector<std::function<unsigned(int const j)>> fs(N); | |
std::vector<int> r(N); | |
while (state.KeepRunning()) { | |
for (int i = 0; i < N; ++i) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A SmallFun with a size of 64 bytes | |
SmallFun<unsigned(int const j), 64> f = [i, N] (int j) { | |
return i * j + N; | |
}; |