I notice there are two settings of optimization in the project settings :
- Single-File Optimization
- Whole Module Optimization
What's the difference? And which one should we choose under what circumstances?
I notice there are two settings of optimization in the project settings :
What's the difference? And which one should we choose under what circumstances?
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
Cons
This optimization mode will run one frontend instance for your whole module. It runs optimizations on all the files at once.
Pros
Cons
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.
Optimization Level(SWIFT_OPTIMIZATION_LEVEL) -O vs -O -whole-module-optimization
swiftc command
definitions
jobs
) for compiling, assembling and linking
Frontend Job
(swift -frontend
) - is responsible for compilationld job
- static linking and other jobscompiling
-enable-batch-mode
) - one frontend job for one CPU-wmo
) - one frontend job
for one module. An advantage is good optimizations results which are reflected on runtime. A disadvantage is no incremental build and worse parrallelismOptimization
-O
Single-File compilation - compiler operated by single file(e.g. .swift). It means that compiled doesn't know any other information out of this file. That is why compilation can be paralleled and incremented(don't compile file and it's dependencies were not changed)
-O -whole-module-optimization
- compiler operated by whole module. It is next level of optimizations. From Xcode v8 it is by default for new projects and SPM for release. On this level compiler has much more information about calls. Compilation consists of two big parts(SIL optimizer(1/3 time) and LLVM(2/3 tile)). SIL optimizer is a bottle neck and can not be paralleled and incremented but LLVM can
Optimizations examples:
Also you can get next warning during debugging with optimizations
Project was compiled with optimization — stepping may behave oddly; variables may not be available.
It is because of additional parameter fast
which is passed. As a workaround you are able to use -Onone
and add user-defined settings
as SWIFT_WHOLE_MODULE_OPTIMIZATION=YES
Compilation Mode(SWIFT_COMPILATION_MODE)
Incremental
- by default for debug - compiles out of date filesWhole Module
- by default for releaseParalelize
Edit Scheme -> Build -> Parallel Build