Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 4

Analyzing Call Tree Views for App Hotspots

Recommended resources

Welcome to our next lesson. In our last session, we focused on how to navigate a trace document and isolate a specific period of interest using time range filtering and os_signpost markers. This brought you to the doorstep of the problem: the call tree for the relevant time slice. Now, it's time to step inside and learn how to read the room.

A raw call tree, even when filtered, can still be a dense forest of function calls. The key to effective diagnosis is not just looking at the tree, but knowing how to reshape it to reveal the information you need. This lesson is dedicated to interpreting call tree views to pinpoint performance hotspots. You will learn to wield several powerful display options—inversion, flattening, and symbol filtering—to transform a confusing stack of calls into a clear map pointing directly to your app's most expensive code paths.

The Language of Call Trees: Total vs. Self Weight

Before we manipulate the call tree's structure, we must understand the fundamental metrics it presents. When you look at a call tree in the Time Profiler, you'll see two primary columns related to cost, often labeled Weight and Self Weight, or represented as percentages.

  • Total Weight (or Total %): This represents the time spent in a function and all the other functions it calls down the line (its callees). A high total weight on a function tells you that this branch of the call tree is expensive, but the work might be happening many levels deep within it.
  • Self Weight (or Self %): This is the time spent exclusively within the function's own code, excluding any time spent in functions it calls. A high self weight is a strong signal that the function itself is a "hotspot"—it is the direct source of significant CPU work.

Think of it as a project manager (viewDidLoad) delegating tasks to team leads (loadData, configureUI). The manager's total time cost includes all the work done by their teams. However, their self time is only the work they do personally. To find the bottleneck, you need to know if the manager is overworked or if one of their teams is struggling.

The image below provides a great visual breakdown of this concept.

This diagram illustrates how 'Total %' encompasses the time spent in a function plus all its children, while 'Self %' is confined to the function's own execution. Functions with high 'Self %' are your primary optimization targets.

As you can see, loadData() has a high total time (65%), but its self time is only 5%. The real work is happening in its callees, parseJSON(), processImages(), and fetchFromAPI(). The true hotspots, marked with high Self %, are resizeImage() and applyFilters(). Your goal as a performance investigator is to follow the high Total % values down the tree until you find the functions with high Self %.

To solidify these foundational concepts, let's turn to a quick reading.

An overview of the time profiler in Instruments - Augmented Code

This article from Augmented Code provides a concise explanation of the call tree view in Instruments.

Please read the first part of the article, from the beginning down to the section on filtering options. Pay close attention to the definitions of the weight and self columns.


Reshaping the Data: Advanced Call Tree Views

Now that you understand the core metrics, let's explore the display options that help you find what you're looking for. These are typically found in a configuration bar at the bottom or side of the detail pane.

The call tree configuration panel in Instruments provides several checkboxes to manipulate the view, such as 'Invert Call Tree' and 'Hide System Libraries'.

Filtering Out the Noise

In the last lesson, we touched on Hide System Libraries. This is your first and most important tool. As an experienced developer, you know that most of your time should be spent analyzing your own code, not Apple's frameworks. Toggling this on removes the system-level calls, dramatically decluttering the view.

However, be mindful that it can sometimes be misleading. A system function might be slow because you are passing it bad data or calling it inefficiently. If hiding system libraries makes a problem disappear, it might be worth toggling it back off to see which system call was taking the time and what part of your code was invoking it.

A video from Kodeco (formerly raywenderlich.com) explains this nuance well.

The Time Profiler - Practical Instruments with iOS 10 - raywenderlich.com

This clip from "Practical Instruments" demonstrates a scenario where Hide System Libraries can be misleading.

Watch the segment from hide system libraries. The presenter shows how this option can incorrectly point to an appDidFinishLaunching method as the bottleneck, when the real issue is revealed by looking at the system calls it triggers.

Inverting the Call Tree

The default call tree is a "top-down" view: it starts with the entry point (like main) and shows the functions that get called. But what if you want to answer the opposite question: "For this specific slow function, who is calling it?"

