Skip to content

Instantly share code, notes, and snippets.

View njlr's full-sized avatar
🌏
F# ing

njlr njlr

🌏
F# ing
View GitHub Profile
// ...
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);
  }
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 ); 
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] );
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
auto lambda = [](int x) { return x; };
using lambdaType = decltype(lambda);
SFConcept<int, int>* functor = new Model<lambdaType, int, int>(lambda);
@njlr
njlr / model.cpp
Created September 19, 2017 11:34
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...);
template<class ReturnType, class...Xs>
struct Concept {
 virtual ReturnType operator()(Xs...) const = 0;
  virtual ReturnType operator()(Xs...) = 0;
  virtual ~Concept() {};
};
@njlr
njlr / test.cpp
Created September 19, 2017 11:30
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) {
@njlr
njlr / results.md
Last active September 19, 2017 11:28
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
// A SmallFun with a size of 64 bytes
SmallFun<unsigned(int const j), 64> f = [i, N] (int j) {
 return i * j + N;
};