Slide 1

Slide 1 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 1/110 Most Malleable Memory Management Method Björn Fahller

Slide 2

Slide 2 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 2/110 Most Malleable Memory Management Method

Slide 3

Slide 3 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 3/110 Most Malleable Memory Management Method

Slide 4

Slide 4 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 4/110 Most Malleable Memory Management Method

Slide 5

Slide 5 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 5/110 Most Malleable Memory Management Method

Slide 6

Slide 6 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 6/110 The heap Allows you to... – Allocate objects of any size and alignment – Allocate and deallocate from any thread ● Even allocate in one thread and deallocate in another – Let objects lifetimes vary wildly

Slide 7

Slide 7 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 7/110 The heap Allows you to... – Allocate objects of any size and alignment – Allocate and deallocate from any thread ● Even allocate in one thread and deallocate in another – Let objects lifetimes vary wildly Complex structure with lookup time and space overhead

Slide 8

Slide 8 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 8/110 The heap Allows you to... – Allocate objects of any size and alignment – Allocate and deallocate from any thread ● Even allocate in one thread and deallocate in another – Let objects lifetimes vary wildly Poor locality of reference

Slide 9

Slide 9 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 9/110 The heap Allows you to... – Allocate objects of any size and alignment – Allocate and deallocate from any thread ● Even allocate in one thread and deallocate in another – Let objects lifetimes vary wildly Fragmentation may make everything worse over time

Slide 10

Slide 10 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 10/110 The heap Allows you to... – Allocate objects of any size and alignment – Allocate and deallocate from any thread ● Even allocate in one thread and deallocate in another – Let objects lifetimes vary wildly Synchronisation overhead

Slide 11

Slide 11 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 11/110 So you turn to allocators...

Slide 12

Slide 12 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 12/110 So you turn to allocators... https://www.edvardmunch.org/the-scream.jsp

Slide 13

Slide 13 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 13/110 So you turn to allocators... https://www.edvardmunch.org/the-scream.jsp But PMR allocators suck less

Slide 14

Slide 14 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 14/110 Polymorphic Memory Resource “container” PMR allocator <>

Slide 15

Slide 15 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 15/110 Polymorphic Memory Resource “container” PMR allocator <> A “container” has a PMR allocator

Slide 16

Slide 16 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 16/110 Polymorphic Memory Resource “container” PMR allocator <> A “container” has a PMR allocator A PMR allocator references a memory resource

Slide 17

Slide 17 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 17/110 Polymorphic Memory Resource “container” PMR allocator <> A “container” has a PMR allocator A PMR allocator references a memory resource Standard containers are conveniently available under namespace std::pmr, e.g. std::pmr::vector

Slide 18

Slide 18 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 18/110 memory_resource class std::pmr::memory_resource { public:         void* allocate(size_t bytes, size_t alignment);         void deallocate(void* addr, size_t bytes, size_t alignment);         bool is_equal(const memory_resource&) const noexcept; private:         virtual void* do_allocate(size_t bytes, size_t align) = 0;         virtual void do_deallocate(void* addr, size_t bytes, size_t align) = 0;         virtual bool do_is_equal(const memory_resource&) const noexcept = 0; };

Slide 19

Slide 19 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 19/110 memory_resource class std::pmr::memory_resource { public:         void* allocate(size_t bytes, size_t alignment);         void deallocate(void* addr, size_t bytes, size_t alignment);         bool is_equal(const memory_resource&) const noexcept; private:         virtual void* do_allocate(size_t bytes, size_t align) = 0;         virtual void do_deallocate(void* addr, size_t bytes, size_t align) = 0;         virtual bool do_is_equal(const memory_resource&) const noexcept = 0; }; Inherit and implement these

Slide 20

Slide 20 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 20/110 memory_resource class std::pmr::memory_resource { public:         void* allocate(size_t bytes, size_t alignment);         void deallocate(void* addr, size_t bytes, size_t alignment);         bool is_equal(const memory_resource&) const noexcept; private:         virtual void* do_allocate(size_t bytes, size_t align) = 0;         virtual void do_deallocate(void* addr, size_t bytes, size_t align) = 0;         virtual bool do_is_equal(const memory_resource&) const noexcept = 0; }; Mildly annoying that alignment uses size_t and not align_val_t Inherit and implement these

Slide 21

Slide 21 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 21/110 memory_resource class tracing_resource final : public std::pmr::memory_resource { public:     tracing_resource(std::ostream& os) : os_(os) {} private:     void* do_allocate(size_t bytes, size_t align) override    {         auto addr = operator new (bytes, std::align_val_t(align));         os_ << "allocate(" << bytes << ", " << align << ") -> " << addr << '\n';         return addr;     }     void do_deallocate(void* addr, size_t bytes, size_t align) override {         os_ << "deallocate(" << addr << ", " << bytes << ", " << align << ")\n";         operator delete(addr, bytes, std::align_val_t(align));     }     bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {         return &other == this;     }     std::ostream& os_; };

Slide 22

Slide 22 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 22/110 memory_resource class tracing_resource final : public std::pmr::memory_resource { public:     tracing_resource(std::ostream& os) : os_(os) {} private:     void* do_allocate(size_t bytes, size_t align) override    {         auto addr = operator new (bytes, std::align_val_t(align));         os_ << "allocate(" << bytes << ", " << align << ") -> " << addr << '\n';         return addr;     }     void do_deallocate(void* addr, size_t bytes, size_t align) override {         os_ << "deallocate(" << addr << ", " << bytes << ", " << align << ")\n";         operator delete(addr, bytes, std::align_val_t(align));     }     bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {         return &other == this;     }     std::ostream& os_; };

Slide 23

