supertux icon indicating copy to clipboard operation
supertux copied to clipboard

Support pre-compiled headers and interprocedural/link-time optimisation

Open Semphriss opened this issue 1 year ago • 6 comments
trafficstars

This adds CMake code to enable pre-compiled headers (PCH) and interprocedural optimisation (IPO, or LTO for link-time optimisation).

PCH

PCH makes builds 50% to 66% faster. A simple time make -j$(nproc) on my 12-core CPU shows:

With PCH:
real    0m58.048s
user    5m33.425s
sys    1m56.666s

Without PCH:
real    1m50.533s
user    15m20.049s
sys    2m56.815s

real is the total time spent, from start to finish, as perceived by the developer. user is the sum of the time spent by each thread, which gives a better idea of what would happen on a single-threaded system. Details

IPO

IPO makes Release builds 15% smaller. I have not tested the runtime performance.


Notes

  • As instructed, I've opened a single PR for both of these.
  • For PCH to work properly, I had to add some missing include guards and include headers in some files.
  • cmake_policy(SET CMP0069 NEW) requires CMake >=3.9, but I did not update cmake_minimum_required to discuss whether or not to keep the policy.

Semphriss avatar Jun 16 '24 21:06 Semphriss

I had to change a few more things in the code for the builds to pass. I changed it to the best of my abilities, but some involve design choices, so it might be good to review them. The diff will show all the changes, but I bumped the minimum CMake version from 3.1 to 3.9 to support policy CMP0069 (needs 3.6) and list(FILTER ...) (requires 3.9). Let me know if you'd like to change something.

Semphriss avatar Jun 18 '24 03:06 Semphriss

I tested this a couple days ago. Seems to be working fine.

tobbi avatar Jun 19 '24 23:06 tobbi

This sounds like enabling pre-compiled headers has only advantages and no disadvantage. If a developer edits a single header file and recompiles the program, is the recompilation faster or slower with pre-compiled headers enabled?

HybridDog avatar Jun 27 '24 18:06 HybridDog

@HybridDog It's something I've been thinking about, whether or not to enable them by default. They're not really relevant during development, only when making releases, which is IMO the preferred place for lesser-known non-default options to live.

I haven't tried actively developing with those options enabled, but I would presume, especially with LTO, that it substantially slows down development compiling. I'm not sure how large the impact is, and active developers would be better placed to report their experience with that.

Semphriss avatar Jun 27 '24 20:06 Semphriss

If they are only relevant for release builds, maybe they can be enabled if and only if the build type is Release or RelWithDebInfo (-DRELEASE).

HybridDog avatar Jul 22 '24 18:07 HybridDog

PCH and IPO are now only enabled on Release|RelWithDebInfo builds.

Semphriss avatar Jul 28 '24 20:07 Semphriss

We will cherry-pick this into #3093

MatusGuy avatar Apr 02 '25 17:04 MatusGuy