Press enter or click to view image in full size

As an iOS developer, we have encountered this problem frequently whereby, after starting the build, it takes a long time to get compiled and built which in turn tends to distract us from our focus zone and reduces productivity.

So we drilled down to identify and tackle the core cause.
After doing a bunch of optimizations, we reduced our build time by ~21%.
So let’s get started.
of the project by executing the below-mentioned command in the terminal:
defaults write com.apple.dt.Xcode ShowBuildOperationDuration YESpost that after closing and reopening the Xcode again and building the project we’ll be able to see the total build time.
Press enter or click to view image in full size

Note:
Before measuring the build time, it’s recommended to clean the project(i.e
⌘+⌥+K) and delete project’s derived data as well. Below is the command to do the same:
rm -rf ~/Library/Developer/Xcode/DerivedData
by adding below mentioned flags in "Other Swift Flags”section under Build settings:
-Xfrontend -warn-long-function-bodies=100
-Xfrontend -warn-long-expression-type-checking=100Press enter or click to view image in full size
warn-long-function-boides=100: Help’s to show the functions which took more than 100ms of time to compilewarn-long-expression-type-checking=100: Help’s to show the specific expression which took more than 100ms of time to compile.Also to debug it further, we have added the below-mentioned flag under "Other Swift Flags”section as well:
-Xfrontend -debug-time-function-bodiesthis will in turn show the list of compilation times of each function.

This will help us provide a summary of time spent on each task, allowing us to pinpoint the exact task we need to focus in order to optimize our build time.
Press enter or click to view image in full size

Press enter or click to view image in full size

Note: Above shared summary is from an incremental build and we can observe that without doing any changes CodeSign took a good amount of time which somehow could have been avoided. Later in the article, we have mentioned ways to optimize that.
Clean build: To clean and rebuild whole project from scratch.
Incremental build: To rebuild a project after some code changes.
It is a log parser tool for Xcode-generated xcactivitylog and gives a lot of insights in regards to build/compilation times, warning, slowest targets etc. for every module and file in our project.
To generate the report, run the below-mentioned command in the terminal:
xclogparser parse --workspace <project-name>.xcworkspace --reporter htmlPress enter or click to view image in full size

and with the help of this report, we jotted top functions that were taking up a lot of compilation time.
Join Medium for free to get updates from this writer.
Remember me for faster sign in
Now that we have analyzed the reason behind the slow build time, let’s deep dive into ways to optimize it.
.init because the compiler needs to spend more time determining the type of init, so it’s preferable to declare the type instead.if let condition check provided it won’t look complex in terms of user readability.Though most of the below-mentioned build settings are by default set by Xcode but for older projects, some of these may not yet be set or may be overridden by other values so it’s always good to take a look and verify the same.
Debug: Yes
Release: No
Debug: Incremental
Release: Whole Module
Debug: No Optimization [-Onone]
Release: Optimize for Speed [-O]
Debug: DWARF
Release: DWARF with DSYM File
Code signing was consuming much time even if there were no code changes, so we debugged and resolved it for debug builds
--digest-algorithm=sha1 to “Other Code Signing Flags” in the Xcode build settingsResource rules are rules given in a plist, telling codesign what to sign and what not to
Press enter or click to view image in full size

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>rules</key>
<dict>
<key>.*</key>
<false/>
</dict>
</dict>
</plist>Before:
Press enter or click to view image in full size
After:
Press enter or click to view image in full size