List of expensive things

So while learning scripting you get bits and pieces of what’s expensive on the CPU, but there doesn’t seem to be a comprehensive list to see it in one go. I don’t expect to get the exact nuances from a single thread, but it would be nice to get a starting point to think on how to optimize.

Anyway, here’s what I know to be expensive (please correct me if I’m wrong):

1) Rigid bodies, gravity, and physics - I see a lot of tutorials that have you use these for simple games, such as shoot-em-ups, when it is absolutely unnecessary and overkill.

2) GetComponent - I understand that it is important not to use this in each frame and it is better to declare it outside of updates.

3) Instantiate and Destroy - I also understand that it is better to disable and enable an object than to instantiate and destroy it whenever possible. I am, however, not entirely clear on how cloned objects work.

4) Loops - I’ve read on some threads that a lot of people get massive frame rate issues due to an out of control loop. Not that loops are inherently bad, just that they can pile on the CPU fast.

5) Not Using a Texture/Sprite Atlas - Wow, this one had me screwed up for a while. It is way, way, way cheaper and faster to use one giant texture than it is to do multiple ones.

So those are five things that I know of that can get expensive to use. Does anyone else have things that they know of to add to the list?

I’d really like to learn some new things to look out for and keep in mind.

Regarding 4) Loops:
Don’t use foreach() if you can use a for() loop. Foreach uses boxing, which generates unneccessary pressure on the Garbage Collector.

This depends, some (or most) collections in the .Net framework use structs as their enumerators, which doesn’t need an allocation on every iteration. When you’re using for loops, you are sure that it isn’t creating garbage. But using for instead of foreach might be less clear in some occasions

This aren’t Hardset rules depends on the type of collection used in the loop, the physics system can be more optimized than implemeanting your own at times, and texture atlas can have negative performance impact at times since if there is large section of the atlas not in use you are still spending memory on them anyway.

You have to use what is best for the situation at hand, and also don’t waste time optomizing everything before you know what your projects bottle necks are.

1 Like

3) Instantiate and Destroy
It’s not how cloned objects work, but rather the process of creating and destroying objects is quite slow. When dealing with lots of objects, pooling is a way to reuse them rather than constantly destroying and creating new ones.

6) I’m not 100% sure on the specifics, but I hear that using many meshes (especially with their own textures) is waaay slower than 1 giant mesh with 1 giant texture. I believe the ‘static’ has something to do with this as well? Would love more info on how to optimize meshes and models in the scene.

That’s texture atlasing. And it has to do with draw calls.