skip to content

How does C++17's std::pmr::monotonic_buffer_resource over a fixed buffer make a tick's temporary containers allocation-free, and where can that setup still reach the heap?

level: seniorimportance: should knowfreq 32%

answer

  1. a bump pointer over your buffer
  2. deallocate does nothing
  3. the upstream decides what overflow does
  4. inner elements need pmr types too
  5. copies fall back to the default

basics

~20 s

Build std::pmr containers on a monotonic_buffer_resource placed over a preallocated buffer: allocation bumps a pointer, deallocation does nothing, and everything is released at once. Overflow goes upstream, by default to new and delete, and copies and non-pmr elements escape too.

solid answer

~40 s

Construct `std::pmr::monotonic_buffer_resource mr(buf, size, upstream)` for each tick and pass `&mr` to `std::pmr::vector`, `std::pmr::string` and friends. Allocation carves the next aligned block from `buf`; `do_deallocate` has no effect; the destructor or `release()` frees everything at once. Three paths still reach the heap. First, overflow: when the buffer is full the resource asks its upstream for a bigger one, and the upstream defaults to `get_default_resource()`, normally `new_delete_resource()` — pass `std::pmr::null_memory_resource()` to make overflow throw `std::bad_alloc` instead. Second, element types: `std::pmr::vector<std::string>` keeps its string objects in the arena, but each string's characters come from `std::allocator`; `std::pmr::vector<std::pmr::string>` passes the resource down through uses-allocator construction. Third, copies: copy-constructing a pmr container gives the copy `polymorphic_allocator()`, the default resource.

code

cpp · 28 lines
cpp
#include <array>
#include <cstddef>
#include <cstdio>
#include <memory_resource>
#include <new>
#include <string>
#include <vector>

int main() {
    alignas(std::max_align_t) static std::array<std::byte, 64 * 1024> arena;
    for (int tick = 0; tick < 2; ++tick) {
        // Fresh resource per tick over the same buffer; overflow throws instead of calling the heap.
        std::pmr::monotonic_buffer_resource mr(arena.data(), arena.size(), std::pmr::null_memory_resource());
        std::pmr::vector<std::pmr::string> symbols(&mr);
        symbols.emplace_back("AAPL.NASDAQ.L1.BID.QUOTE.LONG");  // the inner string uses mr too
        bool inner = symbols[0].get_allocator().resource() == &mr;
        std::pmr::vector<std::pmr::string> copy = symbols;      // copy construction does not propagate
        bool dflt = copy.get_allocator().resource() == std::pmr::get_default_resource();
        std::printf("tick %d: inner string on mr=%d, copy on default resource=%d\n", tick, inner, dflt);
    }  // ~monotonic_buffer_resource releases everything at once
    try {
        std::pmr::monotonic_buffer_resource mr(arena.data(), arena.size(), std::pmr::null_memory_resource());
        std::pmr::vector<char> big(&mr);
        big.resize(arena.size() + 1);
    } catch (const std::bad_alloc&) {
        std::puts("over budget: std::bad_alloc from null_memory_resource()");
    }
}

go deeper

for a junior

Recall that std::pmr containers take a memory resource pointer and that a monotonic resource frees everything at once rather than block by block.

for a middle

Explain bump allocation, the no-op deallocate, release at the end of the tick, and why nested strings must be pmr strings to share the resource.

for a senior

Name every path back to the heap: the default upstream, non-pmr elements, copy construction, default-constructed containers, and show the null upstream that turns a miss into an exception.

for a principal

Weigh pmr's runtime flexibility and per-container pointer against template allocators, and decide which resource each data lifetime in the engine gets.

