How does game “optimization” work? Are people sitting there changing lines of code to more “optimal” ones? What is “optimized”?


The recent The Last of Us for PC made me realize I had no idea what’s meant by “optimizing” a game.

Same with optifine in Minecraft improving performance. How do these things work to just make games use fewer resources?

In: 158

Yes, code could be optimized through decreasing algorithms complexity (for example you make fewer loops inside the loops and replace this with another construction), you can remove objects that are taking too much space or unnecessary calls, clean up objects as soon as they are not used, etc.

Also graphics can be optimized, by adding occlusion culling and managing the level of details, but I don’t know much about it.

It’s complicated. Even game developers clearly don’t get it right all the time. But a simplified example would be – don’t load textures into RAM before you actually need them. This will reduce the amount of RAM (or video memory – VRAM) required. Because high VRAM usage is a known problem with The Last of Us, this seems like a good solution. But let’s say you didn’t load the textures needed to display a certain building or enemy. Then the player turns a corner, it’s supposed to come into view, but there would be a brief decrease in performance or delay before it could be displayed, because you didn’t load the texture ahead of time and suddenly you need it. So this “good idea” of being conservative with the textures you load ahead of time has become a problem…

Perhaps you could check where the player is to help determine what they are *likely* to see or where they are *likely* to go, now you can load the textures they actually need without missing any or loading excess ones they don’t need. Seems like a good solution right? Problem is, that’s additional work the computer needs to do, and players can be unpredictable in the directions they travel or look, so all that work could just lead to the computer making a wrong prediction about what stuff it should or should not prepare for.

Besides loading textures, there is other work that a game can skip for items that are too far away to see or obscured by other objects. But what if the player comes around a corner and suddenly can see those objects again? It’s a balancing act to get this right and try to predict what work is valuable or wasted.

So it’s a balance of – do stuff that is necessary – don’t do stuff that is unnecessary – don’t mess that up. It’s going to be different for every game and no single solution will give the best possible performance for all players who go through the game differently. So it will never be *perfect* and getting it *good enough* requires a lot of work and testing. If you don’t budget and allow time for it, it’s not going to happen automatically by clicking an “optimize” button right before you release the game.

There are uncountable ways of getting the same outcome, and some of those ways are faster than others.

Take a list of numbers, for example, which is in random order, and you want to sort it. It’s a fairly common problem, but how do you sort a list, exactly? A naive approach for making a list ordered might be just “Look at the first two elements, then swap their positions if they’re not in order, then move to the next two. Loop and repeat until you can’t make any more swaps.” For a small list, that works, and if you’re on your 80th workhour that week, that might be good enough.

However, that is also extremely inefficient since you end up making a ton of pointless comparisons, and the algorithm becomes awful with a long list. There are [tons of clever ways to sort a list]( faster like merge sort, bubble sort, quicksort and a hundred others which do different tradeoffs. Some are fast on average, some are fast if the given set is almost ordered already, some take little memory, so on.

If you’re optimizing a game, you’re looking at situations like that where you’re using an inefficient solution, and replacing them with more performant ones. Maybe you have a huge list which takes a ton of memory to process, so you use a sorting algorithm which is “slower” but frees up more memory. Or maybe you look at it a bit harder and realize that you don’t actually need to sort the list in the first place, so you just remove the sorting part and the code runs 30% faster.

There are lots of different ways of optimising programs. Sometimes it’s possible to use a different algorithm that does exactly the same thing but faster. Sometimes it can be faster to store some results in memory or on disk so that they don’t have to be calculated again, sacrificing some memory/disk space for speed. Sometimes you can avoid doing something that is unnecessary or unimportant, for example, in a game, you might decide that there is no point in displaying details on distant objects since they would barely be visible anyway. Sometimes it’s possible to cut out some checks, for example you might have some code that keeps checking whether a value is zero even though it’s actually impossible for the code that changes this value to set it to zero.

These all sound like they should be obvious, but it can be very tricky to find ways of optimising code in practice. Designing fast algorithms can be very subtle, and sometimes it takes many years of research for someone to realise that there is a much faster way of doing something than anyone thought possible. A typical software package is very complicated and is made up of lots of different components, so it can be hard to understand how they all fit together and where unnecessary work is being done. And the operation of CPUs is pretty subtle – sometimes something that seems like it should be faster ends up being slower because it means the CPU has to move some stuff out of its cache, for example.

I recommend watching these videos: optimizing code in general, of course those kinds of optimization can be applied in games.
and the guy goes in detail of everything he did

but it’s very dependent on the game and the way it was implemented in the first place. what’s important generally is to optimize parts of the code that are used the most. there are university courses about game optimization.