Slide 23 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 23/110 memory_resource class tracing_resource final : public std::pmr::memory_resource { public:     tracing_resource(std::ostream& os) : os_(os) {} private:     void* do_allocate(size_t bytes, size_t align) override    {         auto addr = operator new (bytes, std::align_val_t(align));         os_ << "allocate(" << bytes << ", " << align << ") -> " << addr << '\n';         return addr;     }     void do_deallocate(void* addr, size_t bytes, size_t align) override {         os_ << "deallocate(" << addr << ", " << bytes << ", " << align << ")\n";         operator delete(addr, bytes, std::align_val_t(align));     }     bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {         return &other == this;     }     std::ostream& os_; };

Slide 24

Slide 24 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 24/110 memory_resource class tracing_resource final : public std::pmr::memory_resource { public:     tracing_resource(std::ostream& os) : os_(os) {} private:     void* do_allocate(size_t bytes, size_t align) override    {         auto addr = operator new (bytes, std::align_val_t(align));         os_ << "allocate(" << bytes << ", " << align << ") -> " << addr << '\n';         return addr;     }     void do_deallocate(void* addr, size_t bytes, size_t align) override {         os_ << "deallocate(" << addr << ", " << bytes << ", " << align << ")\n";         operator delete(addr, bytes, std::align_val_t(align));     }     bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {         return &other == this;     }     std::ostream& os_; };

Slide 25

Slide 25 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 25/110 memory_resource class tracing_resource final : public std::pmr::memory_resource { public:     tracing_resource(std::ostream& os) : os_(os) {} private:     void* do_allocate(size_t bytes, size_t align) override    {         auto addr = operator new (bytes, std::align_val_t(align));         os_ << "allocate(" << bytes << ", " << align << ") -> " << addr << '\n';         return addr;     }     void do_deallocate(void* addr, size_t bytes, size_t align) override {         os_ << "deallocate(" << addr << ", " << bytes << ", " << align << ")\n";         operator delete(addr, bytes, std::align_val_t(align));     }     bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {         return &other == this;     }     std::ostream& os_; };

Slide 26

Slide 26 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 26/110 https://godbolt.org/z/P7jojKdTf

Slide 27

Slide 27 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 27/110 Most Malleable Memory Management Method It is very important that your memory resource instance outlives all containers that use it.

Slide 28

Slide 28 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 28/110 Most Malleable Memory Management Method It is very important that your memory resource instance outlives all containers that use it. It’s very easy to miss using a PMR type, and not so easy to detect the mistake

Slide 29

Slide 29 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 29/110 Most Malleable Memory Management Method It is very important that your memory resource instance outlives all containers that use it. It’s very easy to miss using a PMR type, and not so easy to detect the mistake There are tools, though

Slide 30

Slide 30 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 30/110 string frequency example class histogram { public:     void add(const std::string& word) {         ++words_[word];     }     void print_top(size_t n, std::ostream& os) const {         using count = std::pair;         std::vector popular(words_.begin(), words_.end());         std::ranges::partial_sort(popular,                                                   popular.begin() + n, std::greater{}, &count::second);         for (const auto& stat : popular | std::ranges::views::take(n)) {             os << stat.first << '\t' << stat.second << '\n';         }     } private:     std::unordered_map words_; };

Slide 31

Slide 31 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 31/110 string frequency example class histogram { public:     void add(const std::string& word) {         ++words_[word];     }     void print_top(size_t n, std::ostream& os) const {         using count = std::pair;         std::vector popular(words_.begin(), words_.end());         std::ranges::partial_sort(popular,                                                   popular.begin() + n, std::greater{}, &count::second);         for (const auto& stat : popular | std::ranges::views::take(n)) {             os << stat.first << '\t' << stat.second << '\n';         }     } private:     std::unordered_map words_; };

Slide 32

Slide 32 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 32/110 string frequency example class histogram { public:     void add(const std::string& word) {         ++words_[word];     }     void print_top(size_t n, std::ostream& os) const {         using count = std::pair;         std::vector popular(words_.begin(), words_.end());         std::ranges::partial_sort(popular,                                                   popular.begin() + n, std::greater{}, &count::second);         for (const auto& stat : popular | std::ranges::views::take(n)) {             os << stat.first << '\t' << stat.second << '\n';         }     } private:     std::unordered_map words_; };

Slide 33

Slide 33 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 33/110 string frequency example class histogram { public:     void add(const std::string& word) {         ++words_[word];     }     void print_top(size_t n, std::ostream& os) const {         using count = std::pair;         std::vector popular(words_.begin(), words_.end());         std::ranges::partial_sort(popular,                                                   popular.begin() + n, std::greater{}, &count::second);         for (const auto& stat : popular | std::ranges::views::take(n)) {             os << stat.first << '\t' << stat.second << '\n';         }     } private:     std::unordered_map words_; };

Slide 34

Slide 34 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 34/110 string frequency example class histogram { public:     void add(const std::string& word) {         ++words_[word];     }     void print_top(size_t n, std::ostream& os) const {         using count = std::pair;         std::vector popular(words_.begin(), words_.end());         std::ranges::partial_sort(popular,                                                   popular.begin() + n, std::greater{}, &count::second);         for (const auto& stat : popular | std::ranges::views::take(n)) {             os << stat.first << '\t' << stat.second << '\n';         }     } private:     std::unordered_map words_; };

Slide 35

Slide 35 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 35/110 string frequency example class histogram { public:     void add(const std::string& word) {         ++words_[word];     }     void print_top(size_t n, std::ostream& os) const {         using count = std::pair;         std::vector popular(words_.begin(), words_.end());         std::ranges::partial_sort(popular,                                                   popular.begin() + n, std::greater{}, &count::second);         for (const auto& stat : popular | std::ranges::views::take(n)) {             os << stat.first << '\t' << stat.second << '\n';         }     } private:     std::unordered_map words_; };

Slide 36

Slide 36 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 36/110 string frequency example class histogram { public:     void add(const std::string& word) {         ++words_[word];     }     void print_top(size_t n, std::ostream& os) const {         using count = std::pair;         std::vector popular(words_.begin(), words_.end());         std::ranges::partial_sort(popular,                                                   popular.begin() + n, std::greater{}, &count::second);         for (const auto& stat : popular | std::ranges::views::take(n)) {             os << stat.first << '\t' << stat.second << '\n';         }     } private:     std::unordered_map words_; };