## The pieces of std::pmr C++17 added `<memory_resource>`, a runtime-polymorphic allocation interface built from three parts: - **`std::pmr::memory_resource`**, an abstract class with `allocate`, `deallocate` and `is_equal`, which call the virtual `do_allocate`, `do_deallocate` and `do_is_equal`. - **`std::pmr::polymorphic_allocator<T>`**, a standard allocator that holds a `memory_resource*` and forwards every request to it. - **Aliases** such as `std::pmr::vector<T>` (`std::vector<T, std::pmr::polymorphic_allocator<T>>`) and `std::pmr::string`. Because the resource is chosen at run time, a `std::pmr::vector<double>` built on an arena and one built on the heap have the same type. ## How the monotonic resource makes allocation cheap `std::pmr::monotonic_buffer_resource` is, in the standard's words, a special-purpose resource for building up a few objects and releasing them all at once. Given a buffer, `do_allocate` takes the next block that fits the requested size and alignment from the current buffer — in practice a pointer bump. `do_deallocate` has **no effect**; memory used by the resource only grows until `release()` or the destructor hands it all back to the upstream in one step. That matches a tick exactly: 1. Before the first tick, set aside a buffer: a static array, or one heap block allocated at startup. 2. At the start of each tick, construct a `monotonic_buffer_resource` over that buffer (or call `release()` on a long-lived one; the current wording resets it to the initial buffer). 3. Build the tick's temporary `std::pmr` containers with the resource's address. 4. At the end of the tick, destroy the containers, then the resource. Nothing is freed one block at a time. ## Why not just reserve every vector? `reserve` works when one container's peak size is known. A tick usually builds many short-lived things of varying shape: a vector of parsed fields, a few strings, a small map of changed price levels. Pre-sizing each one separately is fragile: `std::map` has no `reserve`, and `std::unordered_map::reserve` sizes only its bucket array, not the nodes. A monotonic resource gives all of them one shared budget — the buffer — and one release point, so the tick's scratch memory has a single number to size and a single place to check. ## Where it still reaches the heap | Leak | Why | Fix | |---|---|---| | Buffer overflow | the resource asks `upstream_rsrc` for a new buffer; the upstream defaults to `get_default_resource()`, initially `new_delete_resource()` | pass `std::pmr::null_memory_resource()` so overflow throws `std::bad_alloc`, or a counting fallback | | Non-pmr elements | in `std::pmr::vector<std::string>` each string's characters come from `std::allocator` | use `std::pmr::vector<std::pmr::string>`; `polymorphic_allocator::construct` performs uses-allocator construction, so inner strings get the same resource | | Copy construction | `polymorphic_allocator::select_on_container_copy_construction()` returns `polymorphic_allocator()`, which uses the default resource | construct the copy with an explicit allocator argument, or avoid the copy | | Default-constructed containers | a pmr container built without a resource uses `get_default_resource()` | always pass the resource; in tests set the default to `null_memory_resource()` to catch misses | The example shows the second and third rows on libc++: the inner `std::pmr::string` reports the tick's resource, the copy reports the default resource, and a request larger than the buffer throws because the upstream is `null_memory_resource()`. ## Traps that are not heap allocations - **Growth wastes the arena.** A `std::pmr::vector` that doubles inside a monotonic resource abandons each old block until release, so it consumes roughly twice its final size. Reserve inside the resource first. - **Lifetime order.** Every container built on the resource must be destroyed before `release()` or the resource's destructor; the note on `release()` says memory goes back upstream even for blocks never deallocated, so a container that outlives it dangles. - **No locking.** Among the standard's resource classes, only `synchronized_pool_resource` is specified as usable from several threads without external synchronization; `monotonic_buffer_resource` has no locking. Give each tick thread its own resource and buffer. - **Assignment does not propagate.** `polymorphic_allocator` defines none of the `propagate_on_container_*` traits, so they default to false: move-assigning between containers on different resources moves element by element into the target's resource. - **Implementation-defined sizing.** Without a buffer, the initial size and the growth factor for later buffers are implementation-defined; libc++ starts at 1,024 bytes. ## Choosing between the standard resources `monotonic_buffer_resource` suits scratch data that dies together. When objects are freed individually and memory must be reused within the same lifetime — order-book levels coming and going — `unsynchronized_pool_resource` (single thread) or `synchronized_pool_resource` (shared) keep pools of fixed-size blocks and recycle them.

  • When would you pick unsynchronized_pool_resource over monotonic_buffer_resource on a tick thread?
    When objects die one at a time and their memory must be reused before the whole batch ends, such as price levels inserted and erased in an order book. `unsynchronized_pool_resource` sorts requests into pools of fixed-size blocks and recycles freed blocks, while a monotonic resource only grows until release. It has no locking, so it belongs to one thread; `synchronized_pool_resource` is the shared variant.
  • Why does a std::pmr::vector that grows inside a monotonic resource use up the buffer faster than its size suggests?
    Each reallocation allocates a larger block and deallocates the old one, but `do_deallocate` on a monotonic resource does nothing. With doubling growth, the abandoned blocks add up to about the final capacity again, so the vector consumes roughly twice its final size. Calling `reserve` once inside the resource avoids it.

A monotonic buffer resource is a notepad for one meeting: you keep writing on the next free line, crossing something out frees nothing, and at the end you tear off the used pages in one go.

  • A monotonic buffer resource frees each block as soon as the container releases it
  • A fixed buffer guarantees no heap use, whatever the upstream resource is
  • A pmr::vector of std::string keeps the strings' characters in the arena too
  • Copying a pmr container keeps the source container's memory resource
  • One monotonic buffer resource can be shared by several tick threads without locking