Skip to content

Rendering: sprites take about 45% less CPU time to update and draw - #1916

Merged
obiot merged 1 commit into
masterfrom
perf-sprite-cpu
Nov 24, 2026
Merged

obiot merged 1 commit into
masterfrom
perf-sprite-cpu

Conversation

@obiot

@obiot obiot commented Nov 24, 2026

Copy link
Copy Markdown
Member

Description

When the CPU is the bottleneck, most of what a sprite cost each frame was bookkeeping around it, not the draw itself. A profile at 10,000 sprites put most of it in Renderable.pos and anchorPoint going through a Proxy, so every write ran updateBounds through it. Next came the matrix work for each quad's corners, the renderer's save/restore copies, an effect-chain array built for every renderable, and a closure made for every child in Container.update.

Five changes, none of them public:

  • Positions without a Proxy. ObservableVector2d, ObservableVector3d and ObservablePoint use plain setters that fire the same callback. revoke() swaps in a revoked proxy, so a use after revoke() still throws. A write to pos.x costs about 2 ns instead of 34.
  • Quad corners. For an affine matrix, transformQuadCorners and Bounds.addFrame skip the full apply and compute each corner with the same arithmetic, in the same order. The corners are bit-identical, which a new spec checks against apply.
  • save/restore copies. Color.copy and Matrix3d.copy copy element by element. TypedArray#set cost about 6 ns per call. Matrix3d.copy keeps set for a source that is not 16 entries long.
  • No empty effect chains. A renderable without postEffects gets one shared, frozen empty chain, and Canvas's beginPostEffect returns early. Before, two arrays were built for every draw.
  • Camera check. Container.update asks the stage's cameras in a plain loop, with no closure per child. It stops at the first camera that sees the child; with one camera nothing changes.

10,000 sprites, headless Chromium, 600 frames, 4 runs each, ranges:

update, master → branch draw, master → branch
WebGL, moving 4.8–5.1 → 2.3 ms 3.6–3.7 → 2.5–2.6 ms
WebGL, rotating 9.9–10.6 → 3.4–3.5 ms 4.3–4.9 → 2.7 ms
WebGL, in 100 containers 6.1–6.4 → 3.2 ms 3.6–3.9 → 2.5–2.7 ms
WebGPU, moving 4.2–5.4 → 1.6–1.7 ms 4.9–5.2 → 3.7–3.8 ms
WebGPU, in 100 containers 6.3–6.6 → 3.3–3.4 ms 5.0–5.4 → 3.8–3.9 ms
Canvas, moving 4.7–5.7 → 1.9 ms 19.0–19.9 → 18.1–18.5 ms

Canvas drawing is mostly the browser's own work, so only its update gets faster. Frames are byte-identical to master on WebGL, WebGPU and Canvas, for moving, rotating, nested and faded sprites.

One more change was measured and dropped: reading width, height and the anchor once in updateBounds. It saved a few nanoseconds per call, and its frame-time effect came from V8's inlining, not from the work it saved.

Type of change

  • Bug fix
  • New feature
  • Documentation update
  • Performance improvement
  • Refactoring (no functional changes)

Checklist

  • I have read the Contributing Guide
  • My code follows the existing code style (pnpm lint passes)
  • I have tested my changes locally (pnpm test passes)
  • I have added tests that cover my changes (if applicable)
  • The build succeeds (pnpm build)

The new specs were written by breaking each change on purpose and checking which mistakes the suite missed:

  • affine-corners-exact: corners and bounds equal apply exactly.
  • renderable-position-updates: moves inside an offset container, pos.x += every frame, set, setPosition, setMuted and anchorPoint, counting bounds updates.
  • renderer-state-between-sprites: a tinted, faded, 3D-turned sprite between two probes, inside a faded group.
  • container-camera-visibility: a main camera and a radar camera, a walker crossing both, a floating HUD.

There are also new cases in renderable.spec.js (a child drops its parent's shader), camera-minimap-coords.spec.js (a minimap clips to its corner) and mat3d.spec.ts (a Matrix2d into Matrix3d.copy).

Full suite on this branch: 543 files, 12,549 tests pass.

Related issues

None.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SeWCkETjW35ZbXDRswV1hx

When the CPU is the limit, most of a sprite's cost was bookkeeping around
it rather than the draw itself. Five changes, none of them public:

- Renderable positions (ObservableVector2d/3d, ObservablePoint) no longer
  go through a Proxy: plain setters fire the same callback. revoke() swaps
  in a revoked proxy, so a use after revoke still throws.
- transformQuadCorners and Bounds.addFrame skip the full matrix apply for
  an affine matrix, with the same arithmetic in the same order, so the
  corners are bit-identical.
- Color.copy and Matrix3d.copy copy element by element, which makes the
  renderer's save/restore cheaper.
- A renderable with no post effects gets a shared empty chain, and Canvas
  returns early from beginPostEffect: no array built twice per draw.
- Container.update asks the cameras in a plain loop, with no closure per
  child, and stops at the first camera that sees it.

10,000 sprites, Chromium, per frame (update + draw): WebGL 8.6 -> 4.9 ms,
rotating 14.5 -> 6.1 ms, 100 containers 10.0 -> 5.8 ms; WebGPU 9.7 ->
5.4 ms. Frames are byte-identical to master on WebGL, WebGPU and Canvas.

New specs: affine-corners-exact, renderable-position-updates,
renderer-state-between-sprites, container-camera-visibility, plus cases
in renderable, camera-minimap-coords and mat3d.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SeWCkETjW35ZbXDRswV1hx
Copilot AI balanced review requested due to automatic review settings November 24, 2026 23:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@obiot
obiot merged commit 0ee5de9 into master Nov 24, 2026
6 checks passed
@obiot
obiot deleted the perf-sprite-cpu branch November 24, 2026 23:33
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