Slide 37

Slide 37 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 37/110 string frequency example class histogram { public:     void add(const std::string& word) {         ++words_[word];     }     void print_top(size_t n, std::ostream& os) const {         using count = std::pair;         std::vector popular(words_.begin(), words_.end());         std::ranges::partial_sort(popular,                                                   popular.begin() + n, std::greater{}, &count::second);         for (const auto& stat : popular | std::ranges::views::take(n)) {             os << stat.first << '\t' << stat.second << '\n';         }     } private:     std::unordered_map words_; };

Slide 38

Slide 38 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 38/110 string frequency example class histogram { public:     void add(const std::string& word) {         ++words_[word];     }     void print_top(size_t n, std::ostream& os) const {         using count = std::pair;         std::vector popular(words_.begin(), words_.end());         std::ranges::partial_sort(popular,                                                   popular.begin() + n, std::greater{}, &count::second);         for (const auto& stat : popular | std::ranges::views::take(n)) {             os << stat.first << '\t' << stat.second << '\n';         }     } private:     std::unordered_map words_; }; int main() { std::string word; histogram hist; while (std::cin >> word) { hist.add(word); } hist.print_top(5, std::cout); }

Slide 39

Slide 39 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 39/110 Live Demo

Slide 40

Slide 40 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 40/110 Most Malleable Memory Management Method Nothing surprising here, but good to have a baseline and a tool Let’s PMR it!

Slide 41

Slide 41 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 41/110 PMR histogram class histogram { public: histogram(const std::pmr::polymorphic_allocator<>& alloc = {}): words_(alloc){} void add(const std::pmr::string& word) { ++words_[word]; } void print_top(size_t n, std::ostream& os) const { using count = std::pair; std::vector popular(words_.begin(), words_.end()); std::ranges::partial_sort(popular, popular.begin() + n, std::greater{}, &count::second); for (const auto& stat : popular | std::ranges::views::take(n)) { os << stat.first << '\t' << stat.second << '\n'; } } private: std::pmr::unordered_map words_; };

Slide 42

Slide 42 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 42/110 PMR histogram class histogram { public: histogram(const std::pmr::polymorphic_allocator<>& alloc = {}): words_(alloc){} void add(const std::pmr::string& word) { ++words_[word]; } void print_top(size_t n, std::ostream& os) const { using count = std::pair; std::vector popular(words_.begin(), words_.end()); std::ranges::partial_sort(popular, popular.begin() + n, std::greater{}, &count::second); for (const auto& stat : popular | std::ranges::views::take(n)) { os << stat.first << '\t' << stat.second << '\n'; } } private: std::pmr::unordered_map words_; }; PMR-ify the container and strings

Slide 43

Slide 43 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 43/110 PMR histogram class histogram { public: histogram(const std::pmr::polymorphic_allocator<>& alloc = {}): words_(alloc){} void add(const std::pmr::string& word) { ++words_[word]; } void print_top(size_t n, std::ostream& os) const { using count = std::pair; std::vector popular(words_.begin(), words_.end()); std::ranges::partial_sort(popular, popular.begin() + n, std::greater{}, &count::second); for (const auto& stat : popular | std::ranges::views::take(n)) { os << stat.first << '\t' << stat.second << '\n'; } } private: std::pmr::unordered_map words_; }; Get an allocator to use

Slide 44

Slide 44 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 44/110 PMR histogram class histogram { public: histogram(const std::pmr::polymorphic_allocator<>& alloc = {}): words_(alloc){} void add(const std::pmr::string& word) { ++words_[word]; } void print_top(size_t n, std::ostream& os) const { using count = std::pair; std::vector popular(words_.begin(), words_.end()); std::ranges::partial_sort(popular, popular.begin() + n, std::greater{}, &count::second); for (const auto& stat : popular | std::ranges::views::take(n)) { os << stat.first << '\t' << stat.second << '\n'; } } private: std::pmr::unordered_map words_; }; In C++17 it was necessary to express a type here, but in C++20 onwards, just use the diamond

Slide 45

Slide 45 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 45/110 PMR histogram class histogram { public: histogram(const std::pmr::polymorphic_allocator<>& alloc = {}): words_(alloc){} void add(const std::pmr::string& word) { ++words_[word]; } void print_top(size_t n, std::ostream& os) const { using count = std::pair; std::vector popular(words_.begin(), words_.end()); std::ranges::partial_sort(popular, popular.begin() + n, std::greater{}, &count::second); for (const auto& stat : popular | std::ranges::views::take(n)) { os << stat.first << '\t' << stat.second << '\n'; } } private: std::pmr::unordered_map words_; }; int main() { std::pmr::string word; histogram hist; while (std::cin >> word) { hist.add(word); } hist.print_top(5, std::cout); }

Slide 46

Slide 46 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 46/110 Live Demo

Slide 47

Slide 47 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 47/110 PMR histogram class histogram { public: histogram(const std::pmr::polymorphic_allocator<>& alloc = {}): words_(alloc){} void add(const std::pmr::string& word) { ++words_[word]; } void print_top(size_t n, std::ostream& os) const { using count = std::pair; std::vector popular(words_.begin(), words_.end()); std::ranges::partial_sort(popular, popular.begin() + n, std::greater{}, &count::second); for (const auto& stat : popular | std::ranges::views::take(n)) { os << stat.first << '\t' << stat.second << '\n'; } } private: std::pmr::unordered_map words_; }; int main() { std::pmr::string word; histogram hist; while (std::cin >> word) { hist.add(word); } hist.print_top(5, std::cout); }

Slide 48

Slide 48 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 48/110 PMR histogram class histogram { public: histogram(const std::pmr::polymorphic_allocator<>& alloc = {}): words_(alloc){} void add(const std::pmr::string& word) { ++words_[word]; } void print_top(size_t n, std::ostream& os) const { using count = std::pair; std::vector popular(words_.begin(), words_.end()); std::ranges::partial_sort(popular, popular.begin() + n, std::greater{}, &count::second); for (const auto& stat : popular | std::ranges::views::take(n)) { os << stat.first << '\t' << stat.second << '\n'; } } private: std::pmr::unordered_map words_; }; int main() { std::pmr::string word; histogram hist; while (std::cin >> word) { hist.add(word); } hist.print_top(5, std::cout); } Not much new, because a default constructed polymorphic_allocator uses the default memory_resource, which is new_delete_resource.

Slide 49

Slide 49 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 49/110 PMR histogram class histogram { public: histogram(const std::pmr::polymorphic_allocator<>& alloc = {}): words_(alloc){} void add(const std::pmr::string& word) { ++words_[word]; } void print_top(size_t n, std::ostream& os) const { using count = std::pair; std::vector popular(words_.begin(), words_.end()); std::ranges::partial_sort(popular, popular.begin() + n, std::greater{}, &count::second); for (const auto& stat : popular | std::ranges::views::take(n)) { os << stat.first << '\t' << stat.second << '\n'; } } private: std::pmr::unordered_map words_; }; int main() { std::pmr::string word; histogram hist; while (std::cin >> word) { hist.add(word); } hist.print_top(5, std::cout); } But we did see it being used!

Slide 50

Slide 50 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 50/110 Choosing a memory resource The header offers: std::pmr::new_delete_resource std::pmr::null_resource std::pmr::synchronized_pool_resource std::pmr::unsynchronized_pool_resource std::pmr::monotonic_buffer_resource

Slide 51

Slide 51 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 51/110 Choosing a memory resource The header offers: std::pmr::new_delete_resource std::pmr::null_resource std::pmr::synchronized_pool_resource std::pmr::unsynchronized_pool_resource std::pmr::monotonic_buffer_resource You’ve seen this one. Plain operator new and operator delete.

Slide 52

Slide 52 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 52/110 Choosing a memory resource The header offers: std::pmr::new_delete_resource std::pmr::null_resource std::pmr::synchronized_pool_resource std::pmr::unsynchronized_pool_resource std::pmr::monotonic_buffer_resource A resource that always fails to allocate. Can be useful when testing.

Slide 53

Slide 53 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 53/110 Choosing a memory resource The header offers: std::pmr::new_delete_resource std::pmr::null_resource std::pmr::synchronized_pool_resource std::pmr::unsynchronized_pool_resource std::pmr::monotonic_buffer_resource A thread safe pool of memory blocks, in which allocations are simple indexing operations into an available slot in a block.

Slide 54

Slide 54 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 54/110 Choosing a memory resource The header offers: std::pmr::new_delete_resource std::pmr::null_resource std::pmr::synchronized_pool_resource std::pmr::unsynchronized_pool_resource std::pmr::monotonic_buffer_resource Same as above, but without the overhead of thread synchronization. Very fast when you know allocation and deallocation will happen in the same thread.

Slide 55

Slide 55 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 55/110 Choosing a memory resource The header offers: std::pmr::new_delete_resource std::pmr::null_resource std::pmr::synchronized_pool_resource std::pmr::unsynchronized_pool_resource std::pmr::monotonic_buffer_resource Very fast resource, where each allocation is at a monotonically increasing space in the available buffer. Deallocation only happens when the resource is destroyed. Useful in very local scopes.

Slide 56

Slide 56 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 56/110 Choosing a memory resource The header offers: std::pmr::new_delete_resource std::pmr::null_resource std::pmr::synchronized_pool_resource std::pmr::unsynchronized_pool_resource std::pmr::monotonic_buffer_resource You can cascade memory resources. Construct with a pointer to an upstream resource

Slide 57

Slide 57 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 57/110 Choosing a memory resource The header offers: std::pmr::new_delete_resource std::pmr::null_resource std::pmr::synchronized_pool_resource std::pmr::unsynchronized_pool_resource std::pmr::monotonic_buffer_resource void do_work(std::pmr::polymorphic_allocator<> alloc); auto heap = std::pmr::new_delete_resource(); std::pmr::monotonic_buffer_resource monotonic_buffers(heap); std::pmr::unsynchronized_pool_resource pool(&monotonic_buffers); do_work(&pool);

Slide 58

Slide 58 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 58/110 Choosing a memory resource The header offers: std::pmr::new_delete_resource std::pmr::null_resource std::pmr::synchronized_pool_resource std::pmr::unsynchronized_pool_resource std::pmr::monotonic_buffer_resource void do_work(std::pmr::polymorphic_allocator<> alloc); auto heap = std::pmr::new_delete_resource(); std::pmr::monotonic_buffer_resource monotonic_buffers(heap); std::pmr::unsynchronized_pool_resource pool(&monotonic_buffers); do_work(&pool); Construct a polymorphic allocator using pool.

Slide 59

Slide 59 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 59/110 Choosing a memory resource The header offers: std::pmr::new_delete_resource std::pmr::null_resource std::pmr::synchronized_pool_resource std::pmr::unsynchronized_pool_resource std::pmr::monotonic_buffer_resource void do_work(std::pmr::polymorphic_allocator<> alloc); auto heap = std::pmr::new_delete_resource(); std::pmr::monotonic_buffer_resource monotonic_buffers(heap); std::pmr::unsynchronized_pool_resource pool(&monotonic_buffers); do_work(&pool); Constrct a pool resource using monotonic_buffers.

Slide 60

Slide 60 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 60/110 Choosing a memory resource The header offers: std::pmr::new_delete_resource std::pmr::null_resource std::pmr::synchronized_pool_resource std::pmr::unsynchronized_pool_resource std::pmr::monotonic_buffer_resource void do_work(std::pmr::polymorphic_allocator<> alloc); auto heap = std::pmr::new_delete_resource(); std::pmr::monotonic_buffer_resource monotonic_buffers(heap); std::pmr::unsynchronized_pool_resource pool(&monotonic_buffers); do_work(&pool); Construct a monotonic buffer resource using the heap.

Slide 61

