Page 1 of 1

Using Profile-Guided Optimization (PGO) to optimize Factorio's performance

Posted: Thu Aug 15, 2024 2:47 pm
by zamazan4ik
TL;DR
Use Profile-Guided Optimization (PGO) to optimize the CPU performance for Factorio. What PGO is - https://clang.llvm.org/docs/UsersManual ... timization (Clang docs, this optimization is also available for other C++ compilers like MSVC and GCC).
What ?
I suggest optimizing the game with Profile-Guided Optimization. This optimization gathers some runtime statistics about how the application is executed. Then, you can use these statistics (aka a PGO profile) during the release compilation to provide a compiler with additional information about the usual execution paths, etc. Based on this information, the compiler can perform various optimizations like inlining, loop roll/unroll, etc. in a much better way. PGO profiles can be gathered from various "sample" Factorio saves that you decide are "regular" for the game.

PGO works nicely with Link-Time Optimization (LTO) so no traps should be here. It's even recommended to use PGO with LTO (however, I hope LTO is already enabled for the Factorio Release builds anyway).

Much more details about PGO (various articles, a lot of benchmarks for different domains including game engines, etc.) can be found in my GitHub repository about PGO - https://github.com/zamazan4ik/awesome-pgo.

If you have already used PGO or tried to integrate it into the game earlier, it would be awesome if you could share your benchmark results. I would be happy to link to the repository above.
Why ?
I believe improving CPU performance is important for games such as Factorio since it allows the simulation of bigger worlds on weaker machines. Another point - improved power efficiency (an important point for platforms like laptops and Steam Deck)

Re: Using Profile-Guided Optimization (PGO) to optimize Factorio's performance

Posted: Sun Aug 18, 2024 2:20 pm
by DeHackEd
I suggested this to a dev recently as well.

There are a ton of ways this could go wrong though. In a game with mods, every possible mod concept (from an API standpoint) needs to be tested during Profile-generate mode. If not, it's possible PGO will think some specific feature is never ever used and optimize for that, and then along comes a mod making use of the feature and its performance tanks. One that comes to mind is Stop Chasing Belt Items which was designed for a performance boost but could get nerfed by PGO if not done exactly right.

So while I like the idea of PGO, I fear the portion of the build process requiring running the game in profile mode could take a REALLY long time...