Improve your Xcode (Swift) Compile Time

Alexandros Smponias
Sep 7, 2019 · 4 min read
Image for post
Image for post

Nobody likes slow compile times…
They are annoying. Oh thank you Xcode. Don’t tell me after 2 minutes that there is a typo… 🤬

But we can improve the Xcode compile time with a few simple tricks. 😎
Follow me to learn more about Swift, Xcode, UX-Design and more.

Image for post
Image for post

It is time for change

1) Measure your compile time correctly ⏱️

After you have enabled your timer you should see your compile time in Xcode.

Image for post
Image for post
Compile time in Xcode (Without cleaning)

The compile time is pretty low. Too low. If you want a more accurate compile time from Xcode you should clean your build folder before measuring the time. Just use ⌘+⌥+K

Image for post
Image for post
Compile time in Xcode. After cleaning (including the build folder)

But if you want the most accurate compile time from Xcode you should clean your Xcode Project (including the build folder -⌘+⌥+K ) and delete your derived data. Just open your terminal and enter

rm -rf ~/Library/Developer/Xcode/DerivedData

Image for post
Image for post
Compile time in Xcode. After cleaning (including the build folder) and deleting the derived data

When measuring your compile time you should always clean your project (including the build folder) and delete your derived data. This is the method I am using while writing this article.

2) Get a warning for slow code ⚠️

-Xfrontend -warn-long-function-bodies=50

-Xfrontend -warn-long-expression-type-checking=50

If a function or expression needs more than 50 milliseconds to compile you’ll get a warning. (Normally you would take a higher number but I like to get more warnings to improve my code faster…)

Image for post
Image for post
A random function that takes too long to compile

3) The basic improvements 😃

Just enter the following command into your terminal

defaults write com.apple.dt.Xcode BuildSystemScheduleInherentlyParallelCommandsExclusively -bool NO

This “new” Feature was released with Xcode 9.2 (Release Notes) and they mentioned that it could also slow down some of your projects because it is using more memory. To disable it just enter the following command into your terminal

defaults delete com.apple.dt.Xcode BuildSystemScheduleInherentlyParallelCommandsExclusively

(FYI: I used Xcode 10.3 (10G8) when writing this article and I thought this feature is enabled by default now… But it wasn’t).

4) Some coding guidelines 📜

Use let, private etc. whenever you can.

Xcode normally tells you that you should use let instead of var but sometimes it doesn’t…

final class ViewController: UIViewController {
var lol = Lol()
var lol2 = Lol2()
.....
var lol100 = Lol100() }

After changing the vars to let my compile time went from about 6.5s to 6.3s.

Make your classes final (If wanted).

Final Classes are increasing your performance and decreasing the compile time. Apple Docs

We started to make all our classes final (and remove the final keyword if we want to overwrite a class)

The thing with type inference.

I will make it short. Stop using variables without declaring it’s type. If you don’t do it, the compiler has to… And surprise. It will increase your compile time.

No, no, no. Stop 🛑
var name = getName()

Are you a senior developer?!
var name: String = getName()

It’s time to thank some people who “helped” me with this article and optimizing my compile times…

Thanks to Tyler Milner for his great medium article:

Thanks Patrick Balestra for his awesome blog post:

If you liked this article fell free to give it a clap 👏🏻 It helps me and other people who don’t know if they should read this article.

Flawless iOS

🍏 Community around iOS development, mobile design, and…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store