Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dramatically long Swift compilation time

I like Apple and the things they do, but Swift... OH GOD!

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 for "new best and amazing swift, the dream" this configuration is not enough and it works like Google Chrome - InfiniteLoop(find free memory - eat)


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:

  • SR-2461
  • SR-1465
  • SR-1707
  • SR-663
  • and many others, the list is too long, and some of them were opened a few years ago

My question is:

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

like image 459
hbk Avatar asked Jan 11 '17 07:01

hbk


Video Answer


2 Answers

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.

like image 94
Forge Avatar answered Oct 20 '22 08:10

Forge


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

like image 3
Sebastian Boldt Avatar answered Oct 20 '22 07:10

Sebastian Boldt