preface

This article will explore how to analyze and optimize the compilation speed of Swift code from the code level. Here take my personal project (white mouse) dream ledger to optimize it.

For more on iOS indie development, check out Lawliet’s Indie Thoughts

  • Wechat: RyukieW
  • Public id: LabLawliet
  • 📦 Archive of technical articles
  • 🐙 making
My personal project Minesweeper Elic Endless Ladder Dream of books
type The game financial
AppStore Elic Umemi

Analyze compile bottlenecks in your code with timeout warnings

1.1 Project Configuration

Code exceeding the set point is identified in the form of a warning. The following uses 100ms as an example

Add the following configuration under BuildSettings other Swift Flags:

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

Just add it under DEBUG. After all, it’s just for testing

1.2 Compilation Check

If Clean is compiled once, you can see a number of warnings:

You might think this is a little inconvenient, given that projects can have hundreds or thousands of alerts. Here’s a tool: BuildTimeAnalyzer- for-xcode

BuildTimeAnalyzer- for-xcode

Clone the project code first (or download the code directly)

git clone https://github.com/RyukieSama/BuildTimeAnalyzer-for-Xcode.git
Copy the code

Here I have detailed instructions for using the steps in Chinese, and compiled the finished App, if you feel good, welcome to leave a small ⭐️ ~

2.1 Project Configuration

Add the following configuration under BuildSettings other Swift Flags:

  • -Xfrontend -debug-time-function-bodies

Then Clean the project and rebuild.

2.2 open the BuildTimeAnalyzer xcodeproj

There are three ways to use it

A. Run directly

B. the App is deduced

Product - Archive

Export an App file and use it directly later.

C. The lazy way – download and use

I’ve got a package here that I can use directly. Support Intel, Apple Silicon.

Download: buildTimeAnalyzer.zip

2.3 Viewing Compilation Time Logs

You can view the log by clicking on the project you just configured and compiled.

Shows the overall compilation time, but also marked with the number of lines of code and function name, click to jump.

Each time may not be the same, there will be some changes according to the operation of the computer, but the overall bottleneck trend is basically the same

Third, optimize the project

Here is a personal proposal to combine the above two ways to locate.

  • BuildTimeAnalyzerIt’s more intuitive to find bottlenecks
  • The way to add warnings is to locate specific code more accurately

3.1 Project Configuration

To combine the two approaches, here is my configuration:

BuildTimeAnalyzerThe results of the analysis

  • Top:DataBaseDataCenter+OverviewTime consuming35075ms

Case 1: Longer logic

Here the WCDB bottleneck is used to show that successive calls to the generic functions in the WCDB are used to obtain a condition for database operations.

Optimization: Split

Clean and then Build to check whether it is effective:

DataBaseDataCenter+Overview time decreased from 35075ms to 19007ms, reducing the time by nearly half. Then optimize the DataBaseDataCenter+Overview code for the same scenario and see what happens.

After optimizing another similar scene, the time was reduced to 915ms.

Case 2: String concatenation

Here + is used to concatenate strings

The total time is 2596ms.

Optimization: use “xx\(xx)xx” splicing

Use \(preString)~\(endString) for concatenation

It worked, down to 617ms.

Case 3: Use less??

There are more?? . The total time is 3561ms.

Optimization: unpack gurad/ iflet earlier or reduce calls?? The number of

Overall it’s down to 855ms.

Four,

Here’s a list of some of the things I did successfully optimize in the project, and some of the things I didn’t find the right way to optimize. I will update this article if there are more valuable points to end.

This time the individual project as a guinea pig for optimization, enterprise projects can be generally optimized in more places. But we should not optimize for the sake of optimization, without compromising the readability and consistency of the source code.

If you have some experience in code level optimization, you are also welcome to exchange information ~ (contact information at the beginning of the article)

reference

  • BuildTimeAnalyzer Chinese manual
  • Configuration instructions
  • IOS wechat compilation speed optimization sharing
  • Optimizing Swift build times