Skip to content

macos: Draw a canvas at once unless it paints often - #444

Merged
huacnlee merged 2 commits into
longbridge:mainfrom
vicanso:macos-canvas-draws-asynchronously
Oct 25, 2026
Merged

huacnlee merged 2 commits into
longbridge:mainfrom
vicanso:macos-canvas-draws-asynchronously

Conversation

@vicanso

@vicanso vicanso commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Description

macOS drew every canvas asynchronously (drawsAsynchronously), which keeps the main thread free for canvases that
change every frame (the Stocks demo's main-thread time per frame went from ~3.3 to ~1.8 ms when it landed). Drawing
so holds graphics memory for the process (footprint's "Owned physical footprint (unmapped) (graphics)", ~50–60 MB
while any canvas draws asynchronously) and costs Metal work per layer. An app whose canvases change now and then — a
menu-bar panel with three small charts redrawn every two seconds — pays that and gains no frame time.

The first version of this PR let the app choose with a desktop={{ drawsAsynchronously: false }} prop. The platform
can tell on its own, so this version decides automatically and adds no API. It builds on #436 (base
linux-canvas-perf), which already draws canvases that keep their static part as a layer at once.

  • macOS (nsh-platform-macos canvas.rs Cadence, view.rs): a canvas draws at once on the main thread and
    moves to Core Animation's threads once it paints 10 times in a second (an animation, live data); a slower second,
    or a pause, brings it back. A canvas keeping its static part as a layer (core: Draw animated pages at the display's rate on every desktop and iOS #436) draws at once however often it paints.
  • Docs: docs/dev/{canvas,macos,performance}.md, the website's Performance report (English and zh-CN).

The prop of the first version (Prop::DrawsAsynchronously, ViewProps::draws_asynchronously,
CanvasView::draws_asynchronously, on::CANVAS, the compat case and the element docs) is gone again, so the
nsh-compat change it needed is no longer needed.

Public API

None.

Performance

Release builds, MacBook Pro (M3), 120 Hz ProMotion display, macOS 27.

Three 340×40 line charts redrawn every 2 s (the menu-bar case; footprint sampled every 2 s over 20 s, CPU time of the
process over ~40 s):

Footprint CPU time
Before: every canvas asynchronous ~83 MB ~0.27 s
After: drawn at once ~21 MB ~0.19 s

The gallery's tour (examples/gallery/tools/perf.py, every page idle and scrolled): frame time and CPU unchanged on
every page (Stocks stays asynchronous: 120 FPS, p95 ~3.3 ms, CPU 35–38%); peak memory of the pages with canvases drops:

Page Before After
Canvas, scrolling ~383 MB ~310 MB
Animations, scrolling ~290 MB ~232 MB
Gestures, scrolling ~279 MB ~221 MB

How to Test

  • cargo test -p nsh-platform-macos canvas: a_canvas_painting_often_draws_asynchronously (a chart redrawn every
    2 s stays at once, a 120 Hz animation moves from its tenth frame, a pause brings it back, a kept layer stays at once).
  • cargo clippy -p nsh-platform-macos -p nsh-macos-host -p nsh-macos-app --all-targets --features nsh-platform-macos/dev.
  • python3 examples/gallery/tools/perf.py --target macos for the gallery table; footprint <pid> of an app with a few
    canvases redrawn every few seconds for the first.

🤖 Generated with Claude Code

vicanso and others added 2 commits October 24, 2026 16:50
macOS draws every canvas asynchronously, which keeps the main thread
free for canvases that change every frame (the Stocks demo). Each
asynchronous drawing also holds GPU memory of its own while it is
drawn: about 97 MB on macOS 27 ("Owned physical footprint (unmapped)
(graphics)" in `footprint`), however little the canvas draws. An app
with a few small charts redrawn every couple of seconds pays that on
every redraw and gains no frame time.

`desktop={{ drawsAsynchronously: false }}` on a `<Canvas>`
(`.draws_asynchronously(false)` in Rust) draws it at once on the main
thread instead. The default is unchanged; the other platforms ignore
the prop.

Measured in a menu-bar app with three 340x40 charts redrawn every
2 s (debug build, Apple M4 Pro, macOS 27):

- Default: footprint 162 MB at rest, CPU 1.70%.
- `drawsAsynchronously: false`: footprint 43 MB at rest (48 MB peak
  while charts mount), CPU 1.60%.
Every canvas drew asynchronously, which holds graphics memory for the
process (~50-60 MB of "Owned physical footprint (unmapped) (graphics)")
and Metal work per layer: worth it for a canvas that changes every
frame, wasted on one redrawn every few seconds, such as a menu-bar
panel's charts. The previous commit let the app choose with a
desktop={{ drawsAsynchronously }} prop; the platform can tell on its
own, so the prop goes again and no API is added.

A canvas now draws at once on the main thread and moves to Core
Animation's threads once it paints 10 times in a second; a slower
second, or a pause, brings it back. A canvas that keeps its static part
as a layer draws at once however often it paints, as before.

Three 340x40 charts redrawn every 2 s: footprint ~83 -> ~21 MB, CPU
time over 40 s ~0.27 -> ~0.19 s. Gallery tour (perf.py): frame time and
CPU unchanged on every page; peak memory of the pages with canvases
drops (Canvas scrolling ~383 -> ~310 MB, Animations ~290 -> ~232).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@huacnlee
huacnlee force-pushed the macos-canvas-draws-asynchronously branch from 85c3445 to 4b4fc7a Compare October 24, 2026 13:16
@huacnlee huacnlee changed the title macos: Let an app draw a canvas at once with drawsAsynchronously macos: Draw a canvas at once unless it paints often Oct 24, 2026
@huacnlee
huacnlee changed the base branch from main to linux-canvas-perf October 24, 2026 13:17
@huacnlee
huacnlee deleted the branch longbridge:linux-canvas-perf October 24, 2026 16:47
@huacnlee huacnlee closed this Oct 24, 2026
@huacnlee huacnlee reopened this Oct 24, 2026
@huacnlee
huacnlee changed the base branch from linux-canvas-perf to main October 24, 2026 16:50
@huacnlee
huacnlee force-pushed the macos-canvas-draws-asynchronously branch from 4b4fc7a to d5c2ea7 Compare October 24, 2026 16:54
@huacnlee
huacnlee merged commit 6e1c6fd into longbridge:main Oct 25, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants