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.
View all comments
Show parent comment
2
u/Ameisen vemips, avr, rendering, systems Jul 08 '26 edited Jul 08 '26
The main issue is I'm issuing this in instruction dispatches in the interpreter, so the small bit that could use the instruction would need to be in its own function, and from what I've seen, Clang refuses to inline functions marked with
target.I could potentially work around this by adding the CPUID bits to the dispatch lookup bits, and allowing it to generate new functions this way, but I cannot conditionally add attributes at compile-time which complicates it (these dispatch functions have declarations and prototypes generated by a mixture of
#defines,templates, and lots ofconstexpr).[Ed: but even then, the lookup/dispatch logic is set up so that it can also inline these functions, and using
targetwould inhibit that at that level instead...]This is basically what it looks like. I am very much wanting to use reflection to vastly simplify this.
Specifically, there are cases like the MIPS
EXTinstruction which I could conditionally map to the BMIBEXTRinstruction, but if aCALLis required then it isn't worth it. (This instruction can be slower on certain chips so it isn't the best example, but yeah).