A better allocator starts with the memory block
Source 00:00Andrei Alexandrescu's aim is to design good memory allocators: mechanisms that obtain and release storage for a program. His provocative starting point is that the historical design of std::allocator has surprisingly little to do with that job. The key questions are what information an allocation interface should carry and which responsibilities belong in it. Watch from 00:30
Allocation performance has moved in cycles
Source 02:49In the speaker's account, the small allocator presented in the Kernighan and Ritchie C book was simple by later standards, but remarkable in its day: C made it possible to control memory layout without writing assembly. He recalls dynamic allocation—the acquisition of storage while a program runs—as unusual in systems programming before it became widespread. Later, he says, general-purpose malloc implementations fell behind custom code, then improved with allocators such as Hoard, jemalloc and tcmalloc. His practical conclusion is to understand how malloc works and recognize that a specialized design can sometimes do better, rather than assume either approach always wins. This is his historical sketch, not a benchmark of current implementations. Watch from 02:49
Return the size with the pointer
Source 05:31C's malloc takes a requested size, but free receives only the pointer to the storage. The speaker argues that this asymmetry can cost performance: the allocator must recover or manage size information elsewhere, even when the caller knows it. Early allocators already kept block sizes for their own bookkeeping, but that historical convenience became part of an enduring interface. He points to a C++ proposal concerning the performance consequences of omitting size from global operator delete, while noting, through his exchange with Howard Hinnant, that changing allocation interfaces in a standard is difficult. Watch from 05:31
For a new design he proposes a block: a pointer and a length returned together on allocation and passed back together on release. Two pointers could express essentially the same range. The point is to preserve information at the interface, so deallocation need not rediscover the length merely because the caller discarded it. This is a proposed interface, not a claim that free takes a block. Watch from 08:07
C++ new adds type syntax but brings its own complications
Source 08:39The speaker values the type information associated with C++ new, while criticizing its historical relationship with malloc: in his account, that information is quickly lost when allocation is forwarded. He also mentions realloc, C's resizing function, as a powerful but easy-to-misuse facility with no direct renew counterpart. These are his design judgments; the talk is not establishing that every implementation of operator new must call malloc. Watch from 08:39
His syntax example is int[100], the type of a fixed array of 100 integers. new int yields an int*; new int[100] also yields an int*, pointing to the first element of an allocated array, rather than a pointer to the whole int[100] array. He finds that interpretation surprising. He also complains that class-specific allocation functions and constructors do not communicate the way one might want, and advises against relying on class-level operator new for serious allocation work. Watch from 10:14
Array allocation introduces a matching rule: use delete for an object created by new, and delete[] for an array created by new[]. A destructor is the operation that cleans up an object when its lifetime ends. The speaker describes the danger in terms of failing to destroy the array's elements; technically, mismatching these forms is undefined behavior, so its outcome must not be treated as reliably calling just one destructor. He regards this separate array machinery as unnecessary complexity. Watch from 12:23
Separate allocation from object construction
Source 14:04The speaker traces the design of std::allocator to support for near and far pointers in older segmented memory systems, where an address involved a segment and offset. He argues that remnants of this motivation shaped an interface ill suited to allocation itself, and says the C++ committee removed the near/far-pointer machinery. This is his account of the design's history and of the standard as discussed at the time of the talk, not a description of every current allocator API. Watch from 14:04
To make the point, he recalls an earlier, lengthy talk titled “Making Allocators Work”: roughly two decades after the original design, he says, its title still posed the question of making allocators work, and he found no slide dedicated to memory allocation itself. His criticism is that the allocator abstraction and the actual allocation strategy had become separated. Watch from 15:29
For the storage operation itself, he identifies two primary properties: size and alignment. Alignment is the boundary at which an address must be placed. An int and an unsigned int of the same size and layout ordinarily need no different allocation strategy just because their type names differ. Two 128-byte objects of different types might nevertheless warrant different treatment; alignment matters, and a specialized allocator may be especially effective for a size-and-alignment combination rather than one particular type. Watch from 19:04
That leads to his proposed division of labor: an allocator should obtain and release blocks, while constructing and destroying objects is a separate responsibility. He criticizes the historical allocator interface for mixing in construct, destroy, and machinery such as rebind—used to obtain an allocator for a different type—while the underlying storage is still expressed in terms of pointers and sizes. His proposed block consists of a void* and a size_t. Watch from 20:30
Composable allocators need state and a way to route frees
Source 21:40An allocator has to keep track of memory somehow, even if that state is shared or global, as with the speaker’s description of malloc. C++14 permits stateful allocators, a change Andrei Alexandrescu credits to John Lakos and Pablo Halpern. His next requirement is composition: building an allocator from smaller allocation strategies. Watch from 21:40
He points to allocators that choose different strategies for small, medium and large requests, with smaller size classes and buckets inside a strategy. His approximate thresholds for Doug Lea’s allocator illustrate the idea, rather than specifying a universal rule. Composition should be a first-class design concern, he argues, because an allocator can then be assembled from pieces suited to a workload.
The simplest piece is a null allocator: allocation always returns null, and deallocation asserts that its input is null. Since a block must be returned to the allocator that supplied it, this allocator has nothing else valid to free. It can terminate a chain of allocation attempts. A pass-through can be obtained by combining it with another allocator. Watch from 25:39
A fallback allocator combines a primary and a secondary allocator. It tries the primary first and, if that fails, tries the secondary. In the talk’s example, the two are held through private inheritance, allowing empty base optimization; members could also hold them. Allocation is straightforward, but freeing a block raises a question: which allocator supplied it? Watch from 27:35
The proposed answer is an owns operation. If the primary says it owns the block, route deallocation there; otherwise route it to the fallback. The secondary need not implement owns for this routing. But for the composite itself to answer owns in the presented design, both components must support it: its answer is the logical OR of their answers. The speaker calls attention to a C++ template property he jokingly names “method definition failure is not an error”: an unused member function of a class template need not be instantiated. Watch from 31:08
An audience member suggests adding a Boolean marker to each allocation instead. Alexandrescu accepts this as a possible approach but points to its time and space cost, especially where owns is already a cheap test. A last-resort allocator may be malloc or the null allocator, so it is useful that the fallback does not have to answer ownership queries for the composite to route frees. Watch from 34:01
A stack allocator trades arbitrary frees for a fast reset
Source 35:55A stack allocator advances a pointer through a fixed buffer as it hands out blocks. Pair a 16 KB stack buffer with a malloc fallback and requests that do not fit can go to the secondary allocator. Its deallocation is conditional: if the block’s starting address plus its size rounded up to the allocator’s alignment equals the current allocation pointer, it is the most recent allocation and the pointer can move back; a block in the middle cannot be reclaimed that way. owns can be a range check, and deallocateAll can reset the pointer to make the whole buffer reusable—but that reset is risky while anything still depends on those blocks. Watch from 35:55
At this layer the speaker handles an untyped address and size, separating raw allocation from objects and their lifetime management. Typed allocators can be built on top. He demonstrates cleanup at scope exit and suggests that a stack buffer backed by malloc can accelerate many workloads while retaining a path for larger requests. His “95%” remark is an informal estimate in the talk, not a benchmark or a guarantee for a particular application. Watch from 39:15
Alignment matters when advancing the pointer. If an aligned buffer supplies one byte and the pointer moves forward by exactly one byte, the next address may be unsuitable for an aligned allocation. The demonstrated solution rounds each request up to the chosen alignment before advancing and checks whether the buffer has room. An audience member proposes aligning on demand instead; the speaker considers that viable when the necessary type information is available. Watch from 40:54
A free list reuses blocks by storing links inside freed memory
Source 43:28A free list starts empty and uses a backup allocator for its first requests. When a block of the particular size it handles is freed, it threads that block into a linked list. The link is stored in the freed memory itself, making this an intrusive list with little additional memory overhead; the allocator keeps a pointer to the head. On a later request for that exact size, a nonempty list supplies a previously freed block. Watch from 43:28
Free lists trade fast reuse for retained memory and contention
Source 45:22A free list can return a matching freed object in constant time, but that speed comes with costs. In the simple version, freed objects remain in the list instead of returning to the backing allocator. A burst of one million 64-byte objects can therefore leave a million objects retained after the burst, fragmenting memory. 45:22
A shared list also has poor thread affinity: memory used by one thread may be handed to another whose cache is cold, and access to a global list can be contentious. The speaker suggests a small thread-local list per thread as a first line of defense, optionally backed by a global list. The list is intrusive: it stores links inside freed memory, so writing to an object after freeing it can corrupt the list.
Cache behavior is subtler than “reuse means hot memory.” An application may free an object after its cache lines have gone cold; writing the next pointer into that object then touches cold memory. The speaker stresses that behavior depends on the system, while properly used free lists can still be very fast for small objects. 49:24
The basic implementation wraps a parent allocator, which could itself be a malloc-backed or stack allocator. For a request of its chosen size, it pops the head if the list is nonempty; otherwise it asks the parent. On deallocation, a different-sized block goes back to the parent, while a matching block stores the next link in its first word and becomes the new head. 51:21
A practical version can accept a range of sizes, allocate a batch of objects, and cap the number retained. For example, a 1 KB free list can serve a 900-byte request; batching reduces allocation overhead, but makes it harder to know when the entire backing block can be released. In the talk's concrete configuration, requests from 17 through 32 bytes receive 32-byte objects, eight are obtained at a time, and at most 1,024 entries are retained. Individual objects return to the list; the 1,024 limit is its length, not the object size. Batching is useful but adds nontrivial implementation work. 52:08
This wrapper can also sit on a stack allocator. For objects of the right size, returning an object to the list allows reuse of space in the middle even though the underlying stack allocator alone has a more restrictive release order. This is the first example here of composition adding a capability. 55:25
Affixes add checks and allocation information
Source 56:06An affix allocator wraps another allocator and adds optional prefix and suffix objects around each allocation; a void affix means no object on that side. One use is a guard: put known signatures around a block and check them on deallocation for corruption. The speaker also describes reserving larger guard regions before and after large blocks. 56:06
A prefix can record the file and line that requested a block, making it possible to attribute allocation activity to a call site. A statistics wrapper can collect selected counts, such as calls to allocate and deallocate, alongside per-allocation or global information for later analysis. The speaker contrasts this with what he could see using perf at the time; his platform and tooling remarks are historical, not a current tool comparison. 57:16
He sketches another way to gather call-site statistics without dynamically allocating the bookkeeping: an allocation macro creates a static instance of a small structure containing file, line, counters and a link to the next static record. The records form a singly linked list, which can be traversed from its head at program end. This is a sketch of the idea, not a complete implementation. 59:56
Bitmap blocks separate metadata from the objects
Source 61:41A bitmap block allocator divides a region into fixed-sized blocks and tracks occupancy with one bit per block. With 64-byte blocks, a request can consume several contiguous blocks. The bitmap is stored separately from the data, so the allocator can manipulate compact metadata with fast bit operations, potentially including SIMD. The speaker presents this as an alternative to the free list's cache problems, not a guarantee that metadata is always cached. 61:41
The tradeoff is internal fragmentation: a request that is not an exact multiple of the block size occupies more space than it needs. Sharing the bitmap among threads also introduces contention. A cascading allocator can extend the design: start with a bitmap allocator over a 1 MB region, lazily add another managed region when capacity runs out, and release regions when they are no longer used. Managing those regions requires additional logic. 63:25
Size-based routing composes specialized allocators
Source 64:48A segregator sends requests at or below a threshold to a small-object allocator and larger requests to another allocator. Given the original block size on deallocation, the same rule routes it back, so this design does not need an owns check to choose between its two children. The children may themselves be segregators. 64:40
For example, one split can route smaller-than-4 KB requests to a second split, which uses a free list for sizes below 128 bytes and a medium allocator for the rest; larger requests go to a large allocator. The speaker suggests arranging many splits like a binary search tree, but notes that a linear ordering can favor common small requests. Exact threshold equality should follow the chosen implementation's rule. 65:55
A bucketizer generalizes size routing across a compile-time minimum and maximum: each size category has its own allocator. Buckets can grow linearly or logarithmically, for example by successive powers of two. The speaker's broader point is that a small vocabulary of strategies can be assembled into a realistic allocator; each still carries its own costs and implementation details. 67:10
A small interface lets allocator strategies work together
Source 68:22The examples have reached a practical conclusion: a real allocator can combine several of these strategies, while each piece remains relatively simple. Alexandrescu presents the following interface as one that covers the techniques in the talk and more. Its operations describe what a component can do; an individual allocator need not implement every operation. Watch the API discussion
alignmentstates the allocator’s alignment: the boundary on which an allocated address must lie.goodSize(n)reports the size the allocator would actually reserve for a request ofn, usually rounding up to its next allocation size. A vector can use that size when planning capacity and make better use of memory already reserved.allocateobtains a block.allocateAllobtains all available space and is useful with a stack allocator: another strategy can organize that space as a free list. The malloc-backed allocator in the talk does not define this operation.expandattempts to grow a block in place;reallocateperforms the role of C’srealloc.ownstests whether a block belongs to the allocator.deallocatereleases a block, whiledeallocateAllreleases everything in one operation.- Aligned allocation is also needed in practice, including for games. The speaker calls it a necessary complication and suggests wrapping the relevant POSIX or Windows primitives. Watch the aligned-allocation discussion
Composability is the design goal
Source 70:43Alexandrescu’s criticism of std::allocator is that its interface does not account for composition. His proposed first-principles alternative is an API for simple allocators that work together in different combinations. He argues that the patterns shown can handle most memory-allocation needs, while acknowledging the limits of that claim. Watch the conclusion
The history of the standard allocator also explains why he found it hard to challenge. It appears complete: it has operations such as allocate, deallocate, construct, and destroy. Understanding how it came to be helped him question the assumption that an existing interface must be a good one. For this design, his measure of improvement is composability.