Slide 61 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 61/110 Choosing a memory resource The header offers: std::pmr::new_delete_resource std::pmr::null_resource std::pmr::synchronized_pool_resource std::pmr::unsynchronized_pool_resource std::pmr::monotonic_buffer_resource void do_work(std::pmr::polymorphic_allocator<> alloc); auto heap = std::pmr::new_delete_resource(); std::pmr::monotonic_buffer_resource monotonic_buffers(heap); std::pmr::unsynchronized_pool_resource pool(&monotonic_buffers); do_work(&pool); Get the new_delete_allocator.

Slide 62

Slide 62 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 62/110 Choosing a memory resource The header offers: std::pmr::new_delete_resource std::pmr::null_resource std::pmr::synchronized_pool_resource std::pmr::unsynchronized_pool_resource std::pmr::monotonic_buffer_resource void do_work(std::pmr::polymorphic_allocator<> alloc); auto heap = std::pmr::new_delete_resource(); std::pmr::monotonic_buffer_resource monotonic_buffers(heap); std::pmr::unsynchronized_pool_resource pool(&monotonic_buffers); do_work(&pool);

Slide 63

Slide 63 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 63/110 Choosing a memory resource The header offers: std::pmr::new_delete_resource std::pmr::null_resource std::pmr::synchronized_pool_resource std::pmr::unsynchronized_pool_resource std::pmr::monotonic_buffer_resource void do_work(std::pmr::polymorphic_allocator<> alloc); auto heap = std::pmr::new_delete_resource(); std::pmr::monotonic_buffer_resource monotonic_buffers(heap); std::pmr::unsynchronized_pool_resource pool(&monotonic_buffers); do_work(&pool); The allocator will use the pool to allocate memory.

Slide 64

Slide 64 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 64/110 Choosing a memory resource The header offers: std::pmr::new_delete_resource std::pmr::null_resource std::pmr::synchronized_pool_resource std::pmr::unsynchronized_pool_resource std::pmr::monotonic_buffer_resource void do_work(std::pmr::polymorphic_allocator<> alloc); auto heap = std::pmr::new_delete_resource(); std::pmr::monotonic_buffer_resource monotonic_buffers(heap); std::pmr::unsynchronized_pool_resource pool(&monotonic_buffers); do_work(&pool); If the pool does not have any free memory, it allocates more via the monotonic buffer.

Slide 65

Slide 65 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 65/110 Choosing a memory resource The header offers: std::pmr::new_delete_resource std::pmr::null_resource std::pmr::synchronized_pool_resource std::pmr::unsynchronized_pool_resource std::pmr::monotonic_buffer_resource void do_work(std::pmr::polymorphic_allocator<> alloc); auto heap = std::pmr::new_delete_resource(); std::pmr::monotonic_buffer_resource monotonic_buffers(heap); std::pmr::unsynchronized_pool_resource pool(&monotonic_buffers); do_work(&pool); If the monotonic buffer doesn’t have any memory, it gets more from the heap

Slide 66

Slide 66 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 66/110 A slight detour class tracing_resource final : public std::pmr::memory_resource { public: tracing_resource(std::ostream& os) : os_(os) {} private: void* do_allocate(size_t bytes, size_t align) override    { auto addr = operator new (bytes, std::align_val_t(align)); os_ << "allocate(" << bytes << ", " << align << ") -> " << addr << '\n'; return addr; } void do_deallocate(void* addr, size_t bytes, size_t align) override { os_ << "deallocate(" << addr << ", " << bytes << ", " << align << ")\n"; operator delete(addr, bytes, std::align_val_t(align)); } bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override { return &other == this; } std::ostream& os_; };

Slide 67

Slide 67 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 67/110 A slight detour class tracing_resource final : public std::pmr::memory_resource { public: tracing_resource(std::ostream& os) : os_(os) {} private: void* do_allocate(size_t bytes, size_t align) override    { auto addr = operator new (bytes, std::align_val_t(align)); os_ << "allocate(" << bytes << ", " << align << ") -> " << addr << '\n'; return addr; } void do_deallocate(void* addr, size_t bytes, size_t align) override { os_ << "deallocate(" << addr << ", " << bytes << ", " << align << ")\n"; operator delete(addr, bytes, std::align_val_t(align)); } bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override { return &other == this; } std::ostream& os_; }; Maybe going directly to the heap isn’t such a great idea?

Slide 68

Slide 68 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 68/110 Improving the tracing resource class tracing_resource final : public std::pmr::memory_resource { public: tracing_resource(std::ostream& os, std::pmr::memory_resource* next = std::pmr::get_default_resource()) : os_(os), next_(next) {} private: void* do_allocate(size_t bytes, size_t align) override    {   auto* addr = next_->allocate(bytes, align); os_ << "allocate(" << bytes << ", " << align << ") -> " << addr << '\n'; return addr; } void do_deallocate(void* addr, size_t bytes, size_t align) override { os_ << "deallocate(" << addr << ", " << bytes << ", " << align << ")\n"; next_->deallocate(addr, bytes, align); } bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override { auto p = dynamic_cast(&other); return p && next_ == p->next_; } std::ostream& os_; std::pmr::memory_resource* next_; };

Slide 69

Slide 69 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 69/110 Improving the tracing resource class tracing_resource final : public std::pmr::memory_resource { public: tracing_resource(std::ostream& os, std::pmr::memory_resource* next = std::pmr::get_default_resource()) : os_(os), next_(next) {} private: void* do_allocate(size_t bytes, size_t align) override    {   auto* addr = next_->allocate(bytes, align); os_ << "allocate(" << bytes << ", " << align << ") -> " << addr << '\n'; return addr; } void do_deallocate(void* addr, size_t bytes, size_t align) override { os_ << "deallocate(" << addr << ", " << bytes << ", " << align << ")\n"; next_->deallocate(addr, bytes, align); } bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override { auto p = dynamic_cast(&other); return p && next_ == p->next_; } std::ostream& os_; std::pmr::memory_resource* next_; };

