HeadlinesBriefing HeadlinesBriefing.com

Double Headed Stack Allocator Performance Optimization

Hacker News •
×

When programming high-performance applications such as video games, developers seek tricks to write fast code that maximizes limited resources. Memory allocation is critical for performance-critical code requiring variable memory. A "bump allocator" or stack allocator optimizes this by treating allocation as a simple integer addition and deallocation as an even cheaper integer assignment.

Around 2010, while working at Surreal Software on the cancelled project "This is Vegas," animator programming faced frequent transient allocations for animation blending. These allocations existed only briefly per frame and depended on which characters were active, determined by the camera frustum. The allocator appeared as a hot spot in profiling.

The solution involved implementing a stack allocator where allocation is merely an integer add, and freeing memory is a single integer assignment. By allocating a fixed block (e.g., 1MB) and maintaining a "tos" (top of stack) variable, memory management became nearly instantaneous. The result was a significant performance improvement, with allocation taking measurable time in profilers.

A key limitation is that this allocator does not automatically call destructors; while placement new can handle constructors, destructor management is manual and error-prone. Furthermore, stack allocators excel in multi-threaded environments if the "tos" variable uses atomic addition, allowing multiple threads to allocate from the same pool, beneficial in fork/join CPU situations or GPU algorithms. Partial deallocations are also possible, remembering the "tos" state at the start of recursive processes and resetting it upon completion.

The technique offers substantial speed gains for transient memory needs during a game frame.

Source: Hacker News · Summarized by HeadlinesBriefing