# Profile and Reduce Xcode CI Build Times Guide

Canonical: https://figment.so/blog/mac-ci-build-time-profiling-xcode

Add -showBuildTimingSummary to find the slow build phase, then add Swift frontend timing flags and sort the output to find the slowest function bodies. Fix those first and re-measure on the same host.

**Run `xcodebuild` with `-showBuildTimingSummary` to see which phase is slow, then add Swift frontend timing flags and sort the output to find the slowest functions.** Fix the top few, then re-measure on the same host and scheme. Teams that tune build settings without profiling usually optimize files that are a tiny fraction of the total.

The flags below follow Apple's guidance in [Improving the speed of incremental builds](https://developer.apple.com/documentation/xcode/improving-the-speed-of-incremental-builds). Run them on a full clean build, because incremental builds skip unchanged files and distort per-file numbers.

## How do I turn on build timing in xcodebuild?

Use two layers. Layer 1 is the build phase summary:

```bash
xcodebuild build \
  -scheme "$SCHEME" \
  -destination "platform=iOS Simulator,name=iPhone 16" \
  -showBuildTimingSummary \
  2>&1 | tee build-timing.log
```

Capture with `tee` so you can parse the log offline. The summary prints at the end and groups wall-clock seconds by phase: `CompileSwiftSources`, `CompileC`, `PhaseScriptExecution`, `CodeSign`, `Ld`. That shows which phase is the bottleneck before you look at files.

Layer 2 is Swift frontend per-file timers, passed through `OTHER_SWIFT_FLAGS`:

```bash
xcodebuild build \
  -scheme "$SCHEME" \
  -destination "platform=iOS Simulator,name=iPhone 16" \
  -showBuildTimingSummary \
  OTHER_SWIFT_FLAGS="-Xfrontend -debug-time-function-bodies -Xfrontend -debug-time-compilation" \
  2>&1 | tee build-timing-detailed.log
```

| Flag | What it measures |
|---|---|
| `-Xfrontend -debug-time-function-bodies` | Type-check time per function body |
| `-Xfrontend -debug-time-compilation` | Frontend phase time per source file |
| `-Xfrontend -warn-long-function-bodies=100` | Warning when a function body takes over 100 ms to type-check |

Replace `"$SCHEME"` with a scheme your project defines (from `xcodebuild -list`).

## How do I read the timing summary?

Find the phase with the largest share of seconds. A typical summary looks like this:

```
Build Timing Summary
CompileSwiftSources   (2 tasks)      47.3 seconds
CompileC              (12 tasks)      3.1 seconds
PhaseScriptExecution  (3 tasks)       8.4 seconds
CodeSign              (1 task)        0.9 seconds
Ld                    (2 tasks)       1.2 seconds
```

Swift compilation is roughly 78% of total build time here (47.3 of 60.9 s), so that is where to focus. If `PhaseScriptExecution` is a large share, audit your run script build phases. Scripts without declared input and output file lists invalidate incremental state and rerun every build.

## How do I list the slowest functions?

Sort the `-debug-time-function-bodies` lines in the detailed log. Each line starts with a duration such as `412.30ms`, followed by `file:line:column` and the function name. The script below keeps those lines, sorts them by duration, and prints the top N. It refuses to report on a failed build, because a failed build has misleading timings.

```bash
#!/usr/bin/env bash
# ci/parse-build-times.sh
# Usage: bash ci/parse-build-times.sh build-timing-detailed.log [N]
set -euo pipefail

LOG_FILE="${1:?Usage: $0 <xcodebuild-log-file> [top-N]}"
TOP_N="${2:-10}"

if [[ ! -f "${LOG_FILE}" ]]; then
  echo "ERROR: log file not found: ${LOG_FILE}" >&2
  exit 1
fi

# Confirm the captured build succeeded. A failed build has misleading timings.
if grep -qE "^\*\* BUILD FAILED \*\*|xcodebuild: error:" "${LOG_FILE}"; then
  echo "ERROR: ${LOG_FILE} contains a failed build; refusing to report timings." >&2
  exit 2
fi

echo "==> Top ${TOP_N} slowest function bodies in ${LOG_FILE}"
grep -E '^[0-9]+(\.[0-9]+)?ms[[:space:]]+/.*\.swift:[0-9]+:[0-9]+' "${LOG_FILE}" \
  | sort -t 'm' -k1,1 -rn \
  | head -n "${TOP_N}" \
  | awk '{ printf "%-12s %s\n", $1, $2 }'
```

