The Double Headed Stack Allocator
https://blog.demofox.org/2026/10/03/the-double-headed-stack-allocator/
- discussion_url
- https://news.ycombinator.com/item?id=50098274
HN discussion
Based on 8 selected comments and replies.
Commenters largely treated the double-headed stack as one option among several for reducing fragmentation.
tom_ reported that the same effect is achievable with an ordinary allocator: at a previous employer the general-purpose memory manager had an "allocate high" option that returned the highest possible address instead of the lowest, which helped when allocating transient data while building persistent data. windenntw recalled that AmigaOS exposed a MEMF_REVERSE flag on its allocator, and that a simple mid-1990s patch forcing allocations above roughly 1KB to that mode greatly reduced fragmentation.
kevin_thibedeau preferred bipartite buffers for sequences of variable-sized objects, noting they behave like a circular buffer and avoid the memory exhaustion that can strand a bump allocator, at the cost of sequential rather than random access. Panzerschrek made a similar point for streaming workloads, suggesting a cyclic allocator, as Quake reportedly used for its surface cache.
saagarjha described using doubly mapped (mirror-mapped) regions when streaming weights to a GPU: pointers are handed out from the first region in order but may wrap to the front via virtual addressing, which allowed allocating several pipeline stages ahead, whereas a double-sided allocator effectively supports only one extra stage. pixelpoet supplied the term "magic ring buffer" for this technique.
Asked why not keep a separate stack allocator per object type, akoboldfrying replied that the system handles two categories, and that a category may contain many distinct types — what matters is that members of a category share an allocation and deallocation timeline, which prevents fragmentation. For two categories, they argued, a double-headed stack strictly dominates two separate half-size stacks, since it permits every allocation sequence the latter allows plus additional ones.
Selected comments: tom_ · kevin_thibedeau · Panzerschrek · windenntw · saagarjha · akoboldfrying