As your project gets bigger, it takes longer to compile. Then compilation optimization is essential.

View compile time

The compilation log in Xcode can view the duration, or the total duration, the compilation time for each file.

Swift type check time

Add the following Settings to xcode => Build Settings => Other Swift Flags to see how long the swift expression and function type check takes. And give a warning.

-Xfrontend -warn-long-function-bodies=100 (100 means 100ms here, you should experiment with this value depending on your computer speed and project)

-Xfrontend -warn-long-expression-type-checking=100
Copy the code

The next 100 is in milliseconds, above which a warning is given.

Viewing the Total Time

Execute on terminal:

defaults write com.apple.dt.Xcode ShowBuildOperationDuration -bool YES
Copy the code

Restart Xcode to see the total time it takes to compile

Optimization of project Settings

Use New Xcode to Build System

dSYM

You can set dSYM not to be generated at DEBUG, but only at Release.

modular

Third party libraries such as POD can be packaged as a.a file and then put into the project, so that the compilation time of pod third party libraries is reduced. But that makes debugging inconvenient, so it’s a trade-off.

The optimization of the Object – C

Optimization of OC, focusing on reducing invalid references, has significantly improved the optimization of compile time. Log to see which files take a long time to compile and optimize.

⌘ shortcut + 8

Optimize PCH files

1. Check the PCH file and delete references that are not used much.

H file

Write as few references as possible to.h files. Try to write references in.m files. You can see in the compile log which files take a long time to compile, most of which can be solved by optimizing references.

Delete unnecessary references

After a long project, some classes may no longer be used due to changes in functionality, but references will still exist. This should be deleted.

After optimization, the compilation time was reduced by 2-3 minutes.

Swift optimization

Optimal selection

As mentioned earlier, merging swift files into one can greatly reduce compilation times. But xcode10 no longer uses this option. Xcode10 has given you the choice of optimization, we can mainly choose the right option, according to their own needs, generally debug choose incremental compilation, Adhoc and Release can choose module compilation

The principle can be referred to: medium.com/rocket-fuel…

Code optimization

After the optimization of swift Settings, it is mainly code optimization. Swift is time-consuming mainly type checking, so the focus of code optimization is to check which expressions, function type checking takes too long.

You can use the Swift BuildTimeAnalyzer tool to see the compilation time for each file.

On the Internet to check a variety of statements, the following is the focus of swift code optimization, and indeed useful.

  • Closures and lazy

Is not recommended

private(set) lazy var chartViewColors: [UIColor] = [
    self.chartColor,
    UIColor(red: 86/255, green: 84/255, blue: 124/255, alpha: 1),
    UIColor(red: 80/255, green: 88/255, blue: 92/255, alpha: 1),
    UIColor(red: 126/255, green: 191/255, blue: 189/255, alpha: 1),
    UIColor(red: 161/255, green: 77/255, blue: 63/255, alpha: 1),
    UIColor(red: 235/255, green: 185/255, blue: 120/255, alpha: 1),
    UIColor(red: 100/255, green: 126/255, blue: 159/255, alpha: 1),
    UIColor(red: 160/255, green: 209/255, blue: 109/255, alpha: 1),
    self.backgroundGradientView.upperColor
]
Copy the code

Recommendation:

Cumulative build time: 56.3ms private(Cumulative build time: 56.3ms private(set) lazy var chartViewColors: [UIColor] = self.createChartViewColors() // Build time: 6.2 MS private func createChartViewColors() -> [UIColor] {return [
        chartColor,
        UIColor(red: 86/255, green: 84/255, blue: 124/255, alpha: 1),
        UIColor(red: 80/255, green: 88/255, blue: 92/255, alpha: 1),
        UIColor(red: 126/255, green: 191/255, blue: 189/255, alpha: 1),
        UIColor(red: 161/255, green: 77/255, blue: 63/255, alpha: 1),
        UIColor(red: 235/255, green: 185/255, blue: 120/255, alpha: 1),
        UIColor(red: 100/255, green: 126/255, blue: 159/255, alpha: 1),
        UIColor(red: 160/255, green: 209/255, blue: 109/255, alpha: 1),
        backgroundGradientView.upperColor
    ]
}
Copy the code
  • Array.Dictionary

The collection type needs to specify the type, and do not use + to merge two collections

  • Ternary operator? :
// Before
return someValue > 3 ? someValue - 2 : someValue + 2

// After
if someValue > 3 {
    return someValue - 2
} else {
    return someValue + 2
}
Copy the code
  • Binary operator??
returnCGSize(width: size.width + (rightView? .bounds.width ?? 0) + (leftView? .bounds.width ?? 0) + 22, height: bounds.height) // After var padding: CGFloat = 22if let rightView = rightView {
    padding += rightView.bounds.width
}

if let leftView = leftView {
    padding += leftView.bounds.width
}
return CGSize(width: size.width + padding, height: bounds.height)
Copy the code

? : and?? Both can be resolved by conditional binding.

  • Don’t do too much with a one-line expression

Do not make judgments and calculations in an expression

if number == 60 * 60 {
    // ...
}

// After
let number: Double = 60 * 60

if number == 3600 {
    // ...
}
Copy the code

Code optimization can be referred to here

reference

  • Github.com/fastred/Opt…
  • Medium.com/rocket-fuel…
  • swift.org/blog/osize/