What happened
This article describes the implementation of stack allocators within the StarCraft 2 team at Blizzard to address a memory fragmentation problem.
The core issue was the time taken for dynamic memory allocations during the frame, as these were impacting performance in the rendering pipeline and shader execution. By switching to a system where all transient memory allocation was managed on a single stack (the ‘tos’ variable), the process of allocating and freeing memory became purely integer addition and assignment, drastically reducing overhead.
Why it matters
The impact wasn't just speed; it was the elimination of the allocation time from the profiler. This change significantly streamlined development workflows and allowed for more focused optimization efforts elsewhere in the game engine. The fundamental shift from dynamic memory management to this integer-based approach provided a demonstrable performance boost that directly translated into smoother gameplay.
But, but, but…
While successful, the initial implementation lacked destructor calls, which meant all allocated memory was immediately freed upon frame completion without proper cleanup.
A more robust system would need to incorporate placement new for constructor calls and potentially manual destruction. The article acknowledges this limitation and discusses how a RAII pattern could have been employed, though stack allocators work best with simpler types, where the core principle remains relevant: rapid allocation and freeing are fundamentally faster than dynamic memory management during intensive frame processing.
““The result was night and day. Allocation no longer took any measurable time in the profiler, because allocation was an integer addition, and freeing all memory was a single integer assignment.””
— Dr Non
The counter-take
The failure to adequately address destructor calls and cleanup within initial implementations highlights a common oversight when optimizing performance – focusing solely on allocation speed without considering the complete lifecycle of allocated resources. This is particularly critical in complex game engines where improper resource management can lead to crashes or instability.
The AI angle
The shift to integer-based allocation introduces a degree of automation, potentially reducing the cognitive load on developers needing to manually track resource management during game development. This represents an incremental upgrade in tooling that aims for more efficient memory handling.
