Improve build times in Swift with Xcode

2 minute read

When coding, I like to get compiler feedback fast. With the subsequent builds seconds can add up to minutes. The sluggishness can derail my focus and workflow.

While gulping a drink, I glanced at the activity view bar as Xcode was building project. I saw that until compilation finishes I will sip another glass. It was slow. Was the size of the code base stretching the compiler limits?

Quick builds contain distractions and make the work a pleasure. TDD requires rapid builds. They save time and sanity when running tests again and again. Compilation times should be in seconds. Can that speed our work and increase our response to bugs? Ofcourse.

Profiling

Xcode gives a way to find code lagging the compiler. Here’s how.

In project Build Settings under Swift-Compiler - Custom Flags, Other Swift Flags add:

Subistitute the 300 argument with what suits you best. If compiler goes beyond the specified millisecond limit, Xcode will warn you and show you the offending place.

Improving

How to improve build times? The easiest way:

  • Use explicit types for complex properties. You save the time for the compiler to figure out the type. And you save your colleagues time on figuring out the type of the property.
  • Provide types in complex closures.

  • Don’t use + to concatenate a strings.
  • Precompute. Never do computations directly from the if-else conditions.

  • Dependencies within a module are per-file. Dependencies across targets are for the entire target. Dependencies determine what areas of code the system builds.
  • Incremental Builds are file-based. Unrelated changes outside function bodies can still result in rebuilding. If you change the function bodies then other files won’t be recompiled, as changes in function bodies do not affect the file’s interface.
  • Limit your objective-C/Swift interface. Keep your generated header minimal. User private when possible:
    • Make IBOutlet private and IBAction private.
    • Make methods exposed to Objective-C @objc private. Or you can switch to block based APIs, which may additionally cleanup your code.

Depending on your project and your needs, your can do more. Be sure to watch:

References