Slide 70

Slide 70 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 70/110 Improving the tracing resource class tracing_resource final : public std::pmr::memory_resource { public: tracing_resource(std::ostream& os, std::pmr::memory_resource* next = std::pmr::get_default_resource()) : os_(os), next_(next) {} private: void* do_allocate(size_t bytes, size_t align) override    {   auto* addr = next_->allocate(bytes, align); os_ << "allocate(" << bytes << ", " << align << ") -> " << addr << '\n'; return addr; } void do_deallocate(void* addr, size_t bytes, size_t align) override { os_ << "deallocate(" << addr << ", " << bytes << ", " << align << ")\n"; next_->deallocate(addr, bytes, align); } bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override { auto p = dynamic_cast(&other); return p && next_ == p->next_; } std::ostream& os_; std::pmr::memory_resource* next_; }; Get an underlying memory resource

Slide 71

Slide 71 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 71/110 Improving the tracing resource class tracing_resource final : public std::pmr::memory_resource { public: tracing_resource(std::ostream& os, std::pmr::memory_resource* next = std::pmr::get_default_resource()) : os_(os), next_(next) {} private: void* do_allocate(size_t bytes, size_t align) override    {   auto* addr = next_->allocate(bytes, align); os_ << "allocate(" << bytes << ", " << align << ") -> " << addr << '\n'; return addr; } void do_deallocate(void* addr, size_t bytes, size_t align) override { os_ << "deallocate(" << addr << ", " << bytes << ", " << align << ")\n"; next_->deallocate(addr, bytes, align); } bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override { auto p = dynamic_cast(&other); return p && next_ == p->next_; } std::ostream& os_; std::pmr::memory_resource* next_; }; Use the default memory resource if none is given

Slide 72

Slide 72 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 72/110 Improving the tracing resource class tracing_resource final : public std::pmr::memory_resource { public: tracing_resource(std::ostream& os, std::pmr::memory_resource* next = std::pmr::get_default_resource()) : os_(os), next_(next) {} private: void* do_allocate(size_t bytes, size_t align) override    {   auto* addr = next_->allocate(bytes, align); os_ << "allocate(" << bytes << ", " << align << ") -> " << addr << '\n'; return addr; } void do_deallocate(void* addr, size_t bytes, size_t align) override { os_ << "deallocate(" << addr << ", " << bytes << ", " << align << ")\n"; next_->deallocate(addr, bytes, align); } bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override { auto p = dynamic_cast(&other); return p && next_ == p->next_; } std::ostream& os_; std::pmr::memory_resource* next_; }; Use it to allocate and deallocate memory

Slide 73

Slide 73 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 73/110 PMR histogram class histogram { public: histogram(const std::pmr::polymorphic_allocator<>& alloc = {}): words_(alloc){} void add(const std::pmr::string& word) { ++words_[word]; } void print_top(size_t n, std::ostream& os) const { using count = std::pair; std::vector popular(words_.begin(), words_.end()); std::ranges::partial_sort(popular, popular.begin() + n, std::greater{}, &count::second); for (const auto& stat : popular | std::ranges::views::take(n)) { os << stat.first << '\t' << stat.second << '\n'; } } private: std::pmr::unordered_map words_; };

Slide 74

Slide 74 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 74/110 PMR histogram class histogram { public: histogram(const std::pmr::polymorphic_allocator<>& alloc = {}): words_(alloc){} void add(const std::pmr::string& word) { ++words_[word]; } void print_top(size_t n, std::ostream& os) const { using count = std::pair; std::vector popular(words_.begin(), words_.end()); std::ranges::partial_sort(popular, popular.begin() + n, std::greater{}, &count::second); for (const auto& stat : popular | std::ranges::views::take(n)) { os << stat.first << '\t' << stat.second << '\n'; } } private: std::pmr::unordered_map words_; }; Back to chosing an allocator for the word frequency histogram

Slide 75

Slide 75 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 75/110 PMR histogram class histogram { public: histogram(const std::pmr::polymorphic_allocator<>& alloc = {}): words_(alloc){} void add(const std::pmr::string& word) { ++words_[word]; } void print_top(size_t n, std::ostream& os) const { using count = std::pair; std::vector popular(words_.begin(), words_.end()); std::ranges::partial_sort(popular, popular.begin() + n, std::greater{}, &count::second); for (const auto& stat : popular | std::ranges::views::take(n)) { os << stat.first << '\t' << stat.second << '\n'; } } private: std::pmr::unordered_map words_; }; int main() { std::pmr::string word; std::pmr::unsynchronized_pool_resource r; histogram hist(&r); while (std::cin >> word) { hist.add(word); } hist.print_top(5, std::cout); }

Slide 76

Slide 76 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 76/110 PMR histogram class histogram { public: histogram(const std::pmr::polymorphic_allocator<>& alloc = {}): words_(alloc){} void add(const std::pmr::string& word) { ++words_[word]; } void print_top(size_t n, std::ostream& os) const { using count = std::pair; std::vector popular(words_.begin(), words_.end()); std::ranges::partial_sort(popular, popular.begin() + n, std::greater{}, &count::second); for (const auto& stat : popular | std::ranges::views::take(n)) { os << stat.first << '\t' << stat.second << '\n'; } } private: std::pmr::unordered_map words_; }; int main() { std::pmr::string word; std::pmr::unsynchronized_pool_resource r; histogram hist(&r); while (std::cin >> word) { hist.add(word); } hist.print_top(5, std::cout); } I choose an unsynchronized pool resource.

Slide 77

Slide 77 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 77/110 Live Demo

Slide 78

Slide 78 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 78/110 PMR histogram class histogram { public: histogram(const std::pmr::polymorphic_allocator<>& alloc = {}): words_(alloc){} void add(const std::pmr::string& word) { ++words_[word]; } void print_top(size_t n, std::ostream& os) const { using count = std::pair; std::pmr::vector popular(words_.begin(), words_.end(), words_.get_allocator()); std::ranges::partial_sort(popular, popular.begin() + n, std::greater{}, &count::second); for (const auto& stat : popular | std::ranges::views::take(n)) { os << stat.first << '\t' << stat.second << '\n'; } } private: std::pmr::unordered_map words_; };

