Whole Module Optimization

Updated 29 April 2022

Save

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.

Why do we need Whole module optimization?

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.

How does it work?

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.

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.

Using Whole Module Optimization

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.

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.

Conclusion

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

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home