CppCon 2019: Getting Allocators out of Our Way

Alisdair Meredith and Pablo Halpern

Allocators are great

std::pmr::monotoic_buffer_resource src(buffer, sizeof (buffer);
pmr::set. uniq(&src);

What if we could reduce the cost

goal: reduce cost of developign an allocator aware

What’s our problem?

Allocators bloat class interfaces

Performance in practice

  • Memory hiearchy: Cache; main, virtual
  • objects used together should be together

C++ allocatros

allocator_traits simplify allocators; mostly for users. Containers now have problems in their face

What about nested containers

  • In a vector<string> vector has one and each string potentiallly different allocator
  • scoped allocator model: container passes allocator into each item. Solves memory diffisuion
  • scoped_allocator_adapter
  • Allocator template policies interfere with vocabulary types

C++17: PMR: a simpler allocator model

  • Non-template std::pmr::memoryresource
  • The wrapper
  • class memory_resource
  • polomorphic_allocator
  • polymorphic_allocator<byte>
    • “One true” allocator vocabulary type
    • Only a template to be compatible for C++11
  • std::pmr::new_delete_resource()
    • Uses new/delete
    • always availabel
    • thread safe
  • unsynchronized_pool_resource
    • Pools of similar sized objects
    • good for dynamic data strucures
    • Single-threaded
    • avoids concurrency lock overhead
  • monootonic_buffer_resource
    • Ultra fast; single threaded, contiguous
    • for containers that grow monotonically
  • test resource
    • GitHub bloomberg/p1160

How to use allocators

class Student {
  pmr::string d_name;
pmr::string d_email
pmr::vector<int d_grades;

using allocator_type = pmr::polymovic_allocator<>; // Important


  • Overload each constructor having default_arguments
  • Variadic argument lists: allocator goes at the beginning

Rewriting std::unordered_map to use pmr allocators

  • Example with copying constructors. Lots of duplicated code

What about a language solution

  • Build on a strong foundation built up to C++17

HashMap<int,int> x {} using myAllocator;

  • using myAllocator


  • Bloomberg vision 2020

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.