C++17 will bring us std::pmr::memory_resource
which is a clean interface for allocating and deallocating memory. Unlike the Allocator concept, it does just that and nothing more. There will also be std::pmr::polymorphic_allocator
which wraps a memory resource into a classical allocator so it can be used with existing containers.
If I'm about to write a new container (or other memory-hungry) type targeting C++17 and later, should I continue programming against the Allocator concept or rather use the newer and cleaner abstraction directly?
As of now, my thoughts go like this.
Reasons to continue using allocators:
- It is consistent with the standard library and existing code. Even the new
std::pmr::*
container aliases continue to use allocators. - Since a memory resource can be wrapped into a
std::pmr::polymorphic_allocator
, the allocator interface is more general and satisfies the needs of more clients. - Memory resources always use run-time polymorphism so they have a minor additional run-time overhead compared to the zero-overhead abstraction that allocators can provide.
- Maybe somebody actually needs the other parts of the allocator interface (such as custom pointer types) which cannot be provided by a pure memory resource.
Reasons to start using memory resources instead of allocators:
- The allocator interface is clunky and hard to implement. The
std::pmr::memory_resource
interface is clean and straight-forward. - Since memory resources are polymorphic, they don't affect the type of the container which means fewer template instantiations (and therefore maybe faster compiles and smaller executables) and enables us to move more code into separate translation units.
- If an object uses a memory resource, it can always instantiate a sub-object that still uses allocators by wrapping the memory resource into a
std::pmr::polymorphic_allocator
. The other way round is more difficult. - Memory allocation is a relatively work-intensive task anyway. A single virtual function call doesn't add much overhead, relatively speaking.
Do there already exist any recommendations for how to use the new library feature effectively?