Ever since Swift 3 and Xcode 8 my project compiles quite slowly. Every time I add so much as an empty line to a file, recompiling takes a full minute. When I check the outpu
One common practice that slows down compile time is using Array.append
and String.append
(or their +
operator equivalents). For String
s, it's better to use a formatted string, so instead of
let hello = "Hello, "
let world = "World!"
let combinedString = hello + world
you should use
let combinedString = "\(hello)\(world)"
I can't remember the exact speedup, but it was on the order of 10 times for those particular lines. Odds are, thought, that this won't have a noticeable speedup for any but the tinest projects. For example, our project has hundreds of Swift files, as well as many Objective-C ones, and our compile times are often 10 minutes or more, sometimes even when the only change was to a non-Swift file.
This worked for me on one of my projects.
Go to Product -> Scheme -> Edit Scheme. Select Build in left side column and uncheck "Find implicit dependencies" But this flag should remain checked when you are building the project for first time..
Source
It was a simple project and it increased one of my builds from 1 minute to 2 seconds.
On a physical Device I got these results. For one of my larger projects (42 files) in only decreased it from 2:36 to 2:20. Then I added: SWIFT_WHOLE_MODULE_OPTIMIZATION = YES to Build Settings as a user defined setting. The time went down to - 2:00
On the simulator the build was 49 seconds the first time then I used.
Go to Product -> Scheme -> Edit Scheme. Select Build in left side column and uncheck "Find implicit dependencies" But this flag should remain checked when you are building the project for first time..
and the build took 7 seconds.
I hope this helps.
This is an issue with Xcode 8 where it does not perform incremental builds correctly. If you edit a single swift file it should compile only that file only. This has already been raised here: Xcode 8 does full project rebuild
The 4 files at a time build sounds like Xcode is performing a full rebuild of the project which should not happen again if you only modified a single line in one file.
Make sure you're not combining arrays like let combinedArrays = array1 + array2
. There's a known bug as well for type inference here, where Swift wastes time trying to figure out what type the combinedArrays
should be. Instead, [array1, array2].joined()
should work just as well, and compile far faster.
A issue with this problem is that we don't know where is the wrong initialization/declaration . A solution that my colleague suggest is to find which function take long time to compile so:
Project
select your targetBuild Settings
-> Swift Compiler - Custom Flags
Other Swift Flags
-Xfrontend -warn-long-function-bodies=50
(50 represent the time in milliseconds)after that a warning should displayed as follow:
Getter 'frameDescription' took 108ms to type-check (limit: 50ms)
and after that you know what to do ;)
I've had the same issue only since upgrading to Swift 3/XCode 8 and it appears to be caused by large array literals, similar to this.
I was able to fix the issue by adding type annotations to the variables being assigned to the array literal, e.g.
let array: Array<String> = ["1", "2", "3", "4", "5", "6", "7", "8"]
instead of
let array = ["1", "2", "3", "4", "5", "6", "7", "8"]