There are many articles about Swift compilation acceleration on the Internet, so I will not repeat them here. Optimizing all aspects of compilation is recommended here
There is also the optimized artifact BuildTimeAnalyzer- for-xcode
The following is about the compiler optimization at the specific code level.
How do I find code in a project that takes time to compile?
In XCode 10 Swift supported monitoring compile timeout warnings. It can help us find the functions in the project that need to be compiled and optimized, and quantify the specific optimization time.
Add this in Build Settings Swift Compiler – Custom Flags escalate Other Swift Flags.
-xfrontend -warn-long-function-bodies=<limit> -xfrontend -warn-long-expression-type-checking=<limit>Copy the code
Test model: A12z Apple DTK(so compile time will look much shorter)
Let’s Start
Tip1: Concatenating optional strings with + is extremely time consuming.
Suggestion: Rewrite to “\(string1)\(string2)”
Time-consuming code: 372ms compilation time
let finalResult = (dbWordModel? .vocabularyModel? .justSentenceExplain ?? "") + "<br/>" + (dbWordModel? .vocabularyModel? .justSentence ?? "")Copy the code
After optimization: compilation time 20ms 18.5x compilation performance improvement 🤯.
let finalResult = "\(dbWordModel? .vocabularyModel? .justSentenceExplain ?? "")<br/>\(dbWordModel? .vocabularyModel? .justSentence ?? "")"Copy the code
Tip2: this value is optional??
Assigning default values and nesting other operations can be extremely time consuming.
Optimization: Unpack using guard let.
After the above example is optimized: compilation time 63ms 5.9 times better compilation performance.
guard let dbSentenceExp = dbWordModel? .vocabularyModel? .justSentenceExplain, let dbSentence = dbWordModel? .vocabularyModel? .justSentence else { return } let finalResult = "\(dbSentenceExp)<br/>\(dbSentence)"Copy the code
Tip3: Split the long calculation code into final combined calculation.
Time-consuming code: 736ms compilation time
let totalTime = (timeArray.first? .float()? .int ?? 0) * 60 + (timeArray.last?.float()?.int ?? 0)Copy the code
Optimized after splitting: 22ms 33.4 times compilation performance improved 🤯
let firstPart: Int = (timeArray.first? .float()? .int ?? 0) let lastPart: Int = (timeArray.last?.float()?.int ?? 0) let totalTime = firstPart * 60 + lastPartCopy the code
Tip4: and or not and >=,<=,== Logical operations nested Optional can be time-consuming.
Suggestion: Optimize with Guard let + split.
Time-consuming code: 10420ms compiler failed to compile and reported an error.
if (homeMainVC? .scrollview.contentOffset.y ?? 0) >= ((homeMainVC? .headHeight ?? 0) - (homeMainVC? .ignoreTopSpeace ?? 0)) {}Copy the code
Optimized: 21ms 496 times improved compilation performance 🤯
let leftValue: CGFloat = homeMainVC? .scrollview.contentOffset.y ?? 0 let rightValue: CGFloat = (homeMainVC? .headHeight ?? 0.0) - (homeMainVC? .ignoreTopSpeace ?? 0.0) if leftValue == rightValue {}Copy the code
Tip5: Manually adding type inference reduces compile time.
On the basis of Tip4, we optimize twice:
Before optimization: 21ms
let leftValue = homeMainVC? .scrollview.contentOffset.y ?? 0 let rightValue = (homeMainVC? .headHeight ?? 0.0) - (homeMainVC? .ignoreTopSpeace ?? 0.0)Copy the code
After optimization: added 16ms type inference
let leftValue: CGFloat = homeMainVC? .scrollview.contentOffset.y ?? 0 let rightValue: CGFloat = (homeMainVC? .headHeight ?? 0.0) - (homeMainVC? .ignoreTopSpeace ?? 0.0)Copy the code
Conclusion:
After more than 100 compilation optimizations in the project, the compilation speed of MacBook Pro 15-inch 16-model I7 was improved from 262s to 228s by 34s (under the condition that clear Xcode was immediately compiled after reboot), and the improvement may be even faster on the company’s poor performance packers.
Comprehension:
Sometimes there is a trade-off between compile performance and code readability.
Pursuing compilation performance at the expense of code readability is not an option.
It is also not advisable to waste most of your time compiling when your computer is performing poorly.
M1-chip computers say 🐶.
Appendix: Measuring Swift compile times in Xcode 9