Skip to content

Cut the renderer's cost per sprite move, costume switch and pen line - #11

Merged
Mistium merged 3 commits into
developfrom
faster-pen-lines
Nov 13, 2026
Merged

Mistium merged 3 commits into
developfrom
faster-pen-lines

Conversation

@Mistium

@Mistium Mistium commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Three per-call costs in the renderer that showed up on the real-project replays. With the same VM, this fork's renderer was 10% slower than TurboWarp's on Game of Life and 4.5% slower on Doom. None of the three changes what is drawn or what any query returns.

What changed

  • Costume switches. A skin kept the drawables using it in a Set, so every costume switch paid for a hash delete and a hash add. It is now an array, and each drawable remembers its slot in it.
  • Pen lines. Each line grew the dirty box with eight Math.min and Math.max calls. They are now comparisons. A line with a coordinate that is not a number marks the whole layer dirty, so the read back is never skipped.
  • Sprite moves. Fencing recomputed the drawable's transform and bounds on every move. Each drawable now remembers the box of positions that need no fencing, and a move inside it returns after four comparisons. The memory is dropped when the sprite changes shape, when the stage changes size and for positions beyond 10000. Moves near the edge take the exact path as before. A sprite whose bounds cannot be measured (infinite size, a trick 3D projects use) was never fenced and is now remembered as free everywhere.

Version 0.1.1 to 0.1.2.

Speed

Paired replays with hardware counters, three rounds, every state hash agreeing, same VM on both sides (scratch-vm scratchfoundation#55). Ratios are develop's renderer over this branch, so above 1 is faster.

Project CPU Instructions
Game of Life 1.117 1.124
Doom 1.057 1.073
3D renderer 1.056 1.072
3D Obj Importer 1.032 1.034
Bloons 1.020 1.022
Navier Stokes 1.014 1.031
Geomean of all 12 1.025 1.030

The other six replays are within noise (CPU 0.985 to 1.021). Against TurboWarp (its own VM and renderer) every replay is now at least 1.05x on CPU, with a geomean of 1.44x over all 12. Game of Life was the one replay still behind TurboWarp (0.92x at the same checkpoint) and is now 1.05x. The TurboWarp comparison was run on the commit before the last one, which only adds the guard for coordinates that are not numbers.

Checks

  • test/unit/FencingTests.js: fencing with and without the memory compared over 100,000 random moves, scales, rotations, skin changes and quality switches, plus 18,000 moves placed within a few millionths of the fence from random starting points. Results are identical. Further tests cover far-away and endless positions, unmeasurable bounds, stage resizes, skins with their own fence bounds, and that moves inside the stage skip the bounds. 18 mutants of the new fencing code are caught. One more, dropping the fixed slack, changes nothing because the rounding margin already covers it.
  • test/unit/PenDirtyRegionTests.js: the dirty box against the old Math.min and Math.max form for lines in every direction and 200 random lines, the partial read back, and the full read back for coordinates that are not numbers.
  • test/unit/SkinLifecycleTests.js: a skin tracks exactly the drawables that use it through switches and disposal.
  • An independent review fuzzed the fencing against the old algorithm for 10.8 million queries (scales from 1e-3 to 1e100, positions at the range limit, stage changes) with no mismatch.
  • npm run lint, npm run tap, npm run build and npm run check:conventions pass.
  • Scratch parity project: 2699 of 2731, the same failures as develop.

No visible surface, so no screenshots.

A skin kept the drawables using it in a Set, so every costume switch
paid for a hash delete and a hash add. It is now an array with each
drawable remembering its slot.

Pen lines grew the dirty box with eight Math.min and Math.max calls per
line. They are now plain comparisons.

Fencing recomputed the drawable's transform and bounds on every move.
The bounds are now remembered until the sprite changes shape, and a move
that stays clearly inside the stage returns without recomputing them.
Moves near the edge still take the exact path.
The remembered state is now the box of positions that are free of the
fence, so the check on a move is four comparisons. A sprite whose bounds
cannot be measured, such as one with an infinite size, is never fenced
and is remembered as free everywhere. The margin is sized for the 32-bit
model matrix, positions beyond 10000 drop the memory, and a change of
stage size is noticed.
@Mistium
Mistium merged commit 2caf7b8 into develop Nov 13, 2026
2 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.

1 participant