How to reduce Xcode build time?

The Blue Prototype
Mac O’Clock
Published in
5 min readMay 23, 2020

As our codebase grows, build times can increase and you spend a lot of time in building the project. In this article, I’ll explain to you some approaches to reduce the Xcode build time. I hope that these can be helpful to you.

Get Started

1. Display build time in Xcode

To display build time, you should enable the compile timer. Open the command line and hit the following command:

defaults write com.apple.dt.Xcode ShowBuildOperationDuration -bool YES

To see the time after hitting the command, clean project(cmd + option + k) or delete project derived data.

see build time in centre

2. Find code that compiles slowly

Open the project’s build settings and add the following flags to your Other Swift Flags. As in the below screenshot:

other swift flag

Functions who take long compile time:

-Xfrontend -warn-long-function-bodies=<millisecond>

Expressions who take long compile time:

-Xfrontend -warn-long-expression-type-checking=<millisecond>

When you build the project, you can see the warning times in the Xcode build log.

warnings by other swift flag

3. Compiler diagnostic options

The Swift compiler has a variety of built-in diagnostic options you can use to profile the build.

  • -driver-time-compilation : High-level timing of the jobs that the driver executes.
  • -Xfrontend -debug-time-compilation : Timers for each phase of frontend job execution.
  • -Xfrontend -debug-time-function-bodies : Time spent type checking every function in the program.
  • -Xfrontend -debug-time-expression-type-checking : Time spent type checking every expression in the program.

For example, if you suspect that something is taking too long to compile in project, you should turn on the debug-time-function-bodies option for the compiler.

In your project in Xcode, go to Build Settings and set Other Swift Flags to -Xfrontend -debug-time-function-bodies.

other swift flag

Now build your project and go to the Build Report navigator where you’ll see the most recent build. Then, right-click on the build log for the target you built and choose Expand All Transcripts to show the detailed build log. Now, you should see a line of build times along the left. Scan these lines for anything that looks suspect!.

recent build
build transcript

4. Precompiled bridging headers flag

bridging headers flag

You can reduce debug build time 30% by using the new Swift flag. Open the build settings for your project and set Other Swift Flags to -enable-bridging-pch.

5. Use the new build system

new build system

Apple launched a new build system in Xcode 9. However, with Xcode 10, the new build setting has been activated by default. You can enable from Xcode Files-> Project/Workspace Settings if not enabled.

6. Parallelize Swift Builds

Parallelize Swift Builds

Xcode projects composed of multiple targets, one target may be dependent on another target to build. When one target is depending on another target to build then it creates dependency. Building the targets serially will take time and may not be good for utilizing system resources. With Xcode 10, we can parallelize the build and analyze the dependencies under the hood which increase the build time significantly. We can enable parallel build by editing the Xcode Scheme and checking the parallel builds in the build action of the scheme. Checked both Parallelize Build and Find Implicit.

7. Analyse Build Time

Analyse Build Time

With Xcode 10, we can analyze the build process with a timing summary which helps us to determine where our build is taking the time and take corrective action to reduce the build time. This new feature will show the timings of each task performed by the build system and can be found in the Xcode Product → Perform Action → Build with Timing Summary option.

8. Compilation Mode Vs Optimization Level

Optimization Level
Compilation Mode

There are build settings Compilation Mode and Optimisation Level to control compilation mechanism over the debug builds. In case of the debug builds, the whole optimization is not always required so that we can set Compilation Mode option as incremental for the debug builds. This will speed up the build. The Optimization Level build setting can be set to No optimization for the debug builds and for the release builds it can be set to Optimize for Speed.

9. Build Active Architecture Only

Build Active Architecture Only

Make sure Build Active Architecture Only is set to Yes for Debug builds. This is the default when creating a new Xcode project.

10. Debug Information Format

Make sure Build OptionsDebug Information Format is set to DWARF for Debug builds. This is the Xcode default setting.

Debug Information Format

NOTE: If you’re using Fabric/Crashlytics, you may need to keep this set to DWARF with dSYM File.

11. Minimising run script phases

Do the minimum use of run scripts. You should reduce run script phases with clearly defined inputs and outputs.

12. Pre-built Frameworks

To increase build time, prevent re-compiling third party sources by using pre-built dynamic frameworks/libraries. When you use precompiled frameworks/libraries, these frameworks/libraries don’t compile at the build time and that’s why they increase Xcode build time even when you build your project after a fresh clean step. Using prebuilt frameworks in place of projects improves Xcode responsiveness and indexing times as well.

13. Minimum use of cocoapods dependencies

Whenever we compile an app, the dependencies code is also compiled. So, the project needs a lot of time to build every single one of those frameworks. For this problem, here is a solution, you can use carthage or pre-compiled frameworks.

If you have any suggestions or queries, feel free to comment.

Thank You :)

--

--