std::pmr::memory_resource
object is to use it to construct a std::pmr::polymorphic_allocator
object, then use it in a std::pmr
container.std::pmr::memory_resource
with a stateless allocator Common usage:
std::pmr::memory_resource
→ std::pmr::polymorphic_allocator
→
std::pmr
container
Alternative:
std::pmr::memory_resource
→
stateless allocator →
container
using stateless allocator
std::pmr::memory_resource
sstd::pmr::memory_resource
Memory resources implement memory allocation strategies that can be used by std::pmr::polymorphic_allocator
std::pmr::new_delete_resource
: global operator new and operator deletestd::pmr::null_memory_resource
: throws on allocatestd::pmr::(un)synchronized_pool_resource
: single/multi threaded, object pool pattern
pool resource figurestd::pmr::monotonic_buffer_resource
: single threaded bump allocatorstd::pmr
container uses a std::pmr::polymorphic_allocator
.std::pmr::vector
alias template as an example:
std::pmr::polymorphic_allocator
is a wrapper for std::pmr::memory_resource
malloc
malloc/free: jemalloc/TCMalloc/mimallocstd::allocator
(de)allocate
member functions to use
that pointer for (de)allocation
std::pmr::memory_resource
object and initialize that global pointer to point to it
std::pmr::polymorphic_allocator
s, but observed
performance regression
std::pmr::memory_resource
method, with unmodified
nlohmann json, I observed a 30% decrease in parse time
std::pmr
containersstd::vector
of std::array
of std::string
std::pmr::memory_resource
,
godbolt
std::pmr::memory_resource
std::pmr::memory_resource
final
keyword on the structallocate
/deallocate
non virtual functions
-static-libstdc++
) or
Benchmark adapted from: https://en.cppreference.com/w/cpp/memory/monotonic_buffer_resource
Repeated push_back
on std::list
std::allocator |
pmr::polymorphic_allocator with pmr::new_delete_resource |
stateless allocator with pmr::new_delete_resource |
---|---|---|
baseline | 1.6x | 1.0x |
pmr::monotonic_buffer_resource
2x gains with stateless allocator vs pmr::polymorphic_allocator
Devirtualization helps with performance, but performance is more than the speed of allocation/deallocation
With space and compute advantages of stateless allocator, it is unlikely to observe a regression compared to using a pmr::polymorphic_allocator