Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Single-File Optimization vs Whole Module Optimization

Tags:

swift

I notice there are two settings of optimization in the project settings :

  • Single-File Optimization
  • Whole Module Optimization

enter image description here

What's the difference? And which one should we choose under what circumstances?

like image 767
Yuchen Avatar asked Dec 27 '17 20:12

Yuchen


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.

What is whole module?

Whole-module optimization is an optimization mode of the Swift compiler. The performance win of whole-module optimization heavily depends on the project, but it can be up to two or even five times.


1 Answers

Single-file Optimization

This optimization mode has the compiler run one frontend instance per file in your program. It runs optimizations on each file separately, loading as little information as it can from other files in the project.

Pros

  • When doing incremental compilation, the compiler doesn't have to recompile your whole project, and can instead recompile just the files that have changed or rely on files that have changed
  • The compiler runs one instance per file, so on a computer with multiple cores, it can compile faster

Cons

  • Some optimizations will not be performed if the content that's being optimized spans multiple files
  • The compiler does have to get some information out of other files so it may repeat this work more times than necessary (if 6 files reference one other file, that file may have some work performed on it 6 times when only 1 was needed)

Whole-Module Optimization

This optimization mode will run one frontend instance for your whole module. It runs optimizations on all the files at once.

Pros

  • This will perform the maximum optimizations that the swift compiler can perform
  • Performs less redundant work than Single-file Optimization

Cons

  • This will only use one CPU core to run all the swift-specific optimizations on your code. This means that a multi-core computer will not be fully utilized compiling your code
  • In incremental compilation, your whole module will still have to be recompiled every time

What to Use

For debug builds, I highly recommend completely disabling optimizations. This will make stepping through your code in the debugger more predictable and will make build times shorter. If you really need optimizations, you should probably go with single-file for the better incremental compilation times.

For release builds, I recommend using whole-module optimization, as it can perform more optimizations than single-file optimization.

like image 77
TellowKrinkle Avatar answered Oct 25 '22 09:10

TellowKrinkle