In latency critical systems (such as high frequency trading algorithms or game loop renderers), making dynamic heap allocations during runtime is strictly forbidden. C++17 solved this by introducing Polymorphic Memory Resources (std::pmr), allowing developers to specify custom allocators at runtime.
Let's look at placement new, arena allocators, and the PMR standard library classes.
Placement New: Constructing in Place
Before looking at PMR, we must understand how to construct an object in pre allocated memory. Standard new allocates memory and calls the constructor. Placement New bypasses allocation entirely, constructing the object at a specific memory address you provide:
The PMR Monotonic Buffer Resource (Memory Arena)
A Monotonic Buffer Resource (often called an Arena Allocator) is a custom memory pool resource. It allocates a fixed buffer (on the stack or heap) once at startup. When you add elements to a container, it constructs them inside this buffer, increasing a pointer offset.
OS Heap Locks vs Pointer Bumping
General-purpose allocation can be costly or variable because an allocator may manage free lists, synchronize under contention, request pages from the operating system, and initialize memory. Most calls to new are handled by a user-space allocator rather than a kernel transition, and modern allocators commonly use per-thread caches. Treat an allocation bottleneck as something to profile, not an assumption.
A monotonic arena commonly serves an allocation by aligning an address and advancing an offset. This can be very cheap when the resource has capacity, but it may fall back to an upstream allocator and still needs a lifetime strategy for constructed objects. Benchmark it against the default allocator for the actual allocation pattern.
- The Bookcase Metaphor: Storing variables with the default allocator is like renting a new storage locker every time you buy a book, then driving to return the key when done. Using an arena allocator is like buying a large bookcase once at startup. Placing books on the shelf is instant, and when you move houses, you just throw the whole bookcase away at once.