Repository navigation
macos: Draw a canvas at once unless it paints often - #444
Merged
huacnlee merged 2 commits intoOct 25, 2026
Merged
Conversation
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
force-pushed
the
macos-canvas-draws-asynchronously
branch
from
October 24, 2026 13:16
85c3445 to
4b4fc7a
Compare
huacnlee
force-pushed
the
macos-canvas-draws-asynchronously
branch
from
October 24, 2026 16:54
4b4fc7a to
d5c2ea7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
macOS drew every canvas asynchronously (
drawsAsynchronously), which keeps the main thread free for canvases thatchange 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 MBwhile 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 platformcan 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.nsh-platform-macoscanvas.rsCadence,view.rs): a canvas draws at once on the main thread andmoves 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/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 thensh-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):
The gallery's tour (
examples/gallery/tools/perf.py, every page idle and scrolled): frame time and CPU unchanged onevery page (Stocks stays asynchronous: 120 FPS, p95 ~3.3 ms, CPU 35–38%); peak memory of the pages with canvases drops:
How to Test
cargo test -p nsh-platform-macos canvas:a_canvas_painting_often_draws_asynchronously(a chart redrawn every2 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 macosfor the gallery table;footprint <pid>of an app with a fewcanvases redrawn every few seconds for the first.
🤖 Generated with Claude Code