Categories
apple ios

Xcode Build Time Optimization — Part 1

Learn easy methods to measure the Xcode construct time

Maksym Shcheglov

Photo via Barn Images on Unsplash

Almost each developer every so often suffers from reasonably lengthy Xcode construct time. It leads to decrease productiveness and slows down the advance means of the entire staff. As you’ll see, bettering construct time is an important as it has an immediate have an effect on on time to marketplace for transport new options sooner.

In this put up, we’ll learn to profile the Xcode construct and get its metrics. In the following article, I’ll undergo tactics to get to the bottom of bottlenecks and accelerate the construct. It will have to be discussed as smartly, that we’ll be the usage of Kickstarter iOS assignment, that may be discovered on Github. So let’s get began!

The very first thing we will have to do is to outline what are we looking to measure and optimize. There are two choices to believe:

  • Clean construct — cleansing and rebuilding a assignment from scratch. Frequently blank construct is finished on CI to validate a pull request for correctness and run unit checks.
  • Incremental construct — rebuilding a assignment after some supply code adjustments. This construct is created via a developer whilst operating on a brand new characteristic.

In lots of the instances, Clean construct time enhancements will have to speedup the Incremental construct as smartly. The most suitable option can be to generate metrics for each construct varieties and stay observe of them. We’ll be measuring builds created with Debug configuration best as it’s used lots of the occasions and has a better have an effect on on building.

The best means of making improvements to the construct time will have to be a data-driven method whilst you introduce and examine adjustments in keeping with the construct metrics. Let’s dive into it and take a look on the gear we will use to get insights in regards to the assignment’s construct time.

We can get the construct time simply by way of Xcode. It helps to keep observe of your whole builds via default and you’ll read about the days and logs from the Report Navigator.

There is an approach to show identical data within the Xcode job viewer. You can permit it from the command line:

defaults write com.apple.dt.Xcode DisplayBuildOperationDuration YES

The builds period seems after a construct, along with the “Succeeded” message.

These are simply two fundamental choices that are supposed to provide you with a coarse concept about Clean and Incremental construct time.

Xcode Build Timing Summary is your first buddy in getting construct time insights and discovering bottlenecks. You can run one by way of Product->Perform Action->Build With Timing Summary. Now you will have to see a really perfect breakdown of occasions spent on other duties:

It will have to be a pleasant start line to determine probably the most time-consuming duties inside of your construct procedure. As you’ll see from the screenshot above, CompileStoryboard, CompileXIB, AssembleSwiftSources and PhaseScriptExecution stages took lots of the construct time. Xcode controlled to run one of the most duties in parallel that’s why the construct is completed a lot sooner than the time it took to run each and every of the instructions.

We can get construct timing abstract for blank construct the usage of xcodebuild with -buildWithTimingSummary possibility:

Now let’s get identical metrics for the incremental construct. It will have to be discussed that incremental construct time absolutely relies on the recordsdata being modified on your assignment. To get constant effects you’ll trade a unmarried record and rebuilt the assignment. Unlike Buck or Bazel, Xcode makes use of timestamps to stumble on what has modified and what must be rebuilt. We can replace a timestamp the usage of contact then:

If Swift bring together time is the bottleneck, we will get additional information via surroundings Other Swift Flags from the Xcode construct settings. With those flags enabled Xcode will generate a caution for any operate or expression that took longer than 100ms to type-check:

  • -Xfrontend -warn-long-function-bodies=100
  • -Xfrontend -warn-long-expression-type-checking=100

Now you recognize the code Swift compiler has issues of and will get a hold of some enhancements.

The Swift compiler has various integrated diagnostic choices you’ll use to profile the construct.

  • -driver-time-compilation — high-level timing of the roles that the driving force executes.
  • -Xfrontend -debug-time-compilation — timers for each and every segment of frontend process execution.
  • -Xfrontend -debug-time-function-bodies — time spent typechecking each operate in this system.
  • -Xfrontend -debug-time-expression-type-checking — time spent typechecking each expression in this system.

Let’s use -debug-time-compilation flag to get the highest slowest recordsdata to bring together:

As you’ll see, it took 25s to bring together SettingsNewslettersCellViewModel.swift. From the construct log, we will get extra details about record compilation time:

Now it’s transparent that Type checking and Semantic research is probably the most time-consuming process. Let’s transfer ahead and listing best slowest operate our bodies and expressions within the Type Checking level:

Just like that, we’ve profiled our construct and came upon that we have got reasonably some bottlenecks within the Type Checking level. As the next move you’ll take a look on the purposes and expressions indexed above and take a look at to optimize variety inference.

It will have to be at hand to measure the goals construct occasions one after the other and display them on a chart. One can lend a hand working out which goals are constructed or can also be in-built parallel. We can use xcode-build-times-rendering device for this. Let’s set up one as a RubyGem:

gem set up xcode-build-times

After set up is whole, run the next command that injects timestamp logging in Run Script Build Phase of your goals:

xcode-build-times set up

Then construct the assignment and generate a record by way of:

xcode-build-times generate

As a end result, you will have to get a pleasant build-time Gantt chart, that presentations the construct occasions of your whole goals:

It can be nice to combination other metrics discussed above. XCLogParser is a handy gizmo that mean you can with this. It is a log parser for Xcode-generated xcactivitylog and offers numerous insights with reference to construct occasions for each module and record on your assignment, warnings, mistakes and unit checks effects. You can set up it via cloning the repository and run by way of command line:

git clone https://github.com/spotify/XCLogParser
rake construct
xclogparser parse --project Kickstarter --reporter html

This is the record created for the Kickstarter iOS assignment:

It will have to be identified that construct time metrics are depending on {hardware} and its usage. You can use your device for experiments. The more sensible choice can be automating the method up to you’ll and the usage of a devoted CI device to get metrics day-to-day. Eventually, you’ll stay observe of them on a dashboard and notify in regards to the construct time degradation by way of Slack.

The construct time speedup is an important for builders productiveness and the product’s time to marketplace. Today we’ve discovered easy methods to measure the Xcode construct time and get some metrics to research 🎉.

How do you measure your construct time? Are you the usage of any of the tactics discussed on this article? Feel unfastened to achieve me out on Twitter and let me know your ideas or ask questions you will have.

Thanks for studying!