Why is Swift compile time so slow? Why is Swift compile time so slow? swift swift

Why is Swift compile time so slow?


Well, it turned out that Rob Napier was right. It was one single file (actually one method) that was causing the compiler to go berzek.

Now don't get me wrong. Swift does recompile all your files each time, but the great thing now, is that Apple added real-time compilation feedback over the files it compiles, so Xcode 6 GM now shows which Swift files are being compiled and the status of compilation in real time as you can see in this screenshot:

Enter image description here

So this comes very handy to know which of your files is taking so long. In my case it was this piece of code:

var dic = super.json().mutableCopy() as NSMutableDictionarydic.addEntriesFromDictionary([        "url" : self.url?.absoluteString ?? "",        "title" : self.title ?? ""        ])return dic.copy() as NSDictionary

because the property title was of type var title:String? and not NSString. The compiler was going crazy when adding it to the NSMutableDictionary.

Changing it to:

var dic = super.json().mutableCopy() as NSMutableDictionarydic.addEntriesFromDictionary([        "url" : self.url?.absoluteString ?? "",        "title" : NSString(string: self.title ?? "")        ])return dic.copy() as NSDictionary

made the compilation go from 10/15 seconds (maybe even more) down to a single second... amazing.


We've tried quite a few things to combat this as we have around 100k lines of Swift code and 300k lines of ObjC code.

Our first step was to optimize all functions according to the function compile times output (eg as described here https://thatthinginswift.com/debug-long-compile-times-swift/)

Next we wrote a script to merge all the swift files into one file, this breaks access levels but it brought our compile time from 5-6min to ~1minute.

This is now defunct because we asked Apple about this and they advised we should do the following:

  1. Turn on 'whole module optimization' in the 'Swift Compiler - Code Generation' build setting. Select 'Fast, Whole Module Optimization'

enter image description here

  1. In 'Swift Compiler - Custom Flags', for your development builds, add '-Onone'

enter image description hereenter image description here

When these flags are set, the compiler will compile all Swift files in one step. We found with our merge script this is much faster than compiling files individually. However, without the '-Onone' override, it will also optimize the whole module, which is slower. When we set the '-Onone' flag in the other Swift flags, it stops the optimization, but it doesn't stop compiling all Swift files in one step.

For more info on whole module optimization, check out Apple's blog post here - https://swift.org/blog/whole-module-optimizations/

We've found these settings allow our Swift code to compile in 30 seconds :-) I've no evidence of how it would work on other projects, but I suggest giving it a try if Swift compile times are still an issue for you.

Note for your App store builds, you should leave the '-Onone' flag out, as the optimization is recommended for production builds.


It likely has little to do with the size of your project. It's probably some specific piece of code, possibly even just one line. You can test this out by trying to compile one file at a time rather than the whole project. Or try watching the build logs to see which file is taking so long.

As an example of the kinds of code that can cause trouble, this 38-line gist takes more than a minute to compile in beta7. All of it is caused by this one block:

let pipeResult =seq |> filter~~ { $0 % 2 == 0 }  |> sorted~~ { $1 < $0 }  |> map~~ { $0.description }  |> joinedWithCommas

Simplify that by just a line or two and it compiles almost instantly. The trouble is something about this is causing exponential growth (possibly factorial growth) in the compiler. Obviously that's not ideal, and if you can isolate such situations, you should open radars to help get those issues cleaned up.