How to debug slow Swift compilation time

I have a Swift SpriteKit project with about 25 small files. The time for compiling this project is 30-45 seconds! This is pure Swift without ObjC.

I looked at the compilation in Report Navigator to try and find a single file that takes time. But this is not the only file. This is always the last file on the list, which seems to take all the time. But this file may be different between compilers and still takes all the time.

The step immediately after the last file is Merge xxx.swiftmodule, which happens quickly, but I'm not sure that since it appears right after slowness, it can be connected.

I searched and tried different approaches to finding the culprit. I read this post: Why is Swift compiling so slowly? and tried these approaches. I did a command line build using CTRL- \, but this does not show any useful slowness information.

I looked through my project, looking for places where type inference might have worked, but didn't find much (and indeed, if that were the case, I would have expected one file as the culprit).

Does anyone have any other suggestions on tracking this? Based on the Objective-C project, which compiles almost instantly, it drives me crazy.

EDIT I ​​worked on this a bit more, and I include a screenshot of the assembly, noting where the slowness occurs. The fact is that if I comment on the code in a slow file, the file in front of it in the list becomes slow (where it was good before). If I comment on this code, then the one that before becomes slow, etc.

Build output

+5
source share
3 answers

After more deepening and debugging, I found a problem. It turns out that this is actually a type inference, as other posts suggested. One of the problems is that I did not notice that the output of the assembly in the Report Navigator shows an arrow for files that are still compiling, and a tick for those that are done (my color blindness played a role). So I thought that they all finished compiling when it wasn’t.

Anyway, I read the article here: GM released the Xcode 6 compilation and assembled the entire project line, which showed me the file that was the problem. Then, using the CTRL- \ approach described above, I found the culprit.

It turned out that this line was the problem:

ourWindowCopy.position = CGPoint(x: ((26.5 + theXOffset) * self.multiplierWidth) + (4.0 * CGFloat(randomRow) * 2.0 * self.multiplierWidth), y: ((41.0 + theYOffset) * self.multiplierHeight) + (5.75 * CGFloat(randomColumn) * 2.0 * self.multiplierHeight)) 

I know this is a mess - I played with a bunch of different options at an early stage and have not yet returned to simplification. I replaced it with this (and, of course, we will replace literals, and also just further):

 let floatRandomRow = CGFloat(randomRow) let floatRandomCol = CGFloat(randomColumn) let pointX: CGFloat = ((26.5 + theXOffset) * self.multiplierWidth) + (4.0 * floatRandomRow * 2.0 * self.multiplierWidth) let pointY: CGFloat = ((41.0 + theYOffset) * self.multiplierHeight) + (5.75 * floatRandomCol * 2.0 * self.multiplierHeight) ourWindowCopy.position = CGPoint(x: pointX, y: pointY) 

Now the compilation speed is very fast!

I’m not sure that there is any new information here, but I would like to close this decision if someone comes across it.

+5
source

There is a hidden option in the Swift compiler that displays the exact time intervals that the compiler uses to compile each individual function: -Xfrontend -debug-time-function-bodies .

Simple launch in the terminal and analysis of the results:

 xcodebuild -workspace App.xcworkspace -scheme App clean build OTHER_SWIFT_FLAGS="-Xfrontend -debug-time-function-bodies" | grep [1-9].[0-9]ms | sort -nr > culprits.txt 

Awesome Brian Irace wrote a brilliant article about this Profiling your Swift compilation speeds .

+4
source

Based on other answers, the following commands are useful ...

To view a sorted list of functions by build time:

 xcodebuild -workspace [Your Workspace Name].xcworkspace -scheme [Your Build Scheme Name] clean build OTHER_SWIFT_FLAGS="-Xfrontend -debug-time-function-bodies" | grep .[0-9]ms | grep -v ^0.[0-9]ms | sort -nr > culprits.txt 

To view a sorted list of functions that take 1s to build:

 xcodebuild -workspace [Your Workspace Name].xcworkspace -scheme [Your Build Scheme Name] clean build OTHER_SWIFT_FLAGS="-Xfrontend -debug-time-function-bodies" | grep [1-9].[0-9]ms | sort -nr > culprits.txt 

You can get a list of schema names like this (this command starts a clean one first):

 xcodebuild -workspace [Your Workspace Name].xcworkspace -list 

Useful articles on why your build times can be long:

+1
source

All Articles