Dramatically long Swift compilation time
Asked Answered
N

4

19

I have a problem with Swift compile time.

Configuration:

  • MacBook Pro (Retina, 13-inch, Mid 2014)
  • 2,6 GHz Intel Core i5
  • 8 GB 1600 MHz DDR3
  • SSD 256 (up to 800 MB/s)
  • 1k classes
  • 10 pods

Compile time: 3 min.

Looks like this configuration is not enough.

I tried various methods to speed up the compile time:

  • Set HEADERMAP_USES_VFS to YES (no change)

enter image description here

  • Set Build Settings / Architectures / Build Active Architecture Only to YES (some improvement)

enter image description here

  • Set defaults write com.apple.Xcode PBXNumberOfParallelBuildSubtasks 4 (8) (sometimes it's become even worse)

  • Set optimisation level to -Onone (no major improvement)

enter image description here

  • Set debug information format - DWARF instead of DWARF with dSYM File

enter image description here

  • Disable Find implicit dependencies from your scheme (no major improvement)

enter image description here

  • Created a new project and moved all files to the new one (takes too long and give small time improvement)

  • Change compilator from default to others available (even worse)

  • Strict rules on how to write fast compiled Swift code (below more details)

I've found a few posts where people describe the same problem: here and here

I guess everyone is waiting for new Xcode 8.2 beta 2 where:

Xcode will not rebuild an entire target when only small changes have occurred. (28892475)

But this is only partially true, I still have long compile time - moved from 6-12 minutes to 3-8. Detailed description of this can be found here and here

Also, I followed all these rules during code writing:

  • Chained function reduction
  • Unwrapping the optionals
  • Implicit typing for an object (dictionary especially)
  • Avoiding ?? operator (this one extremely hungry!!)
  • Avoiding ?: operator

In othe words, don't use any "new cool features" because you'll wait for ages.

One more try - is a workaround described here:

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 the first time.

But, this workaround isn't what I'm looking for and this is also can be as a temporary solution.

Swift is a pain not because it's hard to write, but because it's hard to use (at least on some machines).

For comparison: I have a project on Objective-C with 2k classes, and 11 pods - clean compile time is ~30 seconds.

Also I found a lot of opened bugs on Swift performance:

My question is:

Does anyone have any solution on improving compile time for Swift?

Neille answered 11/1, 2017 at 7:43 Comment(4)
i think this is just inherent in more high level languages with lots of features, obj-c is pretty basic compared to swift so the compiler has a lot less work to do to get the code into binary form, i think if you are using obj-c bridging as well it will add a lot of compile time to your swift projectHeliopolis
I think it is a young language with dozen of bugs in the compiler. Anyone who use it should be ready to face with these bugs. I think you can wait new version of the compiler, may be there the bug will be fixed.Mareah
I noticed that changes to the public/open part of the type usually trigger "chain reaction" of recompilation. So it makes sense to keep the public part as stable as possible and carefully set private/fileprivate. Maybe even use some kind of bridge between implementation and interface.Flare
We have same problems with compile time. We break projects to targets, and move files to new targets. It's take lot of time for refactor project in this way, but worth it. Whole project compiles 10 mins, after any change in app target compile take 1 min.Spelunker
Z
7

This issue has been extremely frustrating for me as well. We've tried several of the solutions you've mentioned and none of them worked.

The one thing that made a significant improvement in compilation time is: Turning on the Whole Module Optimization while adding -Onone in Other Swift Flags.
See this - Speed Up Swift Compilation

I'm using Swift 3 with Xcode 8.3.

Zelazny answered 10/4, 2017 at 13:34 Comment(1)
Thanks provided solutions reduced my build time 25 min to 11 minAmigo
S
3

We improved our compile times a lot by using Carthage instead of Cocoapods to include third party libraries.

Sate answered 27/4, 2017 at 14:20 Comment(0)
C
2

It's likely you have code optimisation switched on, whilst this is a must for building your release version, for dev builds it isn't particularly important.

What code optimisation does is exactly what it says, it optimises your code to make the end binary that is produced smaller. It does things such as rename variables/methods to single characters etc... Like I said you obviously want this when submitting to the app store but you might not care if the app is larger whilst developing it.

You can turn off code optimisation for development builds by going to Project Settings > Build Settings, searching 'optim', under Swift Compiler - Code Generation set Optimization Level to None [-Onone] for Debug builds.

This should significantly improve your build times.

Counterplot answered 11/1, 2017 at 7:50 Comment(1)
It's already setted to -Onone - add update in post, but even with this i have to wait few min after every buildNeille
S
1

WHOLE MODULE OPTIMIZATION WITH NO DEBUGGING RESTRICTIONS

Just add SWIFT_WHOLE_MODULE_OPTIMIZATION as an user-defined setting with the value YES and your compile times should improve a lot.

enter image description here

This fix was introduced by the developers at Zalando. Shout out to them. https://jobs.zalando.com/tech/blog/improving-swift-compilation-times-from-12-to-2-minutes/?gh_src=4n3gxh1

Sate answered 24/11, 2017 at 8:18 Comment(2)
in this case in debug mode you can't check values for any variables due to optimization level, isn't it?Neille
Nope, it is not true. We are activating whole module optimization without the o-flag so debugging works as expected.Sate

© 2022 - 2024 — McMap. All rights reserved.