CppCon 2017: Pablo Halpern “Allocators: The Good Parts”

CppCon · 24K views · 2017 · 1:00:49

Open on YouTube

C, C++, Rust, Go & systems languagesSoftware performance & optimizationtalk

Why Stack Overflow recommends it

Stack Overflow users link it when asking or answering questions about allocator-aware containers, polymorphic allocators and propagation of allocators to nested containers.

Recommended by 1 people across 1 different months, 2018 to 2023.

What it is

Pablo Halpern, a C++ standards committee member, gives a CppCon 2017 talk that works as a user manual for C++ allocators. It shows the simplest way to design allocator-aware classes, walks through writing a real allocator, and covers C++17 features such as polymorphic allocators that can be adapted to C++11 and C++14.

What people said

Recommends Oussama Ben Ghorbel · 2021-02 · Stack Overflow · on “Custom allocator for nested containers”

Creating a scoped allocator in C++11/C++14 was a little bit challenging. So I opted for a very modern solution introduced in C++17. Instead of implementing an allocator, I used polymorphic_allocator [LINK] . Polymorphic allocators are scoped allocators, standard containers will automatically pass the allocators to sub-objects. Basically, the idea was to use a polymorphic allocator and inject it with monotonic_buffer_resource [LINK] . The monotonic_buffer_resource can be initialized with a memory resource [LINK] . Writing a custom memory resource was very simple: class custom_resource : public std::pmr::memory_resource { public: explicit custom_resource(std::pmr::memory_resource *up = std::pm

Cites BiagioF · 2018-06 · Stack Overflow · on “Allocators For Nested Container”

Following a talk [this video] about allocators at CppCon , I came across the following piece of code [LINK] : #include <iostream> #include <string> #include <utility> #include <vector> namespace { template <typename T> class MyAllocator { public: using value_type = T; MyAllocator(std::string iType) : _type(std::move(iType)) {} T* allocate(const std::size_t iNo) { return new T[iNo]; } void deallocate(T* iPtr, const std::size_t) { delete[] iPtr; } constexpr bool operator!=(const MyAllocator& oth) const { return _type != oth._type; } const std::string& getType() const noexcept { return _type; } private: std::string _type; }; using MyString = std::basic_string<char, std::char_traits<char>, MyAllocato

Cites glades · 2022-08 · Stack Overflow · on “Why are my pmr-aware containers copied instead of moved?”

I'm trying out these new pmr thingies, and stuff seems really interesting. So in my example I'm hooking up a monotonic_buffer_resource to a buffer and overlay it with a logging resource to gain insight on what allocations are going on. Then I'm creating a pmr::vector using that resource and pushing back my pmr aware container type (which is essentially just a wrapper over a pmr::string . Now I initialize the temporaries in the vector initializer list with my memory resource and pass them to std::vector. What I thought would happen was that vector would move these objects in place, instead they're copied ! I thought that temporaries are always flagged as rvalues when passed into constructors,

Cites glades · 2022-09 · Stack Overflow · on “Is there something similar to the copy-and-swap idiom when allocators are involved?”

Special case: PMR aware containers I've been dealing with pmr containers over the last few months and I really came to like them. So in the manner of @Howard Hinnant I made my own little boilerplate cheatsheed for a most efficient pmr-aware container in the hopes that somebody might find this helpful, as there are many things which need to be considered (and I don't even know if that's all of it...). As we know we will only be dealing with a hardcoded std::byte allocator, we can make certain assumptions, most notably we never propagate allocators (except for DLS move see below)! This saves us from the checks. Here's my current implementation: Demo [LINK] #include <memory_resource> struct pmr

Cites glades · 2023-06 · Stack Overflow · on “Authoring a container to work with both c++11 and pmr allocators”

How do I correctly create a container that works with both, C++11 and C++17 polymorphic allocators? Here's what I have so far (as a generic boilerplate template): Explanation : I've included two fields, res_ which shows how dynamic memory is managed directly from the container, whereas field vec_ is used to demonstrate how the allocator propagates downwards. I've taken a lot from Pablo Halpern's talk Allocators: The Good Parts [this video] but he mainly talks about pmr allocators, not the c++11 ones. Demo [LINK] #include <cstdio> #include <vector> #include <memory> #include <memory_resource> template <typename T, typename Allocator = std::allocator<T>> struct MyContainer { auto get_allocator() co

Cites Turtlefight · 2023-06 · Stack Overflow · on “Authoring a container to work with both c++11 and pmr allocators”

tl;dr All standard library containers must be given an allocator with a value_type that is the same as the value_type of the container; otherwise it would be ill-formed. So in this case one would need to either use a std::pmr::polymorphic_allocator<std::string> for MyContainer , or rebind the allocator type before passing it to std::vector , e.g.: // option 1 MyContainer<std::string, std::pmr::polymorphic_allocator<std::string>> ctr1 = /* ... */; // option 2 template <class T, class Allocator = std::allocator<T>> struct MyContainer { // ... private: std::vector<T, typename std::allocator_traits<Allocator>::template rebind_alloc<T>> vec_; }; MyContainer<std::string, std::pmr::polymorphic_allo

Cites glades · 2023-06 · Stack Overflow · on “Authoring a container to work with both c++11 and pmr allocators”

@HolyBlackCat Pablo Halpern mentions it ( [this video] ). However I see that pmr::vector uses the type as value type. Confusing.