# Why 100% Xcode 27 CAS Hits Still Take 21.8s

*By Jin Li ([@jinfizz](https://x.com/jinfizz) / [github.com/kiyoakii](https://github.com/kiyoakii)) | 2026-11-26*

If you run three or four parallel Claude Code or Codex sessions on an iOS repository, each agent gets its own `git worktree`. With default Xcode settings, every new worktree gets a fresh `DerivedData` hash and compiles the project from scratch.

The obvious fix in Xcode 27 is turning on Apple's compilation cache (`COMPILATION_CACHE_ENABLE_CACHING=YES`) alongside prefix mapping (`SWIFT_ENABLE_PREFIX_MAPPING`, `SWIFT_ENABLE_PROJECT_PREFIX_MAPPING`, and `SWIFT_OTHER_PREFIX_MAPPINGS`) while isolating `DerivedData` per worktree. Variations of that flag recipe are making the rounds on X and engineering blogs right now, with teams reporting that new worktrees drop to tens of seconds.

That is the exact path we built and benchmarked first when designing XCLoom. We took it all the way to a 100.0% compilation cache hit rate (`77` hits, `0` misses) across every target in Apple's `sample-food-truck` app.

The build still took 21.8 seconds, barely faster than plain `xcodebuild` at 24.6 seconds.

This post walks through what we found by decoding Xcode 27's `llvm-cas` keys, reading `-showBuildTimingSummary`, and inspecting `swift-driver`'s binary `.priors` bitstream, and why those measurements forced us to stop wrapping `xcodebuild` and build a direct build engine that runs the same fresh worktree in 0.45 seconds (`1.00s` on `IceCubesApp`, `5.15s` on `Signal-iOS`).

## Five Worktree Scenarios We Measured

To avoid guessing, we built a reproducible harness (`scripts/bench/worktree-cache.sh`) that runs `git worktree add` and measures five everyday multi-agent scenarios on `Mac15,14` (M3 Ultra, 32 cores, macOS 26.6.2, Xcode 27.1, `CODE_SIGNING_ALLOWED=NO`, `env -i`, 3 cold-started rounds):

- Scenario A (Cold, first worktree): Empty cache, build `wt1`.
- Scenario B (No-change rebuild): Rebuild `wt1` immediately with nothing modified.
- Scenario H (Session handoff): Rebuild `wt1` unchanged, simulating a second agent shell with a different `PATH` and extra session environment variables.
- Scenario C (First build, new worktree): Create `wt2` at the same commit and build it.
- Scenario D (One-line edit after cache replay): Edit one line in `App/Orders/OrderRow.swift` inside `wt2` and rebuild.

Before touching anything, simply enabling `COMPILATION_CACHE_ENABLE_CACHING=YES` (`native-cache`) on `apple/sample-food-truck` produced 0 hits and 77 misses (`100.0%` miss rate) in Scenario C, taking `22.7s` against plain `xcodebuild`'s `24.6s`. Every worktree path still leaked into the cache key.

Getting from `0/77` to `77/0` exposed four subtle traps inside Xcode 27's caching layer, plus one hard architectural ceiling.

## Trap 1: Project Prefix Mapping Misses SwiftPM

A Swift or Clang compilation cache key in Xcode 27 is itself a CAS tree object holding the compiler command line. Decoding those keys locally with `llvm-cas --fcas-plugin-path=libToolchainCASPlugin.dylib` showed three distinct path problems:

1. `SWIFT_ENABLE_PREFIX_MAPPING=YES` and `CLANG_ENABLE_PREFIX_MAPPING=YES` normalize only the SDK, toolchain, and `DEVELOPER_DIR`. Worktree paths remain literal.
2. `SWIFT_ENABLE_PROJECT_PREFIX_MAPPING=YES` maps `PROJECT_DIR -> /^src`, `PROJECT_TEMP_DIR -> /^derived`, and `BUILT_PRODUCTS_DIR -> /^built`. App targets begin hitting across worktrees, while SwiftPM package targets still miss: their compiler key includes `-file-compilation-dir <worktree>/<App>.xcodeproj`, which sits outside the package's `PROJECT_DIR`. Worse, collapsing every target's `PROJECT_DIR` onto the same `/^src` makes it impossible to recover debug info for both the app and a local Swift package with a single LLDB `target.source-map`.
3. SwiftPM resolves canonical paths like `/private/tmp/...`, while Xcode normalizes project paths to `/tmp/...`. Mapping only one spelling misses the other.

Our fix in `xcloom --engine xcodebuild` was to turn `*_ENABLE_PROJECT_PREFIX_MAPPING` off and pass one unambiguous command-line mapping via `SWIFT_OTHER_PREFIX_MAPPINGS` and `CLANG_OTHER_PREFIX_MAPPINGS` (command-line overrides propagate to SwiftPM targets, whereas `.xcconfig` files do not): every spelling of the worktree root maps to `/^wt`, and every spelling of its `DerivedData` maps to `/^dd`, longest prefix first.

Compiler diagnostics still print real worktree paths (`<worktree>/CoreModel/Model3.swift`), and a generated `<DerivedData>/xcloom-lldbinit` restores LLDB source mapping (`settings set target.source-map /^wt <worktree> /^dd <DerivedData>`). Because `#filePath` literals and code coverage maps would otherwise record `/^wt/...`, prefix mapping turns itself off automatically whenever coverage flags or `test` / `build-for-testing` actions are requested.

In our multi-framework fixture ablation (`docs/worktree-cache.md`, Section 4.1), turning prefix mapping on dropped Scenario C compilation misses from 322 to 6 (`17.4s -> 11.4s`). On `apple/sample-food-truck`, misses dropped from 77 to 0.

## Trap 2: Private DerivedData Splits 4 Caches

In Xcode's default layout, four shared stores live at the root of `DerivedData`:

- `CompilationCache.noindex`
- `SDKExplicitPrecompiledModules`
- `SDKStatCaches.noindex`
- `ModuleCache.noindex`

When you pass a private `-derivedDataPath` per worktree to prevent write locks, Xcode silently creates private copies of all four stores inside each worktree. Every new worktree then re-runs roughly 200 SDK explicit-module tasks (`+2.0s` in our ablation even when hitting cache) and duplicates hundreds of megabytes of CAS plugin data on disk. Pointing all four settings (`COMPILATION_CACHE_CAS_PATH`, `SDK_EXPLICIT_MODULES_OUTPUT_PATH`, `SDK_STAT_CACHE_DIR`, `MODULE_CACHE_DIR`) to one shared directory cut per-worktree `DerivedData` on our fixture from `1.0 GB` to `0.32 GB`.

`SourcePackages` has the opposite problem: sharing one writable checkout across concurrent worktrees lets one resolution mutate files another worktree is compiling, while isolating it forces every new worktree to re-fetch from GitHub (`10.8s` vs `7.6s` in our GitHub-remote ablation). Snapshotting `SourcePackages` keyed by `Package.resolved` + Xcode build version and cloning it per worktree via APFS `clonefile(2)` gives each agent a private copy in milliseconds with zero network fetches.

One surprise from the ablation: disabling the index store (`COMPILER_INDEX_STORE_ENABLE=NO INDEX_ENABLE_DATA_STORE=NO`), which appears in almost every Xcode optimization snippet online, measured `19.1s` vs `19.0s` across three cold rounds on our fixture (within noise).

## Trap 3: Shell Env Breaks the Build Plan

In Scenario H, nothing in the repository changed. We only changed the calling shell's `PATH` and added session environment variables (the exact situation when a second coding agent picks up an existing worktree).

`xcodebuild` imports environment variables into build settings and folds them into task signatures. On our multi-framework fixture, changing session variables forced plain `xcodebuild` to re-run 93 tasks (`Ld`, `ExtractAppIntentsMetadata`, `GenerateTAPI`) in `8.5s`. On `apple/sample-food-truck`, it re-ran 18 tasks in `4.2s` (`5.0s` with `native-cache`).

Clearing the child environment with `env_clear()` and forwarding a strict allowlist (`HOME`, `USER`, `TMPDIR`, `DEVELOPER_DIR`, `SDKROOT`, `LANG`, `LC_*`, `GIT_*`, `SWIFTPM_*`, proxies, and explicit project allowlist keys) dropped Scenario H on `sample-food-truck` to 0 executed tasks in `2.0s`, and on the multi-framework fixture to 1 task in `2.8s`.

We hit the same task-signature sensitivity with our own embedded CAS Unix socket: originally each build opened a fresh random socket path, passed via `COMPILATION_CACHE_REMOTE_SERVICE_PATH`. Changing that path forced `ProcessInfoPlistFile` to re-run on every target during a no-change rebuild (`17` tasks instead of `11`). Making the socket path deterministic per `DerivedData` digest eliminated those redundant tasks.

## Trap 4: CAS Replay Breaks Swift Incrementals

This was the most surprising bug we uncovered, and it reproduces under plain `xcodebuild` with Apple's cache enabled.

Build `wt2` from a warm compilation cache (Scenario C: `77/77` hits, `0` misses). Now change one line in `App/Orders/OrderRow.swift` and rebuild (Scenario D).

Instead of compiling the edited file and its direct dependents (`4` tasks in `sample-food-truck`), `xcodebuild` + Apple CAS scheduled 33 compilations and 64 total tasks (`5.3s`, slower than plain `xcodebuild` with no cache at `4.3s`). On our multi-framework fixture, editing one file in a 36-file module triggered 33 cache queries and 61 tasks (`6.3s`) instead of `2` queries and `20` tasks.

Running `swiftc` with `-driver-show-incremental` explained why. The `.swiftdeps` files and dependency graph inside `<Module>-primary.priors` were already present and intact. However, when `swift-driver` replays a compilation output from CAS instead of compiling locally, it writes every replayed input record in the `.priors` LLVM bitstream with the 3-bit status `1` (`needsNonCascadingBuild`) rather than `0` (`upToDate`).

On the very next edit, `swift-driver` sees every sibling file in the module marked `needsNonCascadingBuild` and schedules the entire module.

We wrote a post-build `.priors` bitstream pass that checks `<DerivedData>/Build/Intermediates.noindex/**/*.priors` modified during the current build:

- It verifies the bitstream format version strictly against `SUPPORTED_VERSIONS` (`1.6`, verified on Xcode 27.1; any newer or unverified format version is skipped untouched with a diagnostic notice).
- For each input record whose status is `1` and whose recorded mtime still matches the source file on disk, it flips the 3-bit status field in place to `0` without changing file size or `.priors` mtime, re-verifying file bytes before writing.
- On `sample-food-truck`, scanning and patching `3` `.priors` files (`86` inputs) took `6.0 ms` (`30 ms` for `25` files / `324` inputs on our larger fixture).

The effect on Scenario D (`apple/sample-food-truck`, 3-round median):

| Config (Scenario D, 1-line edit) | Wall time | Cache hits / misses | Executed tasks | Root cause in `cache-why` |
| --- | --- | --- | --- | --- |
| Plain `xcodebuild` (`native`) | `4.3s` | `-` | `23` | Incremental baseline |
| `xcodebuild` + CAS without `.priors` fix | `5.3s` | `0 / 33` | `64` | `1` `input_changed` + `32` `same_module_changed` |
| `xcodebuild` + CAS + `.priors` fix | `4.4s` | `0 / 4` | `21` | `1` `input_changed` + `3` `same_module_changed` |

We verified correctness by running a cold-build binary differential (`diff_app.py`) on our multi-framework fixture comparing a fresh empty-cache build of the edited tree against a CAS-replayed + `.priors`-patched incremental build: `34/38` files in `BenchApp.app` (`6,728,736 bytes`, including every Mach-O binary, `Assets.car`, nib, `Info.plist`, and resource bundle) and `328/328` `.swiftconstvalues` were byte-identical after normalizing UUIDs, code signatures, and build-root paths (the remaining 4 are uncached App Intents files whose element order differs between two independent cold builds too).

## The Wall: 77/77 Hits, Still 21.8 Seconds

After fixing prefix mapping, shared caches, `SourcePackages` cloning, shell env scrubbing, deterministic CAS sockets, and `.priors` bitstreams, we ran the full 3-round suite on `apple/sample-food-truck` (`tests/corpus/bench/apple-food-truck-worktree-cache/summary.txt`).

Our own wrapper overhead was down to `20.82 ms` (`0.1%` of wall time) in Scenario C. Compilation cache hits reached `77 / 77` (`100.0%`) across `Food Truck` (`33/33`), `FoodTruckKit` (`33/33`), and `Widgets` (`11/11`).

And Scenario C still took 21.8 seconds (vs `24.6s` plain `xcodebuild`).

Here is the uncached tail straight from `cache-report-xcloom-C.json` (summed task-seconds from `-showBuildTimingSummary`; `204` tasks still ran even with zero cache misses):

| Uncached step inside `xcodebuild` (Scenario C, 77/77 CAS hits) | Tasks | Task-seconds | Covered by Apple CAS? |
| --- | --- | --- | --- |
| `CompileAssetCatalogVariant` | `3` | `19.383s` | No |
| `SwiftDriver` planning | `3` | `2.328s` | No |
| `Ld` (linking) | `7` | `0.586s` | No |
| `ValidateEmbeddedBinary` | `1` | `0.536s` | No |
| `AppIntentsSSUTraining` + `ExtractAppIntentsMetadata` | `5` | `0.373s` | No |
| `GenerateAssetSymbols` + `Copy` | `19` | `0.165s` | No |

On our larger multi-framework fixture (`docs/worktree-cache.md`, Section 6), `SwiftDriver` planning (`8.9s`) + `ScanDependencies` (`6.7s`) + cache-hit replay materialization (`9.4s`) + `Ld`/`GenerateTAPI` (`1.9s`) + App Intents (`1.8s`) + resources (`1.3s`) still left `11.4–12.4s` on the table even with `352/358` hits.

And one target class never hit across worktrees inside `xcodebuild` at all: SwiftPM macros. Xcode passes macro plugins as `-load-plugin-executable <DerivedData>/Build/Products/Debug/<Macro>#<Macro>`. Xcode's dependency scanner does not apply prefix mapping to that flag, and the producer worktree path gets baked into `.swiftmodule` outputs.

## Limits of Slot Mirroring and Product Reuse

Before leaving `xcodebuild`, we also tested skipping `xcodebuild` on identical trees via APFS product reuse (`XCLOOM_CACHE_REUSE_PRODUCTS=on`, rewriting Mach-O `LC_RPATH` entries and re-signing ad hoc). That dropped Scenario C to `0.2s` (`t=0`), and fixed-path worktree slot wrappers (tools that mirror your worktree into a pre-warmed directory at a constant path) attack the same case.

Both approaches share two structural limits:

1. The first build in each slot is still cold, and the moment an agent edits a single line of Swift inside the worktree, you fall right back to full `xcodebuild` incremental planning and relinking (in our product-reuse benchmark, Scenario D without background prewarming actually jumped to `12.0s` because no build database existed yet).
2. Wrapping `xcodebuild` cannot speed up `actool`, `ibtool`, `momc`, SwiftPM macro builds, or whole-project linking, nor does it fix `.pbxproj` merge conflicts, 2 GiB stock simulators, or Keychain signing prompts.

## Bypassing xcodebuild With a Direct Engine

The `77/77 -> 21.8s` result settled the architecture question. Apple's CAS only caches Swift and Clang frontend invocations inside `SWBBuildService`, while asset catalogs, dependency scanning, driver planning, linking, macros, and bundle assembly still run every time a new worktree appears.

XCLoom's default macOS Debug path (`--engine auto -> direct`) parses `.xcodeproj`, `.xcworkspace`, CocoaPods, and XcodeGen (`project.yml`) graphs directly, invokes Xcode's own local compilers (`swiftc`, `swift-frontend`, `clang`, `ld`, `ar`, `actool` / `xcloom-car`, `ibtool`, `metal`, `momc`, `appintentsmetadataprocessor`), and caches every clean action output (including asset catalogs, SwiftPM macros, Core Data codegen, and assembled products) in a worktree-independent content-addressed store backed by APFS `clonefile(2)`.

On `apple/sample-food-truck`, here is the progression from plain `xcodebuild` through 100%-hit Xcode 27 CAS to the direct engine (3-round medians on the same M3 Ultra):

| Approach on `apple/sample-food-truck` | New worktree (Scenario C) | 1-line edit (Scenario D) | No-change (Scenario B) |
| --- | --- | --- | --- |
| Plain `xcodebuild` (baseline) | `23.16s` (`24.6s` in worktree suite) | `5.43s` (`4.3s`) | `3.63s` (`2.6s`) |
| `xcodebuild` + `COMPILATION_CACHE` only (`0/77` hit) | `22.7s` | `5.2s` | `2.7s` |
| `xcodebuild` + CAS + Prefix Mapping + `.priors` fix (`77/0` hit, `100%`) | `21.8s` (`204` tasks ran) | `4.4s` (`21` tasks) | `2.6s` (`7` tasks) |
| XCLoom Direct Engine (`xcloom build --local`) | `0.45s` (`51.5x`) | `1.51s` (`3.60x`) | `0.22s` (`16.5x`) |

Then we ran the same unified 3-round harness (`scripts/bench/direct-engine-large-oss.sh`, revision `3c39d3c`, `XCLOOM_NO_GLOBAL_CAS=1`, all 6/6 outputs verified with `codesign --verify --deep --strict`, `plutil -lint`, and Scenario D Mach-O symbol checks) across six public open-source iOS apps:

| Public OSS Repo | Cold build (A) `xcodebuild -> xcloom` | New worktree (C) `xcodebuild -> xcloom` | 1-line edit (D) `xcodebuild -> xcloom` | No-change (B) `xcodebuild -> xcloom` |
| --- | --- | --- | --- | --- |
| `apple/sample-food-truck` | `23.90s -> 7.78s` (`3.07x`) | `23.16s -> 0.45s` (`51.5x`) | `5.43s -> 1.51s` (`3.60x`) | `3.63s -> 0.22s` (`16.5x`) |
| `Dimillian/IceCubesApp` | `45.68s -> 26.85s` (`1.70x`) | `45.97s -> 1.00s` (`46.0x`) | `12.94s -> 3.30s` (`3.92x`) | `12.08s -> 0.63s` (`19.2x`) |
| `Ranchero-Software/NetNewsWire` | `23.22s -> 14.27s` (`1.63x`) | `22.47s -> 1.60s` (`14.0x`)* | `14.29s -> 3.49s` (`4.09x`) | `15.12s -> 0.51s` (`29.6x`) |
| `jellyfin/Swiftfin` | `70.55s -> 56.65s` (`1.25x`) | `58.70s -> 3.03s` (`19.4x`) | `32.83s -> 18.28s` (`1.80x`) | `11.74s -> 2.03s` (`5.78x`) |
| `signalapp/Signal-iOS` | `76.70s -> 69.02s` (`1.11x`) | `74.10s -> 5.15s` (`14.4x`) | `31.16s -> 14.18s` (`2.20x`) | `10.27s -> 1.94s` (`5.29x`) |
| `wikimedia/wikipedia-ios` | `62.34s -> 48.37s` (`1.29x`)* | `47.36s -> 11.95s` (`3.96x`) | `23.96s -> 16.00s` (`1.50x`) | `23.50s -> 9.70s` (`2.42x`) |

*Cells marked with an asterisk are excluded from headline ranges because their native `xcodebuild` baseline varied by >15% relative to the earlier quiet-host baseline (`89bba10`). Raw per-round TSVs and host load samples are committed in `tests/corpus/bench/large-oss-direct-engine/`.*

And when an agent makes a 1-line Swift function-body edit inside a large monolithic app like `Signal-iOS`, even `14.18s` is still dominated by relinking an `85.4 MB` `Signal.debug.dylib`. For function-body edits, `xcloom hot --local` compiles and signs an `89 KB` `.dylib` live patch instead of relinking the app: in a 3-round interleaved benchmark on `signalapp/Signal-iOS` (Xcode 27.2, shared-host 1-min load `27.89–40.57`), warm edits dropped from `27.37s` (`24.54–29.12s`) to `1.93s` (`0.71–2.21s`, `14.2x`; median round breakdown `1243 ms` compile, `423 ms` link, `30 ms` sign).

## Where Direct Engine Still Has Limits

Leaving `xcodebuild` is a real engineering tradeoff, and we keep its boundaries explicit:

- Fallback under `--engine auto`: Standalone `Package.swift` apps without an `.xcodeproj` or rare unmodeled main-target build rules fall back automatically to `xcodebuild` (with all four worktree-cache fixes above still active) and print `engine_fallback_reason` in JSON and on `stderr`. Unmodeled non-app dependency targets use per-target hybrid delegation (`xcodebuild -target <Dep>` cached into Direct CAS) instead of falling back for the whole workspace.
- Structural ABI edits in large monoliths: `xcloom hot` handles Swift function-body edits. Adding stored properties, changing protocol conformances, or editing class layouts triggers a full relink (`14.18s–18.28s` across `Signal-iOS`, `wikipedia-ios`, and `Swiftfin`).
- Machine scaling: All numbers above were measured on a 32-core / 512 GB M3 Ultra Mac Studio. Smaller 8–16 core Macs have not been benchmarked yet; run the scripts below to measure your own machine.

## Reproduce It on Your Own Mac

Both benchmark scripts live in the repository (`FSL-1.1-ALv2`, free locally for personal, commercial app, and internal CI use):

```bash
brew install xcloom/tap/xcloom
git clone https://github.com/xcloom/xcloom.git
cd xcloom

# Compare plain xcodebuild vs Apple CAS vs XCLoom xcodebuild-cache mode
bash scripts/bench/worktree-cache.sh run --scenarios A,B,H,C,D --rounds 3

# Run the 6-repo direct-engine suite
bash scripts/bench/direct-engine-large-oss.sh --preset icecubes --rounds 3

# Inspect why any xcodebuild/fallback run missed the cache
xcloom cache-why MyApp --repo .
```

Full 24-cell tables, load logs, and raw TSVs: [`https://xcloom.dev/benchmarks/`](https://xcloom.dev/benchmarks/). Source and scripts: [`https://github.com/xcloom/xcloom`](https://github.com/xcloom/xcloom).
