r/cpp • u/zer0_1rp • Jul 02 '26
Redundancy seen in AAA game engines
https://zero-irp.github.io/Redundancy-seen-in-AAA-game-engines/I don't like people treating the compiler like a magic box that optimizes like Bjarne Stroustrup himself is checking every line of C++ to assembly. Clean C++ code does not always mean clean compiled code.
I've been reversing game engines to study how they constructed their fundamental Transformation matrices and handled temporal jitter logic when I spotted a lot of avoidable overhead and "over-engineering" across multiple engines, honestly I wasn't even looking for inefficiencies, but it stood out a lot... That said expect no performance gain this is simply for fun that I wrote this blog!
I’ll theorize how the original C++ code was written, show the unoptimized reality of what the compiler spat out, and then showcase how it could have been better optimized.
119
u/lukaasm Game/Engine/Tools Developer Jul 02 '26
Everything can be better optimised, but no one cares if it runs fast enough and hits the performance target.
The real game cost lies in gameplay systems. I would love to sit and vectorise implementations, do CPUID dispatch by supported feature set, fight with cache misses and branch miss predictions, but engine teams most of the time are small, more workforce is taken by gameplay programmers/designers and with moving gameplay features to higher level ( C#, VisualScripting ) etc, some frontiers yield more gains than others, some are more obvious than others.
35
u/splicer13 Jul 02 '26
Over-engineering has evolved to be a meaningless word but the original definition was spending needless expense and effort on things that didn't matter, or adding unnecessary points of failure.
That's not what is being illustrated here.
104
u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Jul 02 '26
I don't understand the point of this. I'm confident that if you pick any sufficiently large and mature codebase you'll be able to spot many inefficiencies like these, indepedently of the domain.
People need to solve real problems with often tight time and budget deadlines. They will not be able to carefully optimize every single part of the codebase.
45
u/James20k P2005R0 Jul 02 '26
This is a very maximalist viewpoint that I think is massively overstating and overgeneralising what has actually been found and discussed in this article. If you want to make an argument that the entire culture of multiple codebases sucks and ignores CPU execution realities, you need to provide real evidence. This isn't even close
There's a small handful of examples where better code could be generated. There's been no evidence provided that this actually makes the game run badly, other than a misleadingly vague idea that because 80% of the time is spent in misc functions, the code must therefore be uniformly slow
Running a sampling profiler on a game compiled in release mode with inlining that you don't have the source code or debugging symbols to largely leads to gibberish. The profiling results they've gotten simply don't mean what they think they mean
How much of the frame's CPU cycle budget is made up of the matrix inverse? I'm guessing its close enough to 0%. Is the game even CPU bound? If not, this makes actually 0 difference on the final performance
I often see this mentality in beginners, who are fresh with notions that programming is about making things run quickly, and all code has to be super duper optimised and fast. And then you discover that 95% of the time, it literally doesn't matter
63
u/not_a_novel_account cmake dev Jul 02 '26
This is the darkside of never optimizing prematurely, which is a lot of individually harmless non-optimal choices, as you put it, "[raise] baseline execution cost of every function".
An engineer needs to solve one problem in front of them, they do, the overall code didn't get slower within the noise margin. The next engineer does the same, and over, and over again. Over a year, maybe, you can see the effects accumulate, but there's no one person to blame. It's a tragedy of the commons.
There's no real solution for it on the engineering organization-scale. It's not a clean education problem, like "use move semantics" or "don't inhibit NRVO". There's no clang-tidy for wasteful MatrixMultiply4x4.
CPUID-dispatch has been understood for decades and is as close to transparent as you can get. This kind of stuff is really inexcusable.
34
u/sfider_sky Jul 02 '26
Part 1 Case 1 is not overengineered, it's written as simple as it can be. I would argue that your alternative is the one that's overengineered. Why complicate a piece of code, that has no measurable influence on the performance?
Part 1 Case 2 could be the result of function inlining. Somebody called a function that returned the result of
tanand, without looking into implementation details, usedatanto get the result they needed.Probably non of these would show up in the profiler, meaning the time spent on optimizing went into other parts of the code.
9
u/SemaphoreBingo Jul 02 '26
This is suggesting to me that things can't be all that bad. Whatever hard optimization work there was has been done, and everything else is just going to be chipping away at diminishing returns.
The 4x4 matrix inversion is the only one I find particularly regrettable, but without knowing how much run time it's actually using I cant get too excited about it. I think that if it were a heavy-hitter in the profile the author would have said so in the writeup.
9
u/eteran Jul 02 '26
Certainly you make some good points, but keep in mind that the compilation process can CREATE apparent redundancy in the executable.
Basically if there is a small function that gets inlined into like 20 other functions, when you look at the binary, it's gonna look like there's 20 copies of that small function.
TLDR: redundancy in binary ≠ redundancy in the source necessarily.
8
u/kiwidog Jul 02 '26
Clean C++ doesn't equal clean codegen, Frostbite is a huge example of this. They use templated classes with interfaces, which isn't bad. It's easy to read and implement, but good lord disassemble one of those binaries and there's hundreds to thousands of functions for their classes. 2000 types later with 10ish templated functions per type balloons massively.
It doesn't matter much for execution times, and it doesn't look bad in C++, but the generated code is massive.
15
u/Thesorus Jul 02 '26
(disclaimer, so take this with a grain of salt ) I don't understand 1/100 of what you're saying.
it's probably not worth the effort (time and money) to optimize the code presented compared to other more cpu intensive code that have more impact on game play performances.
6
u/Jaegermeiste Jul 02 '26
Stroustrup? For optimization at the assembly level? I mean, Abrash or GTFO.
In all seriousness, Stroustrup himself has actually been warning about the same overengineering that OP is complaining about.
11
u/R3DKn16h7 Jul 02 '26
I don't get your point. If at some point in the code there is a matrix created with a rotation of half / pi, and this function gets called twice per frame, what is the point of optimizing this function? The person reading the source code will clearly understand the intent of the function call, and performance will not be affected at all. So we have clarity of intent and perfectly fine and valid code, and performance i not affected at all. This is good and practical design.
5
u/PossibilityUsual6262 Jul 02 '26
Core of unreal engine gameplay systems is dynamic dispatch via delegates, so none of it ever optional or inlined.
13
u/rdtsc Jul 02 '26
I often am equally impressed what compilers do optimize as I am shocked what seemingly simple things they fail to optimize. Makes you second-guess many of your simple "zero-overhead" abstractions.
9
u/cannelbrae_ Jul 02 '26
The the code is far from the critical path, I’d strongly prefer:
In the critical path? Then the development, documentation, and maintenance cost of an optimized solution makes sense.
Games today are often massive with huge teams. Readability and maintainability are critical.
10
u/FrogNoPants Jul 02 '26 edited Jul 02 '26
You are focusing on the tip of one leaf and ignoring the forest with this post, at least focus on optimization commonly used functions... you build the projection matrix and TAA jitter once per frame.. you could do it in Lua for all it would matter.
9
u/Sopel97 Jul 02 '26 edited Jul 02 '26
how did they manage MatrixMultiply4x4 to have a loop wtf, I'm already outraged
7
u/Jannik2099 Jul 02 '26
This is just plain incorrect. The steps to clean math code is as simple as
2
u/germandiago Jul 03 '26
Much of what I see in the article is algorithmic and math tricks, nothing to do as much with code as it has to do with knowing what to do.
1
u/RetroZelda Jul 03 '26
as gameplay/multipalyer clean code is always my first step. Once I get a feature or system made, the clean code is more readable for the code review. Then after that is working and design can get their hands on it, then I'll start focusing on optimizations as I address design's feedback, changes, and feature creeps - depending on where we are in production ofc. But i found myself getting lazy when working in unreal and not proprietary engines... so I definitly contributed to unreal slop lol
0
u/kiner_shah Jul 02 '26
What did you study in order to research so much and write about it?