Files
zed/crates/ui
Parker Loflin d9089f4eac ui: Re-render spinner labels only as often as their glyph changes (#67261)
## Summary

`SpinnerLabel` and `LoadingLabel` request a frame on every display
refresh, though their text changes only 4–17.5 times a second. Each
frame re-renders every view above the label: in the agent panel,
`ThreadView`, `ConversationView`, `AgentPanel`, and `Workspace`. So
while a turn waits on the model, a confirmation, or a subagent, Zed
renders at the display's refresh rate, or at 30 fps when the window
doesn't have focus.

Both labels now render only when their text changes:

- `Animation::with_steps`, like CSS `steps()`, advances an animation in
equal steps and re-renders it when the next step starts. It counts steps
in integers on the animation's clock, so each step renders on time
however long Zed has run, and synced animations step, and render,
together.
- `SpinnerLabel` takes one step per glyph on the shared clock, so all
spinners render together. `LoadingLabel` takes four steps a second after
its reveal.

CPU time of the Zed process while a thread waits, in ms per second, main
→ this PR:

| State | 60 Hz | 120 Hz | 144 Hz | 240 Hz | Unfocused, 120 Hz |
|---|---|---|---|---|---|
| Waiting for the model | 168 → 48 | 272 → 48 | 319 → 48 | 417 → 49 | 92
→ 43 |
| Awaiting confirmation | 189 → 87 | 312 → 86 | 337 → 88 | 470 → 88 | 98
→ 83 |
| One subagent | 177 → 52 | 304 → 52 | 320 → 52 | 437 → 52 | 93 → 45 |
| Three subagents | 187 → 56 | 327 → 56 | 318 → 55 | 467 → 57 | 97 → 50
|
| Streaming a response | 281 → 266 | 370 → 333 | 379 → 324 | 543 → 335 |
147 → 155 |

While waiting, Zed draws 13 frames a second, 14 to 15 with subagents and
24 awaiting confirmation, instead of one per refresh, and GNOME Shell's
CPU time falls from 30–45 to 6–13 ms per second. Unfocused windows,
which GPUI already caps at 30 fps, draw 11–21. A hidden window draws
nothing either way.

Measured on a release build with a project and a Rust file open, on a
headless GNOME Shell 50 whose virtual monitor refreshes at each rate,
with an RTX 4080 and a Ryzen 7 9800X3D. A mock OpenAI-compatible
provider holds the thread in each state. CPU time is from `/proc`, all
threads, over 6-second windows; medians of 4 runs per variant,
alternating variants (3 unfocused, at 120 Hz). A run counts only if
everything else on the machine used under 180 ms/s of CPU, because other
work changes Zed's own CPU time.
[Harness](https://github.com/ptweezy/zed/tree/bench-spinner-real/bench/spinner_real)
and
[records](https://github.com/ptweezy/zed/tree/bench-spinner-real/bench/spinner_real/results/2026-11-22).

On `windows-latest` (4 vCPUs, no GPU, so Zed renders in software), the
same harness measured 845 → 188 ms/s while waiting for the model, 939 →
353 awaiting confirmation, 894 → 203 with three subagents, and 1,066 →
847 while streaming. `macos-latest` VMs stopped delivering frames
whenever Zed paused its display link, including for the cursor blink, so
they're left out.

## Testing

- `test_spinner_renders_only_as_often_as_its_glyph_changes` steps a
window with a `SpinnerLabel` at 100 Hz for one second and checks that it
renders 10 times, once per glyph change. It renders 100 times without
steps.
- `test_synced_steps_render_once_each` mounts a synced animation of
seven steps a second, 30 ms into a step and 300 days into the shared
clock, and checks that it renders once as each step starts. It doesn't
pass when steps are counted in floats or a step's length is rounded to
nanoseconds.
- `test_steps_finish_and_repeat_on_time` checks that a one-shot
animation of four steps ends on time, that its cap doesn't delay the
animation chained after it, and that a repeating one wraps.
- `test_sooner_step_replaces_the_pending_one` switches a repeating
animation from one step a second to ten and checks that the next step
renders on time, and once.
- All four pass with `SEED` 0–99.
- `cargo test -p gpui -p ui -p agent_ui -p which_key` passes.
- `./script/clippy -p gpui -p ui` passes.

## Self-review

- [x] I've reviewed my diff for quality, security, reliability, and
performance.
- [x] UI changes follow the
[checklist](https://zed.dev/docs/development/ui-checklist).
- [x] Tests cover the new or changed behavior.

Release Notes:

- Improved CPU usage while the agent waits on a model response, a tool
confirmation, or a subagent
- [GPUI] Added `Animation::with_steps`, which advances an animation in
equal steps and re-renders it only when it steps
2026-11-23 16:00:01 +00:00
..
…