4

In an attempt to optimize the build time of my app I've added the following to my OTHER_SWIFT_FLAGS:

OTHER_SWIFT_FLAGS = 
-Xfrontend -warn-long-expression-type-checking=75
-Xfrontend -warn-long-function-bodies=75

I got a warnings for this specific type checking being slow, and I cannot figure out if I can help the compiler in some way here.

var delay: TimeInterval = TimeInterval(index) * 0.05

Any suggestions what can be done to speed up the compile time for such basic arithmetic operations up?

I'm running Xcode 11.5 with Swift 5

Xcode screenshot

Also tried explicitly casting the number to TimeInterval, which shouldn't be needed as all numbers are Doubles by default. enter image description here

Groot
  • 13,943
  • 6
  • 61
  • 72
  • 1
    Just a word of caution: unless compile times are a huge concern for you, don't chase this dragon too far. You'll end up using less and less type inference, adding tons of type information to your code that only clutters it, only for some ms of compilation gain. Considering almost all builds are incremental and only involve a few files, it's usually not too big of a deal – Alexander Jun 29 '20 at 15:30
  • Great word of caution - I won't. This is more out of curiosity. – Groot Jun 29 '20 at 16:00

2 Answers2

2

The compiler performs type checks. If you have a long expression, it takes time. Sometimes when the expression is too long, you even get an error as:

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

try something like

var delay: TimeInterval = TimeInterval(index) * TimeInterval(0.05)
Tomo Norbert
  • 770
  • 4
  • 13
0

In your build settings, under 'Swift Compiler - General' check the 'Reflection Metadata Level' and try setting it to 'None'.

Since upgrading to Xcode 11.4, I had a project jump from compiling in around 20 seconds to compiling in around 6 - 7 minutes.

I noticed that it only happened in Debug builds, which is strange because Release builds perform more optimizations and should really take longer to compile. I eventually traced the slow builds back to the 'Reflection Metadata Level' setting. It is set to 'All' for 'Debug' builds by default.

This might be a bug in the Swift compiler that's included in Xcode 11.4 and later, because this setting never caused a problem for me in earlier releases. (It started happening with Swift compiler version 5.2.)

xgerrit
  • 31
  • 2