Slide 79

Slide 79 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 79/110 PMR histogram class histogram { public: histogram(const std::pmr::polymorphic_allocator<>& alloc = {}): words_(alloc){} void add(const std::pmr::string& word) { ++words_[word]; } void print_top(size_t n, std::ostream& os) const { using count = std::pair; std::pmr::vector popular(words_.begin(), words_.end(), words_.get_allocator()); std::ranges::partial_sort(popular, popular.begin() + n, std::greater{}, &count::second); for (const auto& stat : popular | std::ranges::views::take(n)) { os << stat.first << '\t' << stat.second << '\n'; } } private: std::pmr::unordered_map words_; }; 84 allocations down from 133000

Slide 80

Slide 80 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 80/110 PMR histogram class histogram { public: histogram(const std::pmr::polymorphic_allocator<>& alloc = {}): words_(alloc){} void add(const std::pmr::string& word) { ++words_[word]; } void print_top(size_t n, std::ostream& os) const { using count = std::pair; std::pmr::vector popular(words_.begin(), words_.end(), words_.get_allocator()); std::ranges::partial_sort(popular, popular.begin() + n, std::greater{}, &count::second); for (const auto& stat : popular | std::ranges::views::take(n)) { os << stat.first << '\t' << stat.second << '\n'; } } private: std::pmr::unordered_map words_; }; But you really need tools like heaptrack to find the places you’ve missed.

Slide 81

Slide 81 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 81/110 PMR for your types class counted_string { public: counted_string(size_t count, std::pmr::string str) : str_(std::move(str)) , count_(count)   {} std::string_view str() const { return str_; } size_t count() const { return count_; } friend bool operator<(const counted_string& lh, const counted_string& rh) { return lh.count_ < rh.count_; } protected: std::pmr::string str_; size_t count_; };

Slide 82

Slide 82 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 82/110 https://godbolt.org/z/8c4eKcxhh

Slide 83

Slide 83 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 83/110 PMR for your types A PMR enabled type needs:

Slide 84

Slide 84 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 84/110 PMR for your types A PMR enabled type needs: – A public allocator_type type

Slide 85

Slide 85 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 85/110 PMR for your types A PMR enabled type needs: – A public allocator_type type – Constructors with the allocator type as the last argument

Slide 86

Slide 86 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 86/110 PMR for your types A PMR enabled type needs: – A public allocator_type type – Constructors with the allocator type as the last argument – Variadic constructors with std::allocator_arg_t and allocator_type as the first parameters

Slide 87

Slide 87 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 87/110 PMR for your types A PMR enabled type needs: – A public allocator_type type – Constructors with the allocator type as the last argument – Variadic constructors with std::allocator_arg_t and allocator_type as the first parameters – And a copy-like constructor with allocator

Slide 88

Slide 88 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 88/110 PMR for your types A PMR enabled type needs: – A public allocator_type type – Constructors with the allocator type as the last argument – Variadic constructors with std::allocator_arg_t and allocator_type as the first parameters – And a copy-like constructor with allocator – And move-like constructor with allocator

Slide 89

Slide 89 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 89/110 PMR for your types class counted_string { public: using allocator_type = std::pmr::polymorphic_allocator<>; counted_string(size_t count,std::pmr::string str, const allocator_type& alloc = {}); template counted_string(std::allocator_arg_t,  const allocator_type& alloc, size_t count, Ts&& ... ts); counted_string(const counted_string& orig); counted_string(const counted_string& orig, const allocator_type& alloc); counted_string(counted_string&& orig) noexcept; counted_string(counted_string&& orig, const allocator_type& alloc) noexcept; std::string_view str() const { return str_; } size_t count() const { return count_; } friend bool operator<(const counted_string& lh, const counted_string& rh); protected: std::pmr::string str_; size_t count_; };

Slide 90

Slide 90 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 90/110 PMR for your types class counted_string { public: using allocator_type = std::pmr::polymorphic_allocator<>; counted_string(size_t count,std::pmr::string str, const allocator_type& alloc = {}); template counted_string(std::allocator_arg_t,  const allocator_type& alloc, size_t count, Ts&& ... ts); counted_string(const counted_string& orig); counted_string(const counted_string& orig, const allocator_type& alloc); counted_string(counted_string&& orig) noexcept; counted_string(counted_string&& orig, const allocator_type& alloc) noexcept; std::string_view str() const { return str_; } size_t count() const { return count_; } friend bool operator<(const counted_string& lh, const counted_string& rh); protected: std::pmr::string str_; size_t count_; }; Signal that this type uses a PMR allocator.

Slide 91

Slide 91 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 91/110 PMR for your types class counted_string { public: using allocator_type = std::pmr::polymorphic_allocator<>; counted_string(size_t count,std::pmr::string str, const allocator_type& alloc = {}); template counted_string(std::allocator_arg_t,  const allocator_type& alloc, size_t count, Ts&& ... ts); counted_string(const counted_string& orig); counted_string(const counted_string& orig, const allocator_type& alloc); counted_string(counted_string&& orig) noexcept; counted_string(counted_string&& orig, const allocator_type& alloc) noexcept; std::string_view str() const { return str_; } size_t count() const { return count_; } friend bool operator<(const counted_string& lh, const counted_string& rh); protected: std::pmr::string str_; size_t count_; }; Constructor with allocator last

Slide 92

Slide 92 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 92/110 PMR for your types class counted_string { public: using allocator_type = std::pmr::polymorphic_allocator<>; counted_string(size_t count,std::pmr::string str, const allocator_type& alloc = {}); template counted_string(std::allocator_arg_t,  const allocator_type& alloc, size_t count, Ts&& ... ts); counted_string(const counted_string& orig); counted_string(const counted_string& orig, const allocator_type& alloc); counted_string(counted_string&& orig) noexcept; counted_string(counted_string&& orig, const allocator_type& alloc) noexcept; std::string_view str() const { return str_; } size_t count() const { return count_; } friend bool operator<(const counted_string& lh, const counted_string& rh); protected: std::pmr::string str_; size_t count_; }; Variadic constructor with non-allocator args at end

