问题
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?
回答1:
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.
回答2:
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
).
回答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:
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
回答5:
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.
来源:https://stackoverflow.com/questions/40406392/speed-up-xcode-swift-build-times