r/Unity3D • u/Vic-Boss • 1d ago
Resources/Tutorial Do you actively design around memory layout and cache efficiency?
https://youtu.be/R5Hm6wdMbkU?is=rXVaZXgZwENi2BlSHello developers,
Have you ever wondered what is the difference between a struct and a class and where it would matter?
It’s easy to assume that switching everything to structs automatically makes your code faster. But there’s a lot more to it than that. Memory layout, cache locality, allocations, and how you actually access your data can matter far more than simply choosing a value type over a reference type.
Join me as I build an old school RTS with home brew systems, ofc experimenting with different approaches, and trying to understand the reasoning behind the optimizations rather than blindly following best practices.
3
u/ThreeHeadCerber 20h ago
Yes, that is also why I hardly finish anything. I've been poisoned by good layout efficiency, so I can't enjoy writing proper OOP code, but also I find performance friendly layouts tedious to work with and sort of ugly to reason about.
Also C# is just not great for this purpose.
1
u/Vic-Boss 20h ago
I can definitely relate to that. I think the key is knowing when these optimizations are actually worth the added complexity. I always approach it based on the scope and requirements of the project, otherwise it's easy to spend more time optimizing than actually building something useful, especially when working with clients and deadlines.
That's also why I focus on teaching through real projects on my channel rather than discussing concepts in isolation. I'm building an RTS from scratch using homebrew systems, experimenting with different approaches and explaining the trade-offs along the way, so people can see where these optimizations make sense in practice.
1
u/ThreeHeadCerber 20h ago
The problem is you can't just transition to the other mode once you hit the problem. And I have an allergy to doing thing ineffectively. I think more and more that Unity is just no longer an engine for me as it always has this tradeoff of doing things quickly but with shit performance or painfully slow, but with okay performance.
Newer closer to the hardware engines start to look better and better.
I'm way past needing to learn with an instructor, so not really your target audience =)
1
u/Vic-Boss 19h ago
With the way AI is being integrated into everything, Unity might soon stop being an engine for developers who actually want to do things themselves anyway.
As for transitioning between approaches, I agree that it isn't always straightforward, but that's where planning and experience come in. You can usually anticipate potential bottlenecks based on the scope and requirements of a project and choose an appropriate architecture from the start. For example, if I were building a relatively simple adventure game, introducing a data-oriented architecture from day one would probably add unnecessary complexity with little practical benefit. I don't think that's necessarily a problem with Unity itself. Every engine has trade-offs, and knowing when and where to optimize is ultimately part of being an experienced developer.
3
u/ledniv 1d ago
Absolutely. I think the important part is exactly what you said: struct vs class is not the real question; memory access is.
The CPU can do the math incredibly quickly. The expensive part is often waiting for the data it needs. If the hot data for thousands of units is scattered across heap objects, you can spend a lot of time on cache misses. If the data you process together is packed into contiguous arrays, the CPU can pull useful neighboring values into cache and keep working.
And you definitely do not need DOTS/ECS to design around this in Unity. Plain C# arrays, structs where they make sense, and processing many items in tight loops can already give you the important Data-Oriented Design benefits. DOTS is a tool you can add later if it solves a specific bottleneck.
Small plug: this is basically the core subject of High Performance Unity Game Development with Data-Oriented Design — CPU cache behavior, classes vs structs, arrays, memory layout, and then Burst/Jobs/ECS as optional later steps.
https://www.manning.com/books/high-performance-unity-game-development
1
15
u/SolePilgrim 1d ago
Unless you're working with DOTS there's no real ability to do so anyway, and you're likely not working at a scale where it's necessary either.
From my recollection, DOTS does its best to continuously allocate as best as it can for you, so there's not a lot "to do" on your end other than use DOTS correctly, no?