liveBook · Manning

Chapter 2: Structuring data for performance

What you will learn in chapter 2 of High Performance Unity Game Development

The chapter introduction connects array-based data locality to performance and previews a closer look at how data is stored.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction connects array-based data locality to performance and previews a closer look at how data is stored.

Key terms: data locality, arrays, cache misses

2.1 Struct vs. class - how data affects performance

The section uses EnemyData and Vector2 to explain when C# classes and structs suit game data, with performance implications for copying, allocation, and array layout.

Key terms: struct, Vector2, primitive types, fixed-size value data, EnemyData, class, arrays, 16 bytes, passed by value, class reference

2.2 Stack vs heap - how memory affects performance

This section introduces the stack and heap as the two places where memory is allocated and explains why allocation location matters in data-oriented design.

Key terms: stack, heap, DOD, data locality

2.2.1 The stack

This section uses a simplified stack model to explain the short lifetime of function-local data, nested calls, and the stack’s performance benefits and size limit.

Key terms: stack, LIFO, stack frame, local data, EnemyData, reference, heap, Vector2, struct, nested functions

2.2.2 The heap

This section explains what the heap stores in the Unity example and why its allocation behavior matters for locality, fragmentation, and performance.

Key terms: heap, EnemyData, arrays, strings, class member variables, NumEnemies, data locality, cache misses, managed heap, memory fragmentation

2.3 Passing by value vs. by reference - performance and common issues

Explains what C# copies when passing value and reference types to functions, and why copying structs can cause bugs and performance costs.

Key terms: passed by value, value type, reference type, struct, reference, value types, copy, performance

2.3.1 Copying by value and common issues

This section uses an XP-update bug to show how passing a struct by value differs from passing it by reference, and when using ref is appropriate.

Key terms: struct, passing by value, GameDataStruct, AddXP(), XP, ref keyword, passing by reference, memory address, ref, large structs

2.3.2 Copying by value and performance

This section explains how struct size can affect the cost of passing by value and why that cost may differ between Unity Editor and device builds.

Key terms: passing by value, struct, 16 bytes, Mono, IL2CPP, Unity Editor

2.4 The intricacies of struct allocations in C#

This section compares temporary struct and class allocations, then notes that a struct is not always allocated on the stack.

Key terms: Vector2, randomPosition, EnemyPoint, stack, heap, garbage collector, structs, member variables, heap-allocated object, class

2.4.1 Structs as member variables

This section explains where struct members are allocated and how passing structs or their parent class affects mutation, performance, and the clarity of function calls.

Key terms: struct, member variable, heap, stack, GameData, PlayerPosition, passing by value, ref, MovePlayerHorizontally(), class

2.4.2 Structs with arrays

This section explains why putting arrays in a stack-allocated struct still requires separate heap allocations for the array data.

Key terms: EnemyData, struct, arrays, stack, heap, new

2.4.3 Structs with Strings

This subsection explains why a string field in a struct still requires heap allocation and what happens when that string is changed.

Key terms: struct, string, heap, stack, garbage collector

2.5 Data locality and contiguous memory - arrays of structs vs arrays of classes

The section explains when an array of structs is useful in data-oriented design and how its memory layout differs from an array of classes.

Key terms: DOD, data locality, arrays of structs, separate arrays, CoinStructArray, CoinClassArray, heap, contiguous memory, cache line, cache misses

2.6 Data types and Performance

The section introduces why smaller array element types can save cache-line space and storage while warning that they do not necessarily improve performance.

Key terms: data arrays, byte, int, cache line

2.6.1 Performance

This subsection explains why integer type performance depends on the device and how to choose types for gameplay data without assuming smaller is faster.

Key terms: CPU architecture, byte, short, int, long, profiling, target hardware, minimum target device, network bandwidth, cache density

2.6.2 Memory

This section explains how to choose integer types for game data based on value range, memory and network needs, and measured runtime performance.

Key terms: byte, int, storage size, network bandwidth, EnemyData, hit point array, short, ushort, minimum target device

2.7 Conclusion

This conclusion brings together how data layout, memory location, function passing, and numeric type choices affect performance.

Key terms: data locality, struct, class, stack, heap, value types, reference types, passed by value, ref keyword, byte

2.8 Summary

The section summarizes how C# data types, memory placement, and copying choices affect data locality and performance in Unity.

Where this is used later

Concepts explained in this chapter

← Chapter 1: Understanding data-oriented design Chapter 3: Memory allocations and performance →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.