Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between User Defined SWIFT_WHOLE_MODULE_OPTIMIZATION and Swift Optimization Level?

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:

  • For Debug configuration, set to None [-Onone]
  • For Release configuration, set to 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:

  1. 

What is the difference between Swift Optimization Level settings and SWIFT_WHOLE_MODULE_OPTIMIZATION?

  2. Why does SWIFT_WHOLE_MODULE_OPTIMIZATION reduces compile time?

Thank you!

like image 551
CattDamon Avatar asked Oct 12 '17 09:10

CattDamon


People also ask

What is whole module optimization?

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.

How do I enable whole module optimization?

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.


1 Answers

  1. The main difference is that 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.
  2. 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.
like image 187
jvrmed Avatar answered Nov 15 '22 18:11

jvrmed