Chapter 2: Structuring data for performance
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
- Array Locality and Cache Misses. Explain why arrays are presented as a simple way to improve performance through data locality.
- Small cohesive struct values. Explain why Vector2 is a suitable struct for enemy positions and directions: its two primitive float fields are small and usually used together.
- EnemyData class choice. Justify making EnemyData a class rather than a struct using the kinds, usage, and size of the data it holds.
- Allocation location and performance. Explain that a variable or object is allocated on either the stack or the heap, and that knowing where it is allocated helps explain performance implications and data locality in DOD.
- Stack order and local lifetimes. Explain how the stack’s last-in-first-out organization fits temporary function and loop data, and why a function’s local stack space can be reused after it returns.
- Nested calls and stack growth. Trace how EnemyInRange() and distance() use temporary local data during a nested call, and explain why repeated recursive calls can overflow a small stack.
- Heap storage and lifetime. Explain why the long-lived EnemyData class is stored on the heap and identify arrays and strings as dynamically allocated heap data.
- Heap placement and locality. Explain why heap allocation can make data locality difficult and why allocation choices matter for cache misses.
- Fragmentation and collection risks. Explain how repeated allocation and deallocation on Unity's non-compacted managed heap can cause fragmentation, and state the performance concern about triggering garbage collection.
- Value and reference arguments. Distinguish what is copied when a value type versus a reference type is passed to a function in C# by default, and describe what each copy means for the original data.
- Struct copying consequences. Explain the bug risk and performance cost that can follow from passing structs to functions.
- Struct copy XP bug. Trace what happens when AddXP() receives GameDataStruct by value and explain why the original XP does not change.
- Reference passing and original data. Explain how the C# ref keyword changes the AddXP() example and its resulting XP value.
- Struct size and copy cost. Explain why passing a larger struct by value can cost more, and describe the section’s roughly 16-byte guideline and measured results.
- Temporary position allocation. Trace what happens when the example uses a local Vector2 for an enemy’s random position, and contrast it with using an EnemyPoint class object.
- Struct member memory placement. Explain why a Vector2 stored as PlayerPosition in the GameData class is on the heap, and why a stack-allocated struct can still have heap-allocated arrays or strings.
- Struct mutation and copying. Explain why passing PlayerPosition by value does not move the original player, and describe the two ways shown to update it and their stated performance difference.
- Struct parameter intent signals. Explain how ref in a struct parameter helps a reader tell which variables a function intends to modify, using position and spawnPosition as the example.
- Arrays inside a struct. Explain where an EnemyData struct and its arrays are allocated when the struct is created inside a function, and why the arrays diminish the benefit of stack allocation.
- String fields and heap allocation. Explain why putting a string in a struct does not keep the string’s data on the stack, and why the section advises against adding strings to structs.
- Choosing arrays of structs. Explain why DOD usually favors separate arrays, and when an array of structs is an appropriate exception for data used together by one piece of logic.
- Struct and class array layouts. Trace what an array allocation contains for structs versus classes, and explain the resulting difference in data locality and cache misses.
- Smaller Data Type Tradeoff. Explain why a byte may seem attractive instead of an int for a data array, and why that choice does not guarantee a performance improvement.
- Device-dependent integer performance. Explain that CPU architecture can make integer types perform differently across devices, so a smaller type is not necessarily faster.
- Practical integer type choices. Describe when to default to int for whole-number gameplay data and when a smaller type may be worth using or testing.
- Value range and signedness. Describe how to choose a type by finding the largest value the data may reach and checking whether negative values are possible.
- Memory versus runtime performance. Explain why a smaller integer type is not automatically the best choice for runtime performance and how the section advises checking that tradeoff.
- Data layout and storage choices. Explain when this section favors a struct or a class, and why stack and heap storage have different performance implications.
- Copies at function calls. Distinguish what is copied when a struct is passed by value from what is copied when an array or string is passed by value, and describe the role of ref.
What it builds on
- Using data locality to reduce cache misses (chapter 1) · How data locality can reduce cache misses.
- How cache lines move data into the CPU cache (chapter 1) · Introduces why the data on a cache line matters.
Section by section
Introduction
The chapter introduction connects array-based data locality to performance and previews a closer look at how data is stored.
- Array Locality and Cache Misses: Explain why arrays are presented as a simple way to improve performance through data locality.
Why it matters: This connects the chapter’s focus on array data to the performance benefit introduced in Chapter 1.
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.
- Small cohesive struct values: Explain why Vector2 is a suitable struct for enemy positions and directions: its two primitive float fields are small and usually used together.
Why it matters: It gives you a concrete test for grouping related game data into a struct. - EnemyData class choice: Justify making EnemyData a class rather than a struct using the kinds, usage, and size of the data it holds.
Why it matters: It explains why the container for many enemy-data arrays is treated differently from each Vector2 value. - Value copying costs: Distinguish what is copied when a struct versus a class is passed as an argument, and explain why struct size is a practical performance guideline rather than a strict limit.
Why it matters: It helps explain both the small-struct recommendation and why editor performance may differ from device performance. - Array memory locality: Explain the layout difference between arrays of structs and arrays of classes and its consequence for planning cache-line contents.
Why it matters: It connects the struct-versus-class decision to the chapter’s data-locality concern. - Struct allocation caveat: Distinguish the section’s temporary-versus-long-lived data guidance from the mistaken claim that every struct is stored on the stack.
Why it matters: It prevents a useful design guideline from becoming an incorrect rule about where every struct lives.
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.
- Allocation location and performance: Explain that a variable or object is allocated on either the stack or the heap, and that knowing where it is allocated helps explain performance implications and data locality in DOD.
Why it matters: It gives you a reason to pay attention to where game data is allocated before considering specific memory behavior.
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.
- Stack order and local lifetimes: Explain how the stack’s last-in-first-out organization fits temporary function and loop data, and why a function’s local stack space can be reused after it returns.
Why it matters: It gives you a useful model for the lifetime of temporary data without treating the diagrams as exact runtime behavior. - Local copies and heap objects: Distinguish the local copy of the EnemyData reference from the heap object it refers to, and describe how the Vector2 playerPosition is represented in the section’s simplified function example.
Why it matters: It prevents confusing where a function keeps a reference with where the referenced object lives. - Nested calls and stack growth: Trace how EnemyInRange() and distance() use temporary local data during a nested call, and explain why repeated recursive calls can overflow a small stack.
Why it matters: Nested-call lifetimes explain both ordinary stack reuse and the risk of deep recursion on devices with small stacks. - Stack allocation and data locality: Explain the performance advantage the section attributes to stack allocation and contiguous temporary data.
Why it matters: It connects the stack’s organization to the chapter’s performance focus.
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.
- Heap storage and lifetime: Explain why the long-lived EnemyData class is stored on the heap and identify arrays and strings as dynamically allocated heap data.
Why it matters: The lifetime of game data helps explain why much of it resides on the heap. - Heap-resident member variables: Describe why a NumEnemies member added to EnemyData would be allocated on the heap in the example.
Why it matters: It connects the location of the example object with the location of data it contains. - Heap placement and locality: Explain why heap allocation can make data locality difficult and why allocation choices matter for cache misses.
Why it matters: Long-lived heap data can still perform poorly if the data being accessed is scattered. - Fragmentation and collection risks: Explain how repeated allocation and deallocation on Unity's non-compacted managed heap can cause fragmentation, and state the performance concern about triggering garbage collection.
Why it matters: Heap allocation choices can affect both whether an allocation succeeds and how smoothly the game runs.
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.
- Value and reference arguments: Distinguish what is copied when a value type versus a reference type is passed to a function in C# by default, and describe what each copy means for the original data.
Why it matters: The distinction determines whether a function receives separate data or another reference to the same object. - Struct copying consequences: Explain the bug risk and performance cost that can follow from passing structs to functions.
Why it matters: Remembering that a function receives a struct copy helps explain both unexpected unchanged data and copying overhead.
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.
- Struct copy XP bug: Trace what happens when AddXP() receives GameDataStruct by value and explain why the original XP does not change.
Why it matters: Recognizing which value a function changes helps you catch updates that appear to succeed but never reach game data. - Reference passing and original data: Explain how the C# ref keyword changes the AddXP() example and its resulting XP value.
Why it matters: This distinction lets you deliberately update the caller's value instead of an expendable copy. - Limits of using ref: Justify why ref is not a general reason to make game data an oversized struct.
Why it matters: Avoiding a copy does not remove the design and maintenance costs of an oversized struct.
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.
- Struct size and copy cost: Explain why passing a larger struct by value can cost more, and describe the section’s roughly 16-byte guideline and measured results.
Why it matters: Struct size can matter when a value is passed repeatedly. - Mono and IL2CPP results: Distinguish the reported Mono and IL2CPP results, and explain why Unity Editor measurements may not match performance on a target device.
Why it matters: A performance change measured in the Editor may not appear in a device build.
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.
- Temporary position allocation: Trace what happens when the example uses a local Vector2 for an enemy’s random position, and contrast it with using an EnemyPoint class object.
Why it matters: The example shows why the book favors stack-allocated structs for temporary data. - Struct allocation exception: Explain why being a struct does not, by itself, mean a value is allocated on the stack.
Why it matters: This qualification prevents treating every struct as an automatically removed stack variable.
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.
- Struct member memory placement: Explain why a Vector2 stored as PlayerPosition in the GameData class is on the heap, and why a stack-allocated struct can still have heap-allocated arrays or strings.
Why it matters: It prevents the mistaken assumption that choosing a struct keeps all associated data on the stack. - Struct mutation and copying: Explain why passing PlayerPosition by value does not move the original player, and describe the two ways shown to update it and their stated performance difference.
Why it matters: It helps you update the intended game state without unnecessary copying. - Whole class reference tradeoff: Describe what happens when the entire GameData class instance is passed to a function and why that convenience makes the function's effects less clear.
Why it matters: It highlights a clarity cost when a function receives an entire data object. - Struct parameter intent signals: Explain how ref in a struct parameter helps a reader tell which variables a function intends to modify, using position and spawnPosition as the example.
Why it matters: A function signature can make intended changes visible without requiring a reader to inspect its body.
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.
- Arrays inside a struct: Explain where an EnemyData struct and its arrays are allocated when the struct is created inside a function, and why the arrays diminish the benefit of stack allocation.
Why it matters: Choosing a struct does not put the data in its arrays on the stack.
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.
- String fields and heap allocation: Explain why putting a string in a struct does not keep the string’s data on the stack, and why the section advises against adding strings to structs.
Why it matters: A struct does not make every piece of data associated with it stack-allocated. - Name changes and old strings: Trace what happens when an enemy’s string name is changed, including what happens to the old string’s memory.
Why it matters: Changing a name does not immediately free the memory used by its previous value.
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.
- Choosing arrays of structs: Explain why DOD usually favors separate arrays, and when an array of structs is an appropriate exception for data used together by one piece of logic.
Why it matters: It helps you decide when grouping data improves locality rather than working against it. - Struct and class array layouts: Trace what an array allocation contains for structs versus classes, and explain the resulting difference in data locality and cache misses.
Why it matters: The layout explains why choosing a struct array can reduce cache misses for data processed together. - Limits of struct contiguity: Explain why adding a string or array field prevents all of a struct array's data from remaining contiguous.
Why it matters: It prevents treating every field of a struct array as though its data sits together in memory.
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.
- Smaller Data Type Tradeoff: Explain why a byte may seem attractive instead of an int for a data array, and why that choice does not guarantee a performance improvement.
Why it matters: It keeps a space-saving choice from being mistaken for a guaranteed speed optimization.
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.
- Device-dependent integer performance: Explain that CPU architecture can make integer types perform differently across devices, so a smaller type is not necessarily faster.
Why it matters: Choosing a narrower type on the assumption that it runs faster can hurt performance. - Practical integer type choices: Describe when to default to int for whole-number gameplay data and when a smaller type may be worth using or testing.
Why it matters: This rule balances runtime performance against the real space and bandwidth benefits of smaller types.
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.
- Data size and transfer costs: Explain why a smaller data type can be worthwhile when game data is saved or sent to a server.
Why it matters: The best type depends partly on whether storage and bandwidth are important to your game. - Value range and signedness: Describe how to choose a type by finding the largest value the data may reach and checking whether negative values are possible.
Why it matters: Checking the range lets you save space without choosing a type that cannot represent the game’s values. - Memory versus runtime performance: Explain why a smaller integer type is not automatically the best choice for runtime performance and how the section advises checking that tradeoff.
Why it matters: A memory saving is only useful when it fits the game’s actual performance needs.
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.
- Data layout and storage choices: Explain when this section favors a struct or a class, and why stack and heap storage have different performance implications.
Why it matters: Choosing a data representation affects both where data can live and how readily it can be accessed together. - Copies at function calls: Distinguish what is copied when a struct is passed by value from what is copied when an array or string is passed by value, and describe the role of ref.
Why it matters: The distinction explains why struct size matters when values are passed to functions. - Whole number type tradeoffs: Explain the storage and performance tradeoff between smaller whole-number types and integers, including why target-device testing matters.
Why it matters: A smaller type does not automatically make game code faster.
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
- Memory fragmentation (chapter 3) · Develops the fragmentation problem introduced here.
- Avoiding garbage collector related slowdowns (chapter 3) · Develops the garbage-collector performance concern introduced here.
- AllocateGameData() - Allocating all the data for our game (chapter 4) · The distinction between passing values and passing references.
- The Balance (chapter 4) · The performance implications of copying by value.
- The Logic (chapter 4) · Provides a basis for reasoning about how gameplay functions modify data.
- GC Issue #1 — memory isn’t freed immediately (chapter 3) · the discussion of memory that is not freed immediately
- Functions called by Tick() (chapter 4) · The performance benefit of storing data in contiguous arrays.
- (chapter intro) (chapter 6) · Data locality and contiguous memory.
Concepts explained in this chapter
- structA value-type language construct that groups related data fields and may define associated behavior.
- pass by valueA parameter-passing behavior in which a function receives a copy of an argument's value.
- classA language construct that defines the data and behavior of objects created from that type.
- stack memoryA region of runtime memory typically used for function call frames and short-lived local values.
- array of classesAn array whose elements are references to class instances allocated separately on the heap.
- array of structsAn array whose elements are struct values stored together in the array's contiguous memory block.
- heapA region of runtime memory used for dynamically allocated data whose lifetime is managed separately from function call frames.
- ref keywordThe C# ref keyword specifies that an argument is passed by reference so the called code can access the caller's variable directly.
- value typeA data type whose values are copied directly when passed or assigned, rather than passing a reference to the same object.
- pass by referenceA parameter-passing behavior in which a function receives access to the original argument or its storage location rather than an independent copy.
- data localityThe degree to which related data is stored or accessed close together in the memory hierarchy.
- 16-byte guidelineA practical recommendation to keep structs at or below 16 bytes to limit copying costs and misuse.
- data typeA specification of the kind of value a program element can represent and the operations and storage interpretation associated with it.
- stringA string is a sequence of text characters represented as a value that can be stored and manipulated by a program.
- ShortA fixed-width integer data type for representing relatively small whole-number values.
- primitive typeA fundamental built-in data type representing basic values directly supported by a programming language.
- memory fragmentationMemory fragmentation is the condition in which available memory is divided into separated holes that may not accommodate a requested contiguous allocation.
- arrayAn indexed sequence of elements, typically stored in contiguous memory.
- temporary dataRuntime data whose lifetime spans only a short operation or update rather than the entire gameplay session.
- cache missA memory access in which the requested data is not found in a processor cache and must be retrieved from another memory level.
- contiguous memory layoutA region of memory in which elements occupy adjacent addresses without gaps between them.
- first-in-last-out structureAn ordering discipline in which the most recently added item is the first removed.
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.
