r/cpp • • 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.

186 Upvotes

75 comments sorted by

View all comments

Show parent comment

1

u/Total-Box-5169 Jul 07 '26

To make it worse, while removing a single branch can result in better performance, most of the time that is true for CPUs above a certain performance threshold, while older or lower end models do not benefit at all. And of course, those are the CPUs that badly need the performance boost.

1

u/Ameisen vemips, avr, rendering, systems Jul 07 '26

In my case, most of it was conditionally-executed code that technically could be determined at entry.

So, instead, I ended up making a fancy chain of templated functions that built up a constexpr state object, in the end generating a lot of execute functions for each state which if constexpred instead. This improved runtime significantly.

Other cases were removing extraneous load/stores to non-local addresses, which can be harder than you'd think when you're writing a rather complex VM.