This version uses only POSIX tools, so it runs with the `awk` and `sort` that ship with macOS. Check the output against one real line of your own log before you rely on it, because the line format comes from the Swift compiler and can change between Xcode versions. Run the script on demand, not on every CI push, because the diagnostic flags add overhead.

```bash
bash ci/parse-build-times.sh build-timing-detailed.log 10
```

## What usually makes Swift builds slow, and how do I fix it?

Look for these patterns in the top-10 list.

Complex type inference. A long fluent chain like `items.filter { ... }.map { ... }.reduce(0) { ... }` forces the solver to infer every intermediate type. Split it:

```swift
let active: [Item] = items.filter { $0.isActive }
let values: [Int]  = active.map { $0.value }
let total: Int     = values.reduce(0, +)
```

Large files with heavy generics. Files over roughly 1,000 lines with nested generics or protocol extensions with associated types are reliable hot spots. Split them into smaller files.

Unconditional run script phases. A phase that runs `pod install` or SwiftLint on every build without declared input and output file lists invalidates the build graph. Declare the files so Xcode can skip unchanged runs.

Whole Module Optimization in Debug. `SWIFT_COMPILATION_MODE = wholemodule` is a Release setting that maximizes optimization at the cost of single-threaded compilation. Confirm Debug is `incremental` in your `.xcconfig` or build settings pane.

| Fix | Effort | Where it helps |
|---|---|---|
| Explicit types on hot files | Low | Files in the top-10 with long solver time |
| Split files > 800 lines with heavy generics | Medium | Largest files in top-10 |
| Input/output file lists on run scripts | Low | Builds with 3+ run script phases |
| Debug `SWIFT_COMPILATION_MODE=incremental` | Very low | Projects that inherited `wholemodule` into Debug |
| Clang module caching | Medium | Mixed Swift/Obj-C projects |

Run `parse-build-times.sh` before and after each change on the same host and scheme to measure the real difference. Numbers from other teams on other hardware are not your baseline.

## Which host gives comparable build times?

One that keeps its caches between runs. Build-time results compare only on identical hardware with the same caches warm. A hosted Mac that keeps DerivedData, the SwiftPM `.build` directory and the signing keychain on one disk makes trend tracking meaningful. Preserve these between runs:

- `~/Library/Developer/Xcode/DerivedData`, keyed by scheme and git ref.
- The package `.build` directory for SwiftPM dependency artifacts.
- Signing keychains and provisioning profiles, kept per job and restored from your CI secret manager. Do not dump the environment to a file; transfer secret names and restore the values.

Avoid `xcodebuild clean` except when you are deliberately testing a cold build. A persistent disk is a cache, not a backup.

## Where should you profile Xcode builds for steady timings?

Profile on the Mac you will build on, so match the Xcode version first. The [Apple Silicon Mac VM](/mac-hosting#apple-silicon-vm) runs current Xcode, but its 4 vCPUs are shared, so timings can vary with a neighbor's load. A [Dedicated M4 Pro 48 GB](/mac-hosting#dedicated-m4-pro) is a whole Mac (M4 Pro, 48GB RAM), which gives steadier numbers and fits parallel Swift compilation. The [Intel Mac VM](/mac-hosting#intel-vm) fits only when Xcode 26.3 or earlier on macOS Sequoia is enough.

Cost arithmetic for common cases, before tax:

- Two-week spike on the Intel VM: 14 × $2 = $28 on the daily plan, or $25 on the monthly plan. The monthly plan is cheaper once you need 13 days or more.
- Two-week spike on the Apple Silicon VM: 14 × $6 = $84 daily, or $39 monthly.
- Three months of profiling and migration on the Apple Silicon VM: 3 × $39 = $117.
- Twelve months on a Dedicated M4 Pro 48 GB: $3,228 paid up front, or 12 × $299 = $3,588 on a 12-month term.

A spike that produces a top-10 list and two fixes rarely needs a dedicated Mac. A permanent shared runner often does. You run your own CI runner on the Mac.

Related guides:
- [Build Signed IPAs with Fastlane Gym in CI](/blog/mac-fastlane-gym-ipa-signing-ci)
- [Set Up a GitHub Actions Self-Hosted Runner on Mac](/blog/mac-github-actions-self-hosted-runner)
- [Set Up Remote Xcode on Windows](/blog/mac-xcode-on-windows-remote-setup)
