1

After migrating to Swift 3.0 our first time build time now takes 7 minutes when it used to take 1:20. Does anyone know how can I speed this up?

I work on a large project with lots of Objective C files, approximately 640 Swift files, 20 Cocoapod dependencies, a Today View extension and UITests. After the first time build, an incremental build only takes about 10 seconds.

I have run the excellent Build Time Analyzer here: https://github.com/RobertGummesson/BuildTimeAnalyzer-for-Xcode and attached a screenshot below.

enter image description here

It seems that several of the functions in about 20 of the classes are compiled 631 times. This seems like the problem but I don't know how to fix it. No other function or class takes very long to compile on its own.

Almost all of the 7 minute build time is spent on compiling swift files up until this output: enter image description here After this line all of the rest of the swift files are compiled and run in about 10 seconds.

It's worth noting that the generated project-swift.h file is 4500 lines long and the bridging-header has 150 lines of imports.

Robert Wagstaff
  • 2,664
  • 1
  • 27
  • 40
  • Have you tried deleting derived data? – dylanthelion Feb 13 '17 at 04:00
  • @dylanthelion Yes. – Robert Wagstaff Feb 13 '17 at 04:12
  • I had a similar issue. I was fortunate in that the file responsible for this was no longer used in the project. So, I could afford to delete it – KrishnaCA Feb 13 '17 at 05:10
  • I would just like to point out that the column to the left of the Build Time Analyzer is cumulative build time. Rather than 631 * 65.6 ms, the math is already done for you and is 65.6 ms in total. – Robert Gummesson Feb 13 '17 at 09:54
  • @Robert I don't believe that is true. It should work that way but it doesn't. If it was cumulative time then it would mean that file is being compiled in .103 milliseconds (65.6 / 631). I wish the Swift compiler was that fast! – Robert Wagstaff Feb 20 '17 at 05:09
  • @RobertWagstaff - No, the cumulative time is 65.6ms. Not .103ms and not 38.74s. It's not the file that compiles in .1 ms but the individual routine. If you look in the log files, you'll find that some routines compiles even quicker than 0.1 ms. – Robert Gummesson Feb 20 '17 at 15:31

2 Answers2

1

This may not be a complete solution, but here is at least what is happening. So, you might have 631 files pointing to ViewControllerRecycler.swift.

lets say that ClassA calls something from ViewControllerRecycler.swift, ClassC calls something from ViewControllerRecycler.swift and so on.

If you follow this and add the debugging flag -driver-show incremental to "other swift flags", you will see something like:

Queuing ViewControllerRecycler.swift
Queuing ClassA.swift
Queuing ViewControllerRecycler.swift because of dependencies discovered later

and then

Queuing ViewControllerRecycler.swift because of dependencies discovered later
Queuing ViewControllerRecycler.swift because of dependencies discovered later
Queuing ViewControllerRecycler.swift because of dependencies discovered later

something like that. When ClassA is changed, for example, even if it is nothing about ViewControllerRecycler.swift, it will be queued for compilation again "due to dependencies". This causes your files to be compiled so many times.

You may want to look at the following sites to try to optimise your code to be more faster:

Maybe these will help you to compile a bit, if not extremely, faster

Community
  • 1
  • 1
Papershine
  • 4,995
  • 2
  • 24
  • 48
0

Got the fix! Turned out to be just a settings change as detailed in this SO post here: https://stackoverflow.com/a/40370475/1455770

enter image description here

This change got my compile down from 7 minutes to a far more manageable 2 minutes. Build Time Analyzer shows each file compiling only once now. I'm not 100% sure why this fixed the problem but I noticed that while generating the module-swift.h file the compiler now seems to generate it all in one go instead of file by file. This turns out to be a lot faster.

Community
  • 1
  • 1
Robert Wagstaff
  • 2,664
  • 1
  • 27
  • 40