Profiling CPU and GPU

Play this lesson

The wave that stutters

When a big wave of pests reaches the garden, Shard Garden stutters on budget Android phones. Kai suspects the particles, Remy suspects the network. Neither has measured.

Profiling means recording what the game actually did, millisecond by millisecond, so a guess becomes a fact. Course 1 opened the Performance panel. This lesson reads it like a professional, and adds the GPU.

First, record an honest profile:

Reading the flame chart

The flame chart shows the main thread over the recording. Left to right is time. Top to bottom is the call stack: each bar called the bars below it.

Tasks over 50 ms are long tasks: they get a red triangle, with the part over 50 ms shaded red. In the Frames row, a dropped frame, one Chrome couldn't draw in time, is red with a dense line pattern.

Quick check

In the flame chart, time runs from the top of the chart to the bottom.

Show the answer

False

Time runs left to right. Top to bottom is the call stack: each bar was called by the bar above it, so the bottom bars are the functions doing the actual work.

Self time and total time

Every function has two times:

The Bottom-up tab sorts activities by the time they directly took, so the real worker floats to the top. The Call tree tab starts from the root activities and shows which ones caused the most work.

Read the numbers

Bottom-up, one frame, ms
Function        Self  TotalupdateGarden     0.4   11.2findPath         8.9    9.1drawPests        1.2    1.6

Which function should Kai optimise first?

  1. findPath
  2. updateGarden
  3. drawPests
Show the answer

findPath

findPath does 8.9 ms of work itself. updateGarden has a big total only because it calls findPath; its own code takes 0.4 ms.

Label your own code

Browser tools see function names, not game ideas. The User Timing API lets us add our own labels:

performance.mark('ai-start')
performance.mark('ai-end')
performance.measure('pest-ai', 'ai-start', 'ai-end')

The measure records the time between two marks, and browser tools can show it in the recording. Now "pest AI took 6 ms" sits right in the profile, and the same numbers can go into our field data.

When the GPU is the problem

Profile pick

Kai, #perf
Each frame takes 24 ms, but the CPU flame chart is only busy for 7 ms. Every pest has its own glow filter.

What should Kai try first?

  1. Replace the per-pest glow filters with a glow drawn into the sprite art
  2. Rewrite findPath
  3. Turn off CPU throttling
Show the answer

Replace the per-pest glow filters with a glow drawn into the sprite art

The CPU is idle most of the frame, so the time is going to the GPU. Filters are expensive, and one per pest multiplies the cost. Baking the glow into the art removes them.

Time is one budget, memory another

Profiles show time. A game can also be fast and still crash, because it runs out of memory.

Bolt says: Next: how the garbage collector works, and what a phone does when memory runs out.

Fact-checked 11 October 2026