liveBook · Manning

Contiguous memory layout

As explained in High Performance Unity Game Development

A region of memory in which elements occupy adjacent addresses without gaps between them.

Contiguous memory places elements at adjacent addresses. Arrays of structs and primitive values can use this layout, helping keep related data local and reduce cache misses during access. section 2.8 section 6.1.1

In game data structures, layout affects how efficiently data can be traversed:

  • A C# List stores values in an array, while a linked list’s nodes may be scattered across heap memory. Linked lists allow easy insertion and removal, but traversal can be slow. section 6.1.1
  • Heap fragmentation can leave enough total free memory but no single contiguous region large enough for an allocation. Group related allocations and those with similar lifetimes, and release them together when possible. section 3.1

Fragmented arrays

Allocate arrays of 1 KB, 2 KB, and 1 KB; then delete the first and third. The two 1 KB holes total 2 KB, but neither can fit a 1.5 KB array. section 3.1

Figure 6.2 C# Lists use an array that is stored in a single contiguous block of memory, while Linked Lists allocate nodes wherever there is room, breaking data locality.

Compare the single-block storage of a C# List with linked-list nodes scattered across heap memory. section 6.1.1

How High Performance Unity Game Development covers it

How deep it goes
Explains array layouts, cache effects, and heap fragmentation with a worked allocation example.
Examples
Uses arrays of different sizes to demonstrate fragmentation and contrasts List storage with linked-list nodes.
What you can do after
Apply it by choosing contiguous arrays for frequently traversed values and grouping related heap allocations by lifetime.
How it connects
Connects data locality to arrays, structs, cache misses, heap fragmentation, and linked lists.

Also explained in

Compare how 3 books explain contiguous memory layout →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of High Performance Unity Game Development. The section links point to the parts of the book it draws on.