Compile Time Incredibly Slow
My project consists of ~350 Swift files and ~40 cocoa pod dependencies.
As soon as the entire project was migrated to
Swift 3, build times have been INCREDIBLY slow and took a little over 3 minutes to completely compile.
I’ve noticed that if I rebuild after not changing any files, it builds within a reasonable amount of time. However, if I add a new function, it takes the 3+ minutes.
Cocoapods does not seem to be causing the problem as it delays on
Compiling Swift source files state.
I followed this to investigate:
-Xfrontend -debug-time-function-bodiesflag to my
Other Swift Flagsin my target’s build settings
Build the project
Copied this into terminal and ran
pbpaste | egrep '\.[0-9]ms' | sort -t "." -k 1 -n | tail -100
However, I didn’t see anything of concern. The file that took the longest to compile was only 250ms. The next closest was 100ms, even if all 350 files took 250ms to compile, that would only be a total of 73 seconds which is way off from the 3+ minute builds I am seeing.
What could be causing these long compile times?
It was never as slow before updating to
Xcode 8 and
- How to Convert my Swift Code back to 2.3
- Is there any difference at all between suffix(from:) and dropFirst(_:)?
- How to create managedObjectContext using Swift 3 in Xcode 8?
- Swift Block value error
- Disable blinking cursor from UITextField in swift?
- Apple Mach-O Linker (ld) Error Group with Swift 3 & Xcode 9 GM
7 Solutions Collect From Internet About “Compile Time Incredibly Slow”
I created a new project without running the
Swift 3 conversion, imported my
Swift 3 files, but the build time remains the same.
SWIFT_WHOLE_MODULE_OPTIMIZATION = YES, but as soon as you make changes to more than 1 file, the incremental build fails and it triggers a re-build which lasts for more than 4-5 minutes.
I’ve now re-written my entire code base from
Swift 3 to
Swift 2.3. It didn’t make any difference, the problem lies with the
Xcode 8 compiler.
I can confirm that unchecking these two
will alleviate the pain for a while, the
Xcode 8 bug does seem to be tied to how it checks dependencies between files.
I’ve converted my code base to
Swift 3 from
Swift 2.3 since
Xcode 8.2 beta requires it, the beta should include a fix for “Xcode will not rebuild an entire target when only small changes have occurred. (28892475)”. Sad to say, they haven’t fixed the bug and my compile times are exactly the same with
Xcode 8.2 Beta.
I don’t have enough reputation to comment, but I still wanted to share some resources. I’ve been stuck in this misery for days, upgrading to
Swift 3 has been a complete disaster.
I’m using this to track slow files, even though just like you, that’s not my problem. Something else in xcode is taking literally 4 minutes to complete:
I’ve also made sure I don’t have any
lazy vars or
closures that swift doesn’t like. Don’t use the
+ operator when concatenating strings, etc.
I’ll update this answer if I find anything, it’s just about impossible to be productive with
Swift 3 ATM.
I’m using Xcode 8.1 My issue was with Dictionary which uses
this is my code when it takes 10 minutes to build:
let params: [String:String] = [ "email": email ?? self.email, "clave": password, "tipo_documento": documentType?.rawValue ?? self.typeDocument.rawValue, "documento": number ?? self.documentNumber, "nombre": name ?? self.name, "apellidos": lastName ?? self.lastName, "fecha_nacimiento": birth?.parse() ?? self.birthDate.parse(), "genero": genre?.rawValue ?? self.genre.rawValue, "telefono_movil": cel ?? self.cel, "direccion": address ?? self.address ]
I don’t know why but it advertise me that the Dictionary take a long time to compile.
Then I change it to:
var params: [String:String] = [:] params["email"] = email ?? self.email params["clave"] = password params["tipo_documento"] = documentType?.rawValue ?? self.typeDocument.rawValue params["documento"] = number ?? self.documentNumber params["nombre"] = name ?? self.name params["apellidos"] = lastName ?? self.lastName params["fecha_nacimiento"] = birth?.parse() ?? self.birthDate.parse() params["genero"] = genre?.rawValue ?? self.genre.rawValue params["telefono_movil"] = cel ?? self.cel params["direccion"] = address ?? self.address
Hope it could help some of you.
SWIFT_WHOLE_MODULE_OPTIMIZATION = YES
Xcode version: 8.1 GM
To add choose your target, then go to
Editor > Add Build Setting > Add User-Defined Setting, and add the above.
My clean build time dropped from 35 mins (Ahem, excuse me) to 8 mins with a project file count of 800.
Note: Tried this on Xcode 8.0 first, but didn’t work.
string concatenation seems to be incredible slow in Swift3/XCode8:
item.text = item.text + " " + pickerText + " " + (attribute?.Prefix ?? "") + inputText + (attribute?.Suffix ?? "")
~ took 8-10 seconds to compile
item.text = "\(item.text) \(pickerText) \(attribute?.Prefix ?? "")\(inputText)\(attribute?.Suffix ?? "")"
~ took 1,6 seconds to compile
item.text = [item.text, " ", pickerText, " ", (attribute?.Prefix ?? ""), inputText, (attribute?.Suffix ?? "")].joined();
~ took 0,001 second to compile
I found a couple of coding styles that take a lot of time compiling in Swift (2.3, not tested on 3):
a += b
a = a + b
Also adding array together:
var a = [1,3,4] var b = [5,6,7,8] var c = [8,4,3,5] var d = a + b + c
var a = [1,3,4] var b = [5,6,7,8] var c = [8,4,3,5] var d : [Int] =  d.appendContentsOf(a) d.appendContentsOf(b) d.appendContentsOf(c)
The last optimization took the compilation time for 1 method from 9800ms to 5.5ms…
I migrated a Swift 2x project of 17 files to Swift 3 and had 28 warnings and 55 errors across all files. Compile time was 4-5 minutes.
Find Implicit Dependancies
in scheme and
SWIFT_WHOLE_MODULE_OPTIMIZATION = YES
made only minor improvements.
As I eventually cleared the warnings and errors in each file, the compile time reduced and is now in the seconds. The IDE is back behaving as it should – detecting errors in near real time and compiling quickly.
Firstly, it looks like the compiler is recompiling (or at least cross checking) every file with any error or warning – even if you haven’t edited that file since the last compile.
Secondly, if there are too many dependant errors/warnings across files, the compiler bottlenecks and slows right down.
Whenever you face slow compilation issue, closly looks the the third party SDKs you included in your app and also try to find your coding techniques.
I face this issue twice in my app and I felt like harrassed by Swift 3 😡. Both the times reasons were different.
First time I found that I added a library in my app named AASignatureView. Since this lib was added, my compilation time increased like hell. It started taking near 12-15 Mins in running the app on simulator. The issue was resolved after I removed this lib from my code and added VMSignatureView and my compilation time went into normal state.
Second time I faced this issue after making a code of appending several Strings. Both of these below methods turned app’s compilation time to hell
a = a + b
a += b
Then I changed my code to below way and the problem was solved.
a = "a\(strSometihng),\(strAnother)"
- How to populate table rows, using a array sent from iPhone by Watch Connectivity?
- Xcode 6.1: file was built for x86_64 which is not the architecture being linked (i386)
- Access BPM field on a song (MPMediaItemPropertyBeatsPerMinute not working)
- NOt able to convert from MPMediaItem(mp3 song) to NSData
- NSOperationQueue serial FIFO queue
- How to use Objective-C classes with names which are keywords in Swift
- UITableViewCell subclass wrong image in cell or old image bug
- Xcode 4.3-4.4 crashes with breakpoints using LLDB, breakpoints useless with GDB
- Check if optional protocol method is implemented in Swift?
- Archive submission failed due to the issue listed below. ERROR ITMS-90039
- How can I subclass a UITableView?
- UIActivityViewController in iOS 7
- Google SignIn Cocoapod deprecated
- Set local notification for everyday at midnight
- How to add facebook open source code pop to project directly