Dealing with slow Swift build times

Mohd Asif
Practo Engineering
Published in
3 min readMar 18, 2017

We started our project back in April 2016, and decided that it’d be a pure swift codebase. As the project grew, more and more swift files got added, we started noticing build times of around 2–3 mins. Few months later, we got to a point that the project would take around 7 minutes to build.

Googling around we found some interesting links.

Using the awesome Xcode analyser plugin for Xcode 7.3.1, we tried to get rid of the top culprits in our project and merged a few pull requests which reduced the build time by 3–6 seconds. But these PRs didn’t really have any affect on the overall build time. It still took 7 minutes to build.

Relevant XKCD comic

We were hoping that when Apple released swift 3, there’d be some build time improvements as well. While there were some improvements in the compiler, we didn’t really notice any when building our project.

To make matters worse when Xcode 8.0 was released, it used to do a full recompile instead of doing an incremental build, so changing a simple constant in any file would trigger a rebuild of the whole project. (This was fixed later in Xcode 8.2)

Developers on the team were getting frustrated because of these rebuilds.

Googling around again, we came up on this gem on Stack Overflow.

Workaround for faster builds

Turns out that adding this simple flag to our project settings, reduces the compile time from 7 minutes to 1 minute.

Just go to Build settings for the target in Project settings, and click on the + button to the left of the search bar. Select Add User-Defined Setting
and paste this text into the text field

SWIFT_WHOLE_MODULE_OPTIMIZATION

and set the corresponding value to YES for debug builds and NO for release builds.

Adding SWIFT_WHOLE_MODULE_OPTIMIZATION

When Whole Module Optimization is turned on Xcode compiles all the swift files as one big file, instead of compiling each file individually.

Generally you would have noticed that Whole Module Optimization is said to be enabled for release builds, this is because the compiler can infer and apply optimizations to your codebase if it can see all files at once.

So make sure to set this to No for the release config, and then upload to appstore so that you get the compile time optimizations that the swift compiler can provide.

As a side note you can refer to this answer to see the time it takes to build your project within xcode.

Conclusion

It’s easy to forget that swift is only 2 years old, and lots of contributors are still working hard towards building this language.
While build times have been a very big cause of frustration, the Whole Module Optimisation trick saves us a lot of grief, and allows us to continue writing more and more swift code.

Follow Practo Engineering on twitter for regular updates. If you like this article, please send us some applauses. This will help other Medium users find it. We are also looking for kickass developers. If you are interested, then visit here.

--

--