Let’s face it: there’s a limit to how much fun you can have while you’re waiting for code to compile.
At some point, enough is enough.
Our team has had enough fun compiling Swift code over the past year that we’ve incorporated the following into our project so we can spend less time compiling and more time working.
Here’s what you can do to make your Swift code compile faster:
1. Building only for the active architecture on Debug builds
as discussed in Cut your Swift build times in half with this one weird trick.
This actually cut our build times in half since all our code was being compiled twice. This works for Debug builds since they’re only for the device or simulator you’re using; you can’t do it for Release builds since they’re for distribution and need to work on all supported architectures. Since Debug builds are a huge percentage of the builds we do, so this saves us a ton of time we’d otherwise be compiling.
2. Turn on Whole Module Optimization and specify a flag to turn optimization off
as suggested in Speeding Up Compile Times of Swift Projects.
According to Developear, “this will allow Whole Module Optimization to compile the whole module at once while also not doing any optimization of the code.” This took our build times down from about 70s to about 40s on my 4 GHz machine, making our builds nearly 50% faster; others on our team saw similar results.
3. Find out which functions are the slowest to compile
using command-line options or the Build Time Analyzer for Xcode app.
This may help you track down pieces of code that the compiler is struggling with; you can try refactoring them to make compilation faster. In our project, we found just a few functions that seemed to be taking too long to compile, but refactoring them only gave us marginal improvements to overall build times (making them 1-2% faster) since none of those functions were taking horribly long to compile in the first place. Your situation may be different; use the analyzer to see if the compiler is choking on any particular piece of code in your app and if so, try the suggestions here or here.