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

1.95K views

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

105 Answers

Anonymous 0 Comments

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](https://www.youtube.com/watch?v=kPRA0W1kECg) 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.

Anonymous 0 Comments

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.

Anonymous 0 Comments

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.

Anonymous 0 Comments

Imagine you need to go buy groceries. You make a list of what you need, then you go down every aisle looking for those items until you have traversed the entire store. Easy right? But wait, this isn’t optimal. You know some aisles don’t contain any items you need from your list. So you can optimize your path by skipping the aisles that don’t contain anything on your list.

Optimizing a program is similar. You are looking for places where you are doing more work than you need to and find ways to skip over unnecessary work. One example is object culling. Instead of drawing everything in the game world, only draw the things in front of the player’s camera. You can optimize this even further by only drawing things in front of the players camera that are not completely occluded by another object! There are tons of little tricks like this that can make a program run faster or use less memory.

Anonymous 0 Comments

I recommend watching these videos:
https://youtu.be/c33AZBnRHks optimizing code in general, of course those kinds of optimization can be applied in games.
and
https://youtu.be/t_rzYnXEQlE 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.

Anonymous 0 Comments

I recommend watching these videos:
https://youtu.be/c33AZBnRHks optimizing code in general, of course those kinds of optimization can be applied in games.
and
https://youtu.be/t_rzYnXEQlE 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.

Anonymous 0 Comments

Imagine you need to go buy groceries. You make a list of what you need, then you go down every aisle looking for those items until you have traversed the entire store. Easy right? But wait, this isn’t optimal. You know some aisles don’t contain any items you need from your list. So you can optimize your path by skipping the aisles that don’t contain anything on your list.

Optimizing a program is similar. You are looking for places where you are doing more work than you need to and find ways to skip over unnecessary work. One example is object culling. Instead of drawing everything in the game world, only draw the things in front of the player’s camera. You can optimize this even further by only drawing things in front of the players camera that are not completely occluded by another object! There are tons of little tricks like this that can make a program run faster or use less memory.

Anonymous 0 Comments

One implementation of certain optimizations, including a feature in Optifine, it calls “fast math”. In this case they take expensive math operations, like calculating sine, cosine, or square roots and replace them with faster less accurate functions. These functions are used a lot in graphics processing, so there can be some significant gains by reducing the amount of time those operations take.

Anonymous 0 Comments

Imagine you need to go buy groceries. You make a list of what you need, then you go down every aisle looking for those items until you have traversed the entire store. Easy right? But wait, this isn’t optimal. You know some aisles don’t contain any items you need from your list. So you can optimize your path by skipping the aisles that don’t contain anything on your list.

Optimizing a program is similar. You are looking for places where you are doing more work than you need to and find ways to skip over unnecessary work. One example is object culling. Instead of drawing everything in the game world, only draw the things in front of the player’s camera. You can optimize this even further by only drawing things in front of the players camera that are not completely occluded by another object! There are tons of little tricks like this that can make a program run faster or use less memory.

Anonymous 0 Comments

I think it is also important to mention that there are tools that can analyze code & running programs to tell you where it is spending most of its time. That’s helps identify places where can optimize the code. As an example, I had program that dealt with database records that had a date. And every time to display a record we needed a formatter to make the date look as desired. Using a profiling tool it showed doing that was actually the bulk of processing time used by the app. For each record it created a formatter object which turned out to be a very expensive operation. I fixed it by creating one at the beginning of the app and reusing it for each record. Made the program 90% faster. Optimized!