Swift optimizes the compile time for the Swift project


Some time ago, colleagues in the author’s group developed an application very quickly (let’s say the application was named QiShareDemo). When the author ran the project with Mac Air of 8+128, he found that the project took a long time to compile. After checking relevant materials and doing some practice, he wrote this article. After clone QiShareDemo, the author found that the compilation time of the full compilation project was 105.207s. After the author’s partial optimized compilation time processing, the time of the full compilation project was reduced to 44.573s. Of course, you can continue to optimize this by sorting the compilation time of specific code in your project and dealing with code that takes a long time to compile.

A brief introduction of nouns

Below the author mentioned in this article to make a brief introduction of the noun.

1. Full compilation

Taking the Xcode compilation process as an example, the author understands that a case of full compilation is the process of compiling the project again after deleting the Derived Data generated when The Project is compiled by Xcode.

2. Incremental compilation

Taking the Xcode compilation process as an example, the author understands that a case of incremental compilation is as follows: if Xcode has compiled the project and we modify some files, the modified files and the files that reference related files will be compiled during compilation.

3. Swift Compiler

If the project is not a mixed objective-C and Swift project but a pure Swift project, Swift Compiler is used in the compilation process, as described in section 2.1.2 of part 3 below. For more details on compilation, see The Compilation process

Ii. Compilation process of Swift project

Using Xcode, you can view the project compilation process as follows:

1. Use Xcode to view the specific compilation process of the project

Command + b to compile the project. In Xcode, see the following figure.

The author made the following schematic diagram of Swift project compilation process according to Xcode compilation process.

2. Check the compilation time of the project

Our goal is to optimize the compile time of the project, so first we should know the current compile time. To view the compilation time of the project, run the following command on the terminal: Defaults write com. Apple. Dt. Xcode ShowBuildOperationDuration YES after switch at the top of the Xcode Scheme in the box and run the equipment that can see concrete compilation time. Check the specific compilation time as shown in the figure below:

This is a way of looking at the overall compile time of a project, so if we want to optimize the compile time of a project, we need to find the parts that take longer to compile. As mentioned above, I have included the following schematic diagram of how to see the specific compilation time of a project through Xcode.

3. Consideration of reducing project compilation time

1. Adjust the configuration in Build Settings of Xcode

As for the configuration adjustment in the Build Settings of Xcode, the author found after querying the information that Xcode10 and before had a large adjustment space. In Xcode11, after the author made a preliminary attempt, It turns out that tweaking the configuration in Xcode’s Build Settings has little impact on optimizing the Build time of your project.

2. Package the stable three parties into a Framework

After analyzing the compilation time of the project, the author found that each tripartite introduced in QiShareDemo took 20 + seconds respectively. I don’t know whether it was by checking online article examples or something. It occurred to me that if the source code of these tripartite was not frequently changed during the use of these tripartite, You can package these three parties into a Framework to try to solve the problem of time-consuming compilation. After trying to make the three parties in the project into a Framework for Use in QiShareDemo, a significant reduction in compile time was achieved. After the author clone QiShareDemo from the beginning, the compilation time of the full compilation project is 105.207s. Later, after the author preliminarily packaged the three parties into a Framework, the time of full compilation of the project was reduced to 44.573s;

2.1 Brief discussion on the reasons for shortening compilation time
2.1.1 Compilation will be affected if stable three parties are classified into Framework

This can be discussed in conjunction with the previous article on the compilation process. As for the reason that the project directly uses Framework, the compilation time will be reduced, the group students Mulingluo and Qiwu 647 have mentioned, the author speculated that the reason is: In terms of the compilation process, using the Framework directly saves precompilation, lexical analysis, syntax analysis, intermediate code generation, and object file generation processes compared to using Cocoapods source code dependencies. So the compile time is reduced. This part also has a disadvantage, the group of students into small stack mentioned that it is not convenient to debug and modify the source code after the Framework. This is inevitable, so it is better to package those stable three parties into a Framework, or work with other students in the group to take charge of one of the three parties.

2.1.2 Impact of objective-C and Swift co-programming time

In combination with the compilation process diagram sent by the author, Mu Lingluo, one of the students in the group, mentioned that the mixed programming of Swift and OC projects might take more time than the pure Swift project. This part of the author’s speculation is to see the Swift project compilation process. For a pure Swift project, the Swift compiler is sufficient for the front-end of the compilation. If the project is a mixture of Swift and OC, the front-end process of compilation will also use Clang, Clang will be C, Objective-C API mapping to the Swift API. I think this process is somewhat more time-consuming than the Swift compiler simply compiling Swift code. The author puts a Swift Compiler architecture diagram below, which is drawn and produced by the author in the way of flow chart.

Note: in the previous article on the compilation process, the author introduced GCC, LLVM Compiler, Swift language Compiler is used by its own Swift Compiler.

3. Use a tool to view the compilation time of the code in the project

You can use the tool buildTimeAnalyzer-for-xcode to see the compilation times of your own code in your project. The author used buildTime Analyser-for-xcode to look at the build time of the project and found two places where the build time was time-consuming. The following figure shows the compilation time of the project by the author using BuildTime Analyser-for-xcode. Other Swift Flags in Target -> Build Settings -> Swift Compiler

-Xfrontend -warn-long-function-bodies=100
-Xfrontend -warn-long-expression-type-checking=100
-Xfrontend -debug-time-function-bodies
Copy the code

The above configuration is used to add a configuration that uses BuildTimeAnalyzer-for-xcode to display warnings for methods or expressions that take more than 100ms to compile.

Here are two examples of code I encountered that took time to compile.

3.1?? (nil-coalescing merge operator) and the time taken for “+” to be spliced together

This “??” When used with the + concatenation string, the compilation process is time-consuming. It is best to change to a short code statement.

After the author uses the if let method to deal with the above time-consuming code, the compilation time-consuming problem has been solved.

3.2 Compilation time that may be encountered when using Snapkit

The author opened the code that measured the compile time that might be encountered in the process of using Snapkit. The schematic diagram for detecting compilation time is as follows:

The compilation time problem can be solved by changing the code pointed by the red arrow to the code pointed by the blue arrow. The author’s takeaway: when using Snapkit layout, the reference value should be as clear as possible, and the reference value should not be set as far as possible. Then let the compiler calculate the value for us. We can tell the compiler as much as we know.

Four, other considerations

1. SwiftUI

SwiftUI allows you to see what code looks like in real time and preview it on different devices. SwiftUI can be used to increase development efficiency. Learn to Make Apps with SwiftUI

2. Swift’s HotReload attempt

Considering that Flutter supports HotReload on the market, which can greatly improve the development efficiency. In fact, Swift also supports HotReload. At present, THE author has only tried HotReload of injectionIII Demo, so I will not introduce it too much at present. Swift’s HotReload attempt can use the Injection III tool. InjectionIII is an introduction to the use of InjectionIII. InjectionIII Injection III App Store download address: apps.apple.com/cn/app/inje…

5. Refer to the learning website

  • Swift Compiler
  • IOS wechat compilation speed optimization sharing
  • Incremental Compilation
  • Optimizing Build Times in Swift 4
  • How to improve compilation speed for iOS projects by 5 times
  • Improved compilation speed by a lot! Coupang App Project efficiency Improvement practice
  • How to speed up iOS project packaging by more than 10 times

To learn more about iOS and related new technologies, please follow our official account:

You can add the following xiaobian wechat, and note to join the QiShare technical exchange group, xiaobian will invite you to join the QiShare technical Exchange Group.

QiShare(Simple book) QiShare(digging gold) QiShare(Zhihu) QiShare(GitHub) QiShare(CocoaChina) QiShare(StackOverflow) QiShare(wechat public account)

Recommended articles: Swift 5.1 (14) – Initializing and uninitializing Swift 5.1 (15) – Optional Link Discussion on compilation Process In-depth Understanding HTTPS Discussion on GPU and “App Rendering Process” iOS View and export the project run logs on the Flutter Platform Channel usage and source code analysis and development are not cut map how to do? Vector icon (iconFont) Getting started with guide DarkMode, WKWebView, Apple login must be adapted? ATaller Strange dance weekly