Accelerate compilation in Xcode to swift

With the growth of the project, the compilation speed of the project slows down. This becomes especially noticeable when you test a program, making small changes in the program in parallel.

First, let's find out how long the project is compiled before our improvements. To do this, you need to enter in the terminal

defaults write ShowBuildOperationDuration -bool YES

To do this, go to the Product section, hold down alt and click the Clean Build Folder, and then build the project again. My project compiles 81 seconds . Let's see what the result will be after improvement.

First of all, we should know which places lead to a slower compilation. By default, Xcode does not show a warning where the problem is, but we can fix it.
The "heaviest" places are big functions and type checking. Therefore, we need to write these two lines in the Build Settings section -> Other Swift Flags -> Debug

-Xfrontend -warn-long-function-bodies=100
-Xfrontend -warn-long-expression-type-checking=100

(here we have compilation time 100ms, we can put any number)

Visual drawing

An example of my working project

I have a site that occupies 13778 ms, most likely you will not have one, but because of the specifics of the project, I have such sites. Since there is built a binary tree for OCR.
Because of the deep nesting in each other and the type definition only at the very beginning, before the equal sign, the compiler needs a lot of time to understand what type it is in front of it. (The tree takes 30 lines, here is part of it)



We defined the type for each nesting and the warning was gone.

Another weak point is several closures in a row.



Explicitly indicated the type.

After correcting all the warnings, check the project build time again.
Now the project is compiled 26 seconds in two and a half times faster.

There is still something to work on, but the most obvious problems have been fixed.

Also popular now: