The Swift compiler is especially slow when parsing and compiling simple Set, Dictionary and Array literals due to costly type examining procedures.
Unfortunately, we can not avoid some code struct take a long time to compile. In other words complex code struct weary the compiler. We can reduce that struct to avoid. Let me show you to how we can optimize the code and how we can fast the compiler with 6 simple steps.
Here we go:
1- Showing build times in Xcode
Above all, we need the measure compile times. It’s important because, before you have seen your compile times you can’t understand what’s wrong and what you have to change.
To display you should enable the compile timer. Open the command line and paste the following code line and press the enter.
defaults write com.apple.dt.Xcode ShowBuildOperationDuration -bool YES
And then build the project, you have to see below screenshot. Maybe you need to clean project(cmd + option + k) or deleting project derived data folder to measure build times.
rm -rf ~/Library/Developer/Xcode/DerivedData
2- Find code that compiles slowly
if you suspecting your method or expression you can identify that with a little changing in Xcode
Open the project’s build settings and add the following flags to your
Other Swift Flags. As in the following screenshot.
The first line shows you a function of how long it takes.
The second line so shows you an expression of how long it takes.
When you build the project, you can see the warning times in the Xcode build log. And then you can fix or reorganize that function or expressions.
3- Setting the Build active architecture Only
Go your project’s “Build Settings” search the “Build Active Architecture Only”.
When you run your project in debug mode is selected you should “build the active architecture only” is should be set to “YES”. If release mode is selected you must set to “NO”
4- Code Generation-(Optimization Level, Compilation Mode)
Apple recently introduced “Compilation Mode” and “Optimisation Level” that give us more control to change the compiler mechanism over the builds.
The “Optimization Level” build setting can be set to “No optimization” for the debug builds and for the release builds it can be set to “Optimize for Speed”.
In the Xcode 10, that is “Optimize for Size” can be used to check the code size.
5- Xcode has a new build system
In Xcode 9 Apple introduced a new build system, that’s completely writing with Swift. The “new build system” comes as default selected.
6- Manageable and Accessible Dependencies
Reorganize your dependency and third-party framework. Increase easily access to your dependency.
If you are using multiple pods for multiple targets you can organize the dependencies like following.
def shared_pods pod ‘ModellingSDK’, ‘1.7.0’
pod ‘NetworkingSDK’enddef sdk_pods source ‘https://github.com/CocoaPods/Specs.git'
source ‘https://bitbucket.org/somesdk/somesdk.git' pod ‘Fabric’
pod ‘Crashlytics’endtarget ‘mainTarget’ do platform :ios, ‘9.0’ shared_pods
sdk_podsendtarget ‘extensionTarget‘ do platform :ios, ‘10.3‘
Do more caching strategy of dependency management to speed up the build. Add implicit dependencies instead of explicit dependencies. Choose a dependency management system that is should be right for your need. (Cocoapods, Carthage).