7

As my project has grown over the past year, so have its build times. Over the last few months it's gone from 4 minutes to around 7 (time includes GitHub pull, unit tests, etc).

I have investigated with -Xfrontend -debug-time-function-bodies to find lines that are slow to compile, and changed that code.

I believe it's now a question of project size; 182 Swift files, ≈31K lines. 23 storyboards, 52 XIBs. This is a regular UIKit app with a handful of Cocoapods dependencies.

The bulk of the build time is spent in the "Compiling Swift source files" phase.

The build machine time I care less about than the edit-build-debug cycle, which has also been slowing.

What can be done to improve build times?

3
  • 7
    Factor some code out into frameworks. Those don't have to get compiled every time you change something. Commented Nov 3, 2016 at 16:15
  • Have you tried without xibs? I built a project in Swift 1.x with no xibs and no obvious difference in compile times, but I've since joined a company where there are xibs everywhere, so haven't had chance to test this out with a large project. Commented Nov 3, 2016 at 16:18
  • Physical hardware plays a big part too, a Mac with a fast SSD and a lot of physical threads will crunch through Swift source files. Commented Nov 5, 2016 at 13:20

5 Answers 5

4

Here's an article about benchmarking/speeding up compilation time - swift-profiling.

In case it goes dead here is the tldr:

xcodebuild -workspace App.xcworkspace -scheme App clean build OTHER_SWIFT_FLAGS="-Xfrontend -debug-time-function-bodies" | grep .[0-9]ms | grep -v ^0.[0-9]ms | sort -nr > culprits.txt

You can just run that or add the following flags to your build under the other-swift-flags in build settings:

-Xfrontend -warn-long-function-bodies=100

This will show you which lines are slowing down your compile time.

Sign up to request clarification or add additional context in comments.

1 Comment

@ricardopereira I don't see why it wouldn't. Let me know if it doesn't.
3

Xcode 8.1/Swift 3.1 adds some relief for projects including some ObjC. Precompiled headers are back! https://swift.org/blog/bridging-pch/

If your project includes a bridging header, this will help. (In Xcode 8.1 beta 4 and later, this is the default; in prior betas add -enable-bridging-pch to Other Swift Flags).

Comments

3

Turning on the Whole Module Optimization while adding -Onone in Other Swift Flags worked for me, it reduced compilation time to 3 minutes from 10.
Read more here - Speed Up Swift Compilation

I'm using Swift 3 on Xcode 8.3.

4 Comments

I'm amazed. My build went from 68s to 52s. Xcode bug? Debugging still works fine.
I opened Radar 31539545 to ask Apple if there are negative repurcussions, and if not, could this become Xcode's default.
Hi what does the -Onone flag do ? Thank you.
-Onone tells the compiler not to optimize anything, leaving it in a more easily debuggable state.
1

Checkout this great post: https://github.com/fastred/Optimizing-Swift-Build-Times

Some contents included:

  • Type checking of functions and expressions
  • Slowly compiling files
  • Build active architecture only
  • dSYM generation
  • Whole Module Optimization
  • Third-party dependencies
  • Modularization
  • XIBs
  • Xcode Schemes
  • Use the new Xcode build system
  • Enable concurrent Swift build tasks
  • Showing build times in Xcode

Comments

-1

Since Xcode 9.3 there is a Compilation Mode called Single File. It's explicitly said that it can change a game when talking about compilation times in the Xcode release notes

The choice for compiling Swift code by file or by module moved from the Optimization Level setting to Compilation Mode, which is a new setting for the Swift compiler in the Build Settings pane of the Project editor. Previously this choice was combined with others in the Optimization Level setting. Compiling by file enables building only the files that changed, enabling faster builds. Compiling by module enables better optimization.

It truly is a life saver. My incremental compilation times for enterprise project was decreased to couple of seconds. You can try it.

enter image description here

EDIT: In Xcode 12, the same can be referred as Incremental

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.