the-moisrex / webpp

C++ web framework | web development can be done with C++ as well.
https://t.me/webpp
MIT License
133 stars 9 forks source link

stack_allocator for small object optimization #153

Open the-moisrex opened 2 years ago

the-moisrex commented 2 years ago

A stack_allocator that can get other allocators as template parameter and use the stack first and when it ran out of stack, uses the parent allocator. Parent allocator should be defaulted to std::allocator so it doesn't hold any burden on the stack size.

The usage is something like this:

  using allocator_type = stack_allocator<std::allocator<void(*)()>, 1>; // stack size = 1 * sizeof(void(*)())
  using func = istl::function<void(), allocator_type>;
  using vec_alloc = stack_allocator<std::allocator<func>, 5>; // a stack good for 5 functions
  vector<func, vec_alloc> routes;

The point of this is to put the burden of implementing Small Object Optimization technique on the allocator and not the object itself.

See also:

the-moisrex commented 11 months ago

C++23 has spanstream. Tweet