I'm currently looking into optimizing my project's compile time.
Although I've known there's something called whole module optimization (WMO for short), but am afraid of checking it out in Build Settings since I didn't really dig deep into it yet.
As I understand it:
WMO should result in a faster code execution but slightly increase the compile time, because it compiles whole module files as one whole instead of compiling each file separately in parallel, according to this Swift official blog on whole module optimizations .
So it's recommended to set Swift optimization level as follows:
None [-Onone]
Fast, Whole Module Optimization [-O -whole-module-optimization]
as it is not that important to have best compile time for occasional release builds.However, when digging for tips about how to reduce compile time for Debug configuration, I found this User Defined settings:
SWIFT_WHOLE_MODULE_OPTIMIZATION = YES
for Debug
SWIFT_WHOLE_MODULE_OPTIMIZATION = NO
for Release
This settings reduced my Debug compile time almost by half.
Since I'm new to Swift compiler and User Defined settings, I tried to find official documentation on SWIFT_WHOLE_MODULE_OPTIMIZATION
but what's confusing is there's not any documentation there online.
People just say it reduces compile time but no further explanation, or they conflicts with Swift Optimization Level mentioned above.
As I understand it, this settings set to YES
should increase the compile time as it enables WMO. Therefore I think I took WMO wrong.
Questions:
What is the difference between Swift Optimization Level
settings and SWIFT_WHOLE_MODULE_OPTIMIZATION
?
Why does SWIFT_WHOLE_MODULE_OPTIMIZATION
reduces compile time?
Thank you!
Whole module optimization is a compiler pass that can add significant performance gains, and so it's always worth enabling when doing a release build of your app for the App Store.
Whole-module optimization can be enabled with the -whole-module-optimization (or -wmo ) compiler flag, and in Xcode 8 it is turned on by default for new projects. Also the Swift Package Manager compiles with whole-module optimizations in release builds.
whole-module-optimization
refers to how the compiler optimises modules and the Swift Optimization Level
refers to each file compilation. You can read more about the different flags for Swift Optimization Level
here.SWIFT_WHOLE_MODULE_OPTIMIZATION
improves compilation time because the compiler has a more global view on all functions, methods and relations between files, allowing it to ignore unused functions, optimise compile order, among other improvements. It also focuses on compiling only modified files, which means that even with that flag activated, if you clean your project and delete the derived data folder, you will still have a bigger compilation time during the first run.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With