skip to content

How does a per-frame linear allocator in a game engine work, and when do you need a double-buffered version instead of a single buffer?

level: middleimportance: must knowfreq 55%

answer

  1. one buffer, one moving offset
  2. align up, then bump
  3. free everything by resetting
  4. read next frame needs two
  5. frame N uses buffer N mod 2

basics

~20 s

A per-frame linear allocator bumps an offset through one preallocated buffer and frees everything by resetting the offset once a frame. Data still read during the next frame needs two buffers used alternately, so a reset never destroys it.

solid answer

~50 s

Temporary data that lives for one frame, such as a list of objects found by a query, scratch arrays or formatted debug text, goes into a buffer reserved at startup. To allocate, round the current offset up to the requested alignment, return `base + start`, and advance the offset by the size; there is no per-allocation free. At the start of the next frame the offset goes back to zero, which frees everything at once. The danger is lifetime: a pointer kept past the reset points at bytes a later frame may already have overwritten. When data written in frame N is read in frame N+1, such as hit events handled at the start of the next frame, use two buffers: frame N allocates from buffer `N mod 2` and resets only that one, so frame N-1's data survives through frame N. Size each buffer from the measured peak frame and decide what overflow does.

code

pseudocode · 25 lines
pseudocode
FrameAllocator
  base:     address        // reserved at startup, aligned to the largest align used
  capacity: int
  offset:   int = 0

  alloc(size, align):      // align is a power of two
    start = (offset + align - 1) AND NOT (align - 1)
    if start + size > capacity:
      return NONE          // overflow policy decides what happens next
    offset = start + size
    return base + start

  reset():                 // once per frame
    offset = 0

DoubleFrameAllocator
  buffers: [FrameAllocator, FrameAllocator]
  current: FrameAllocator

  beginFrame(frameIndex):
    current = buffers[frameIndex mod 2]
    current.reset()        // discards data from frame frameIndex - 2 only

  alloc(size, align):
    return current.alloc(size, align)

go deeper

for a junior

Know that the allocator moves an offset forward through one buffer and frees everything by setting the offset back to zero once a frame.

for a middle

Explain the align-up formula, the capacity check, the reset, and why data read one frame later needs two alternating buffers.

for a senior

Talk through sizing from measured peaks, overflow policy, per-thread buffers and the debug fill pattern that exposes pointers kept past a reset.

for a principal

Weigh how much frame memory each platform gets against the pressure it removes from the general heap, and when a deeper lag justifies a third buffer.

## Why a frame needs its own allocator Every frame an engine produces a burst of **temporary data**: the results of a spatial query, scratch arrays for sorting, strings for on-screen debug text, lists of things to process later in the frame. All of it is dead by the next frame. Allocating and freeing each piece from the **general-purpose heap** costs a heap call per item and, worse, risks the allocator's slow path inside the frame budget. A **per-frame linear allocator** (also called a bump or stack-style frame allocator) exploits the one fact the general heap cannot know: everything allocated this frame dies together. ## The bump pointer, step by step The allocator owns one buffer reserved at startup, a `capacity`, and an `offset` that starts at zero. 1. **Align up.** Round `offset` up to the requested alignment `align` (a power of two): `start = (offset + align - 1) AND NOT (align - 1)`. With `offset = 13` and `align = 16`, `13 + 15 = 28`, and clearing the low four bits gives `16`. 2. **Check capacity.** If `start + size > capacity`, the frame has overflowed; the engine's overflow policy decides what happens. 3. **Bump.** Set `offset = start + size` and return `base + start`. A 32-byte request at `start = 16` leaves `offset = 48`. 4. **Reset.** Once per frame, set `offset = 0`. Every allocation of the frame is freed by that single store. Aligning the offset aligns the address only if `base` itself is aligned to the largest alignment anyone will request, so the buffer is reserved with that alignment. ## Alignment for SIMD **SIMD** instructions load several floats at once: a 128-bit vector holds four 32-bit floats and wants **16-byte alignment**, a 256-bit vector wants 32. Some instruction sets offer aligned loads that fault on a misaligned address, and a misaligned load that straddles a cache-line boundary costs extra on many processors. That is why the allocator takes an `align` argument instead of assuming one: particle positions or skinning scratch data headed for vector code ask for 16 or 32, a list of 32-bit integers asks for 4. ## The double-buffered variant A single buffer is enough when data is produced and consumed within one frame. It breaks when a consumer runs **one frame later**, for example when the simulation gathers hit events during frame N and gameplay handles them at the start of frame N+1: the reset at the start of N+1 would wipe them before they are read. A **double-buffered frame allocator** keeps two buffers and alternates: | Frame | Allocates from | Resets at frame start | Still valid during the frame | |---|---|---|---| | 10 | buffer 0 | buffer 0 (frame 8's data) | frame 9's data in buffer 1 | | 11 | buffer 1 | buffer 1 (frame 9's data) | frame 10's data in buffer 0 | | 12 | buffer 0 | buffer 0 (frame 10's data) | frame 11's data in buffer 1 | The rule generalises: a consumer that lags `k` frames needs `k + 1` buffers in a ring. Each extra buffer costs a full buffer's worth of memory, so the lag is kept as small as the design allows. ## Overflow and sizing - **Size from the peak**, not the average: record the highest offset reached per frame in development builds and size the buffer from that plus headroom. - **Overflow policy**: assert in development builds, and in shipping builds either fall back to a slower allocator for that frame and log it, or chain a second block. Silently returning garbage is never an option. - **Per-thread buffers** are the usual answer when several worker threads allocate frame data, since one shared offset would need synchronisation on every allocation. ## The traps - **Keeping a pointer past the reset.** Nothing tracks it; the pointer may silently read whatever a later frame wrote there. Development builds often fill the buffer with a recognisable byte pattern on reset so such reads show up as obvious garbage. - **Objects that need cleanup.** Nothing runs per-object teardown on reset, so an object that owns another resource (a file, a pooled slot, a heap block) must not live in frame memory. - **Growing containers.** A dynamic array allocated from frame memory that doubles its capacity leaves its old storage stranded until the reset; reserve the needed size once. - **Data that outlives the frame by accident.** A cache that stores a frame-allocated string turns a temporary into a dangling reference; copy it into longer-lived memory instead.

  • Why not free frame data individually with the general heap and skip the reset trick?
    Individual frees cost a heap call per item and can hit the allocator's slow path mid-frame, and thousands of short-lived blocks fragment the general heap. A bump allocator's allocate is an add and a compare, its free-all is one store, and its memory is reserved once, so its cost per frame is small and predictable.
  • What happens if a consumer starts reading frame data two frames late with a double buffer?
    By then the producer's buffer has been reset and refilled: frame N's data sits in buffer N mod 2, which frame N+2 resets. The consumer reads newer data or garbage. Either the lag is cut back to one frame or the ring grows to three buffers, at the cost of a third buffer's memory.
  • How does a development build catch reads of frame memory after the reset?
    On reset it fills the buffer with a distinctive byte pattern, so a stale read yields obviously wrong values or asserts in validation code. Some engines also stamp each allocation with its frame number and check it on access in debug builds.

A frame allocator is a whiteboard wiped clean at the start of every meeting: writing is fast because nobody erases one note at a time. If a note must survive into the next meeting, you need a second whiteboard and to wipe the two alternately.

  • A per-frame allocator frees each allocation individually at end of frame
  • Resetting the offset leaves last frame's data safe to read
  • A single buffer works for data consumed one frame later
  • Alignment does not matter because the allocator packs bytes tightly
  • Objects with destructors can live in frame memory safely