This is where Invert Call Tree is invaluable. It flips the tree "bottom-up," placing the most deeply nested functions (the leaves of the original tree) at the top level and showing their callers as children.

When to use it: You've identified a hotspot, like resizeImage(), which has a high Self Weight. You notice it's being called from multiple places. By inverting the tree, you can see all the different code paths that lead to resizeImage() grouped under a single, top-level resizeImage() entry, allowing you to see which path is the most frequent or expensive caller.

This video demonstrates the practical application of inverting the call tree.

Apple Debugging L9 - Instruments Time Profiler

This clip from the "Apple Debugging" series shows how inverting the call tree quickly reveals the root functions causing performance issues.

Watch from invert call tree. The presenter uses this option to immediately bring the expensive image decoding functions to the top of the list, making the source of the problem obvious.

Flattening Recursion

If your code contains a recursive function, the call tree can become incredibly deep and difficult to read, with the same function appearing over and over. Flatten Recursion collapses this entire sequence of recursive calls into a single entry in the tree. The time reported for this single entry is the cumulative time for all the recursive calls combined.

When to use it: Any time you are profiling code with recursion. It allows you to see the total cost of the entire recursive operation at a glance, rather than getting lost in the individual calls.

Top Functions: The Executive Summary

Sometimes you don't need the hierarchical view at all. You just want a simple, flat list of the functions that are consuming the most CPU time themselves. This is exactly what Top Functions provides. It's essentially a pre-sorted list of all functions by their Self Weight.

When to use it: This is often the fastest way to find your hotspots. Instead of manually drilling down through the call tree, you can switch to Top Functions to immediately see which functions are doing the most work. From there, you can select an interesting function and use another powerful tool: the Heaviest Stack Trace view in the right-hand inspector pane. This view shows you the most expensive call path that led to the function you selected. This combination is a highly efficient workflow for hunting down bottlenecks.

Using Xcode Instruments for C++ CPU profiling

This article, while using C++, perfectly explains the Top Functions and Heaviest Stack Trace workflow, which is identical for Swift/Objective-C.

Read the sections Top functions and Heaviest stack trace. Notice how these two views work together to quickly identify the most resource-intensive function and then show its most critical call path.


A Systematic Workflow for Analysis

With these tools in your arsenal, you can approach call tree analysis systematically rather than randomly.

  1. Initial Triage: Start with the standard, top-down call tree. Select the main thread. Enable Hide System Libraries.
  2. Drill Down: Use Option-click on the disclosure triangles to expand the "heavy" branches (those with high Total %). Follow the path of highest weight until you find where the Self % spikes. That's your likely culprit.
  3. Identify Callers: If you find an expensive function that is called from many places, enable Invert Call Tree to identify all its callers and determine which path is most problematic.
  4. Quick Scan for Hotspots: Switch to Top Functions for a "cheat sheet" view of the most expensive functions by Self %.
  5. Contextualize with Stack Trace: Select a function of interest in the Top Functions list and examine the Heaviest Stack Trace in the inspector to understand how you got there.
  6. Jump to Code: Once you've identified a problematic function in your own code, simply double-click its name in the call tree. Instruments will open the source file directly to that function, often highlighting the specific lines of code that are most expensive.

Conclusion

You are no longer just a passive observer of the call tree. By understanding the difference between Total and Self weight and by actively manipulating the display with inversion, flattening, and filtering, you can force the data to tell you its secrets. These techniques are the core of the "profiling" part of performance tuning—transforming raw data into actionable insights.

Key Takeaways:

  • Total Weight shows the cost of a function and its children; Self Weight shows the cost of the function itself. High Self Weight identifies a hotspot.
  • Use Invert Call Tree to answer "Who is calling this expensive function?"
  • Use Top Functions for a quick, flat list of the most expensive functions, and pair it with the Heaviest Stack Trace view for context.
  • Always start by using Hide System Libraries to focus on your own code, but remember its limitations.

In our next lesson, we will put these interpretation skills into practice. We'll dive deep into the Time Profiler instrument, using it to analyze CPU usage, detect unintended work on the main thread, and pinpoint the most expensive call paths in a real-world scenario.