🔦 Speed Up Compilation with Prebuilt Swift-Syntax

TL;DR: Starting with Swift 6.1.1 and Xcode 16.4, you can dramatically reduce build times for macro projects by enabling prebuilt SwiftSyntax. Use Xcode defaults or the —enable-experimental-prebuilts flag in command-line builds to skip rebuilding SwiftSyntax from source. Clear existing build caches before first use for best results.

Background

Since the introduction of macros in Swift 5.9, developers have increasingly created and used custom macros to boost productivity. However, the large Swift-Syntax dependency required by macros often leads to significantly longer build times, becoming a major pain point in development.

Solution: Enable Prebuilt Swift-Syntax

Starting from Swift 6.1.1 (Xcode 16.4), developers can use a prebuilt version of Swift-Syntax during compilation, avoiding the need to rebuild it from source and significantly speeding up project builds.

Xcode Configuration

Run the following commands in Terminal to enable or disable prebuilt support in Xcode:

Bash
# Enable
defaults write com.apple.dt.Xcode IDEPackageEnablePrebuilts YES

# Disable
defaults delete com.apple.dt.Xcode IDEPackageEnablePrebuilts

Command-Line Builds

When using swift build or swift test, add the following flag to enable prebuilt support:

Bash
swift build --enable-experimental-prebuilts

Notes

  • Requires Swift 6.1.1 or later.
  • Before first use, it’s recommended to clear build caches:
    • Xcode projects: delete the DerivedData directory.
    • Command-line projects: delete the .build directory.

Real-World Performance

After enabling this feature, building a custom macro project dropped from tens of seconds to just a few seconds—greatly improving the development experience.

Further Reading

If this article helped you, feel free to buy me a coffee ☕️ . For sponsorship inquiries, please check out the details here.

Weekly Swift & SwiftUI highlights!