Skip to content

Turn on Xcode compilation caching for SwiftPM package targets - #60520

Open
chrfalch wants to merge 1 commit into
mainfrom
spm-compile-cache-workspace-setting
Open

chrfalch wants to merge 1 commit into
mainfrom
spm-compile-cache-workspace-setting

Conversation

@chrfalch

@chrfalch chrfalch commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Xcode's compilation cache makes a warm clean build of a React Native SwiftPM app much faster. In a test app on Xcode 27.2, a Debug build went from 43.6 s to 12.9 s. But Xcode applies the cache to Swift package targets only through a per-user workspace setting, CompilationCachingSetting. Project and xcconfig build settings, the shared workspace settings and Package.swift flags do not reach the package targets.

This change makes the "Sync SPM Autolinking" build phase turn the setting on:

  1. On every build, the phase checks <App>.xcodeproj/project.xcworkspace/xcuserdata/<user>.xcuserdatad/WorkspaceSettings.xcsettings.
  2. If the file is missing, the phase creates it with CompilationCachingSetting = Enable.
  3. If the key is missing, the phase adds it with plutil and keeps the other keys.
  4. If the key has any value, for example Disable, the phase leaves it.
  5. If the file is not a valid plist, the phase leaves it.
  6. RCT_SPM_SKIP_COMPILATION_CACHE_SETTING=1 in the build environment turns the step off.
  7. The step never fails the build. spm deinit leaves the setting in place.

Xcode reads the setting from the next build on. The build that writes it does not use the cache. The next build fills the cache, and later builds hit it. Existing apps get the step on their next react-native spm update, which regenerates the sync script.

Results

A SwiftPM test app (RN 0.89.0-rc.4, Xcode 27.2, iOS simulator, Apple Silicon Mac). Three runs for each column. "Without cache" is the median of 3 runs. "Cache, empty" is run 1, which starts with an empty cache. "Cache, filled" is the mean of runs 2 and 3. A clean build is xcodebuild clean and then build. xcodebuild clean does not delete the compilation cache.

Build Without cache Cache, empty Cache, filled
Clean build, Debug 45.7 s 49.5 s 14.9 s (3.1x faster)
Clean build, Release 78.2 s 81.4 s 17.7 s (4.4x faster)
Build with no change, Debug 4.5 s 4.6 s 4.5 s
Build after a one-line edit in AppDelegate.swift, Debug 5.0 s 8.1 s 8.3 s
  • The first build with an empty cache is about 4 s slower, because it writes the cache.
  • A build after a small edit is about 3 s slower with the cache on. Most of a typical day is these builds. This cost comes from Xcode, not from this change.
  • These runs turned the cache on with COMPILATION_CACHE_ENABLE_CACHING=YES on the xcodebuild command line. The Test Plan below shows that the per-user setting from this change gives the same cache hits.

Changelog:

[IOS] [ADDED] - SwiftPM: the sync build phase turns on Xcode compilation caching for Swift package targets in the per-user workspace settings

Test Plan

  • node_modules/.bin/jest --no-cache -i packages/react-native/scripts/spm: 1270 of 1270 tests pass. The new tests run the generated script in a temp folder. They cover: create the file, keep other keys, keep a user's value, leave a malformed file, and the opt-out variable. One lifecycle test runs the real sync-autolinking.sh. The test that needs plutil -insert skips on non-macOS hosts.
  • yarn flow-check: 0 errors. ESLint and Prettier pass on the changed files.
  • A SwiftPM test app (RN 0.89.0-rc.4, Xcode 27.2, Debug, simulator), built with xcodebuild -project and no compilation-cache setting on the command line or in the project:
    • Build 1 wrote the setting. 0 of 327 package compiles came from the cache.
    • Build 2, a clean build, filled the cache. 0 of 327 came from the cache.
    • Build 3, a clean build: 327 of 327 package compiles came from the cache, in 14.8 s.

Xcode applies its compilation cache to Swift package targets only through
the per-user workspace setting CompilationCachingSetting. Project and
xcconfig build settings do not reach them. The Sync SPM Autolinking build
phase now writes CompilationCachingSetting=Enable to the user's
WorkspaceSettings.xcsettings when the key is missing. It keeps other keys,
leaves an explicit value or a malformed file alone, never fails the build,
and is turned off with RCT_SPM_SKIP_COMPILATION_CACHE_SETTING=1.

Signed-off-by: Christian Falch <christian.falch@gmail.com>
@chrfalch
chrfalch force-pushed the spm-compile-cache-workspace-setting branch from becf804 to 6265176 Compare December 3, 2026 22:11

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant