Updated 29 April 2022
Here is again an interesting article that attracts the developer’s attention to how the compiler uses Whole module optimization for the Swift code.
This optimization is related to the compiler that we use for Swift development.
So without any delay let’s start diving into the article.
Here is one common answer to why we need it.
Optimizing the modules inside the project for better performance is the main reason behind the optimization.
For instance, when we create the final version of the build by combining all the files together this technique evaluates the whole build.
Using compiler flag -wmo we can enable the whole-module optimization.
The swift compiler compiles each file in the module separately, which is automatically done by the compiler behind the scenes.
The compiler optimizes the code and then generates the machine code and then creates an object file. Finally, the linker connects all the object files and creates an executable file.
Here is a simple example.
1 2 3 4 5 6 7 8 9 10 |
HelperClass.swift: //generic structure struct Example<T> { var supposedVariable: T func getValue() -> T { return supposedVariable } } |
1 2 3 4 5 6 |
SomeViewController.swift: func callValue (attr: Example<Float>) -> Float { return attr.getValue() } |
When the compiler is optimizing the SomeViewController.swift it just knows the existence of the callValue function and is not aware of its implementation.
When the compiler is optimizing the HelperClass.swift it does not know the particular type of the caller.
The compiler only generates the generic version which is much slower when the type is specified.
By using the whole module optimization technique the compiler optimizes all files of a module as a whole.
The compiler checks the implementation of all functions in the module, then the compiler specializes the generic functions to a specified type and creates a new version of the function.
So the compiler changes the function inside our HelperClass.swift file for the specialized type.
1 2 3 4 5 6 7 8 9 10 |
HelperClass.swift: //specialized structure after WMO struct Example { var supposedVariable: Float func getValue() -> Float{ return supposedVariable } } |
Whole module_optimization eliminates the unused non-public functions from the code. This makes the build much lighter and increases the performance.
After completing the whole module_optimization the compiler splits the modules into multiple parts, and with the help of LLVM, multiple threads are created for each part.
The compiler does not process those threads which have not changed during the development.
This saves the recompilation time for the preprocessed threads.
We hope you would like the overview of the whole module optimization.
For more detailed information please check swift documentation here
To read my other blogs please click here
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.