Slide 93

Slide 93 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 93/110 PMR for your types class counted_string { public: using allocator_type = std::pmr::polymorphic_allocator<>; counted_string(size_t count,std::pmr::string str, const allocator_type& alloc = {}); template counted_string(std::allocator_arg_t,  const allocator_type& alloc, size_t count, Ts&& ... ts); counted_string(const counted_string& orig); counted_string(const counted_string& orig, const allocator_type& alloc); counted_string(counted_string&& orig) noexcept; counted_string(counted_string&& orig, const allocator_type& alloc) noexcept; std::string_view str() const { return str_; } size_t count() const { return count_; } friend bool operator<(const counted_string& lh, const counted_string& rh); protected: std::pmr::string str_; size_t count_; }; Copy-like constructor with allocator, used by containers

Slide 94

Slide 94 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 94/110 PMR for your types class counted_string { public: using allocator_type = std::pmr::polymorphic_allocator<>; counted_string(size_t count,std::pmr::string str, const allocator_type& alloc = {}); template counted_string(std::allocator_arg_t,  const allocator_type& alloc, size_t count, Ts&& ... ts); counted_string(const counted_string& orig); counted_string(const counted_string& orig, const allocator_type& alloc); counted_string(counted_string&& orig) noexcept; counted_string(counted_string&& orig, const allocator_type& alloc) noexcept; std::string_view str() const { return str_; } size_t count() const { return count_; } friend bool operator<(const counted_string& lh, const counted_string& rh); protected: std::pmr::string str_; size_t count_; }; Move-like constructor with allocator, used by containers

Slide 95

Slide 95 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 95/110 https://godbolt.org/z/aoYPWGxP8

Slide 96

Slide 96 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 96/110 In conclusion

Slide 97

Slide 97 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 97/110 In conclusion ● Making your own types use PMR allocators is not hard, but it’s easy to make mistakes

Slide 98

Slide 98 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 98/110 In conclusion ● Making your own types use PMR allocators is not hard, but it’s easy to make mistakes ● Using PMR types with standard containers is easy

Slide 99

Slide 99 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 99/110 In conclusion ● Making your own types use PMR allocators is not hard, but it’s easy to make mistakes ● Using PMR types with standard containers is easy ● PMR is very flexible because the memory resources and the types that uses them are decoupled

Slide 100

Slide 100 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 100/110 In conclusion ● Making your own types use PMR allocators is not hard, but it’s easy to make mistakes ● Using PMR types with standard containers is easy ● PMR is very flexible because the memory resources and the types that uses them are decoupled ● Memory usage is quite opaque, so it’s not easy to see where you’ve made mistakes

Slide 101

Slide 101 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 101/110 In conclusion ● Making your own types use PMR allocators is not hard, but it’s easy to make mistakes ● Using PMR types with standard containers is easy ● PMR is very flexible because the memory resources and the types that uses them are decoupled ● Memory usage is quite opaque, so it’s not easy to see where you’ve made mistakes ● PMR is still not a panacea for your memory management needs, but it’s often sufficient

Slide 102

Slide 102 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 102/110 Just one more thing... http://mojtv.hr/images/fb2de057-6db9-45f8-ba8a-6891149a4a87.jpg

Slide 103

Slide 103 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 103/110 Just one more thing... http://mojtv.hr/images/fb2de057-6db9-45f8-ba8a-6891149a4a87.jpg A pmr container never changes its memory resource.

Slide 104

Slide 104 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 104/110 Just one more thing... http://mojtv.hr/images/fb2de057-6db9-45f8-ba8a-6891149a4a87.jpg A pmr container never changes its memory resource. Not even when move-assigning.

Slide 105

Slide 105 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 105/110 Just one more thing... http://mojtv.hr/images/fb2de057-6db9-45f8-ba8a-6891149a4a87.jpg A pmr container never changes its memory resource. Not even when move-assigning. • If resource operator== says the memory resources are the same, then the internal pointers are taken over, otherwise it’s element-wise move assignment to newly allocated memory.

Slide 106

Slide 106 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 106/110 Just one more thing... http://mojtv.hr/images/fb2de057-6db9-45f8-ba8a-6891149a4a87.jpg A pmr container never changes its memory resource. Not even when move-assigning. • If resource operator== says the memory resources are the same, then the internal pointers are taken over, otherwise it’s element-wise move assignment to newly allocated memory.

Slide 107

Slide 107 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 107/110 Just one more thing... http://mojtv.hr/images/fb2de057-6db9-45f8-ba8a-6891149a4a87.jpg A pmr container never changes its memory resource. Not even when move-assigning. • If resource operator== says the memory resources are the same, then the internal pointers are taken over, otherwise it’s element-wise move assignment to newly allocated memory.

Slide 108

Slide 108 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 108/110 Just one more thing... http://mojtv.hr/images/fb2de057-6db9-45f8-ba8a-6891149a4a87.jpg A pmr container never changes its memory resource. Not even when move-assigning. • If resource operator== says the memory resources are the same, then the internal pointers are taken over, otherwise it’s element-wise move assignment to newly allocated memory.

Slide 109

Slide 109 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 109/110 Learn more https://tinyurl.com/cppweekly-on-pmr YouTube playlist with 6 episodes of Jason Turner’s C++Weekly covering PMR https://youtu.be/RLezJuqNcEQ CppConn 2019 – Pablo Halpern and Alisdair Meredith, “Congratulations! You survived C++11 allocators!”

Slide 110

Slide 110 text

Mmmmm – NDC{TechTown} 2023 © Björn Fahller @[email protected] 110/110 [email protected] @[email protected] @rollbear Björn Fahller Most Malleable Memory Management Method