swift

Swift one-line type-checking lasting 27s

Chasing compiler‘s own tail

In the Swift project I finished recently there was a very strange behavior. At some point I noticed that autocomplete totally stopped working and the indexer was constantly spinning. Only if I would wait for what felt like minutes, things would settled down and it was working fine for a while. Only to re-appear again after I add few more types or methods.

It’s rather large project with about 60k lines of code, but still was really strange. After I got fed up, I began hunting for solution. Luckily for me, rather quickly I found Soroush’s post describing these unofficial Other Swift Flags:

-Xfrontend
-warn-long-function-bodies=100

It‘s probably easier to visualize where these two go, in the target’s Build Settings:

This is where you place them, in target’s Build Settings

This is where you place them, in target’s Build Settings

This warns you when any of your methods take over 100ms to finish type-checking. In my project, I found few nasty surprises:

27s to type-check a single method?!

27s to type-check a single method?!

This last method, *taking up 27s to type-check*, had a total of 5 lines and the problem turned out to be string concatenation. When I replaced it with string interpolation, it dropped below 50ms.

This was really insane and I was curious why would this one line cause so much work for Swift compiler. Matt Gallagher of Cocoa with Love encountered this few months before me, found the issue and even offered a solution.

update: Jordan Rose chimed in on Twitter about the proposed solution: