Improving Swift Compilation Times from 12 to 2 Minutes

by Joao Nunes - 12 Apr 2017

With our Fleek app growing and new features being introduced, its compile times have started to become a real challenge. We recently discovered that to compile the app or just make a minor change it would take approximately 12 minutes.

We wanted to cut this time dramatically to improve the customer experience overall, as well as our work overhead. In this article, I’ll show how we managed to decrease it to just 2 minutes.

In Xcode, we have the option to select three optimization levels: None, Fast and Fast, Whole Module Optimization.

Using Whole Module Optimization makes compilation very fast. But choosing Fast or Fast, Whole Module Optimization won’t allow a developer to debug the application. If we select one of the above, compile the app, and then try to debug the app, we see this in the console:

App was compiled with optimization - stepping may behave oddly; variables may not be available.

So how can we activate the whole module optimization without activating the -O flag? The solution to this problem is easy and was found via this post from Y Combinator. Xcode will not allow us to complete this task via the UI options. Thus, we need to add it as a User-Defined Setting.

To do so, open the target build settings and set your Optimization Level to None in Debug configuration. Next, add a new setting called SWIFT_WHOLE_MODULE_OPTIMIZATION and set it to YES.

And you’re done. With this technique, the app was able to compile from a clean state in about 2:26 mins.

Changing a file that most of the app depends on now takes only 1 minute instead of the full recompilation time.

What does this mean for Fleek?

As an iOS team making changes everyday to our code, this improvement was important for better time management and cost efficiency of our work, on top of overall customer experience. Let’s say we complete 20 compilations a day. This means that we saved approximately 26 hours of compilation time in a day, or about three extra developers.

More information on whole module optimization can be found here. If you are curious about how to measure compile times, I have used this project on GitHub in the past with much success.

Similar blog posts