liveBook · Manning

16-byte guideline

As explained in High Performance Unity Game Development

A practical recommendation to keep structs at or below 16 bytes to limit copying costs and misuse.

Keep structs at or below 16 bytes as a practical guideline: they are passed by value by default, so large structs can copy more data and be easier to misuse. This is not a hard limit; performance depends on the CPU, compiler, runtime, and calling convention. Larger structs can still work, especially when passed by ref. section 2.1

Small value data and EnemyData

Vector2 (8 bytes) and Vector4 (16 bytes) are examples of small structs. EnemyData is better represented as a class: it contains arrays, combines data not always used together, and exceeds 16 bytes. section 2.1

Listing 2.1 C# class holding all the data for all enemies
public class EnemyData
{
    public Vector2[] Position;
    public Vector2[] Direction;
    public float[] Velocity;
}

The EnemyData listing shows its position, direction, and velocity arrays. section 2.1

How High Performance Unity Game Development covers it

How deep it goes
Defines a rule of thumb, explains copying and hardware/runtime caveats, and rejects a universal size limit.
Examples
Uses Vector2 and Vector4 as small structs and EnemyData as a class contrast.
What you can do after
Evaluate struct suitability by checking size, field use, and copying risk.
How it connects
Connects structs to value copying, boxing, classes, and passing by reference.

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.