DOD — Unity Memory Best Practices — Part 8

Nitzan Wilnai
2 min readJun 6, 2024

--

Struct Performance Issues

Because structs are passed by value, the entire struct needs to be copied every time the function is called. In fact the bigger the struct, the slower it will take.

That means just passing a struct to a function is a performance hit!

Here is a quick test passing various sized structs several thousand times:

You can see in the table above that every 4 additional bytes added to a struct results in ~0.05% performance hit. Microsoft recommends using structs only if your data is at most 16 bytes, and you can see there is a bigger hit when the struct size increases from 16 to 20 bytes.

When to use Structs

So when should we use structs?

We should use structs if we have data that needs to be used together.

Unity’s Vector2, Vector3 and Vector4 are great examples.

Let’s look at Vector4 because it is the largest.

Vector4’s data is x, y, z and w:

public Struct Vector4
{
float x;
float y;
float z;
float w;
}

When using a Vector4, whether you are multiplying or calculating dot product, the methods uses all 4 variables:

public static Vector4 operation * (Vector4 left, Vector4 right)
{
return new Vector4(left.x * right.x, left.y * right.y, left.z * right.z, left.w * right.w);
}

public static float Dot(Vector4 vector1, Vector4 vector2)
{
return vector1.x * vector2.x + vector1.y * vector2.y + vector1.z * vector2.z + vector1.w * vector2.w;
}

In both these cases we want all the variables of both Vectors in the cache line.

Conclusion

  • When allocating memory, do it all at once when loading your game or a level.
  • If you can’t do it all at once, make sure you deallocate your data in the reverse order that you allocated it.
  • Avoid triggering the Garbage Collector.
  • If you must trigger the Garbage Collector, do it when you can hide the performance hit, like during loading screens or when switching menus.
  • Only use structs if your data is stack allocated, no larger than 16 bytes, or if the data is often used together.
  • It is better to use arrays of structs because then your data is guaranteed to be sequential in memory and will be on the same cache line.

Special thanks to Ray Graham, Guy Tidhar, Chad Walker and Ryan Chanlatte for helping out with these articles.

--

--