Skip to content

Speed up CPU touching and colour queries - #10

Merged
Mistium merged 1 commit into
developfrom
faster-touching
Nov 12, 2026
Merged

Mistium merged 1 commit into
developfrom
faster-touching

Conversation

@Mistium

@Mistium Mistium commented Nov 11, 2026

Copy link
Copy Markdown
Member

Touching sprite and touching colour are the renderer's share of script time in sprite-heavy projects. This makes both several times cheaper without changing a single result.

What changed

  • Inlined touching loops. isTouchingDrawables and isTouchingColor used to make a generic call per pixel per drawable: inverse matrix lookup, effect check, then skin and silhouette dispatch. Drawables with a standard skin and no shape-changing effect now go through Drawable.isTouchingPair, Drawable.isTouchingPlain and Drawable.samplePlainColor4b, which read silhouette data directly. The arithmetic is the same as getLocalPosition, including the float32 rounding and the 1e-6 snap. Custom skins, custom silhouettes and fisheye, whirl, pixelate and mosaic keep the generic path. Ghost, colour and brightness do not move pixels, so they stay on the fast path.
  • Cheaper candidate collection. Each drawable caches its integer-snapped bounds until its transform or hull changes. Non-overlapping drawables are rejected before the skin checks, and candidate records are pooled instead of allocated per overlap. The pool drops its references when a drawable or skin is destroyed.
  • No forced silhouette readback for smaller SVG MIPs. Smaller MIPs are rasterised on a shared scratch canvas, so the skin's own canvas, which the silhouette reads lazily, is left alone.

No public RenderWebGL method changed name or signature. Shader sources are untouched.

Results

Script time on the scratch-bench real-project replays (Chromium on SwiftShader, frame by frame against TurboWarp, same MistWarp VM before and after):

Project Before After
Flow field 1.0x TurboWarp 2.2x to 2.3x
Goopy 1.0x 1.8x to 2.0x
Other ten projects 1.0x unchanged within noise
Geometric mean of all twelve 1.02x 1.11x

Every state checkpoint of all twelve replays still hashes the same as TurboWarp.

How it was checked

  • Call for call against the old code inside the replays: 37,524 touching sprite calls in Flow field and 3,317,074 touching colour calls in Goopy, plus Bloons and BBC, with zero differences in results or candidate lists.
  • A random scene built in the old and the new build side by side (SVG, bitmap, pen and text skins, every effect, scales, rotations, hiding, layer changes, deleting drawables): about 200,000 isTouchingDrawables, isTouchingColor (with and without a mask) and drawableTouching queries across Chromium, Firefox and WebKit, zero differences.
  • The scratch-parity project (2,656 tests) gives byte-identical output with the old and the new renderer.
  • New unit tests in test/unit/TouchingFastPathTests.js compare each fast path with the generic one, including the fallbacks.
  • npm run lint, the conventions check, npm run build and npm run test pass.

Version 0.1.0 to 0.1.1.

Nothing visible changes, so there are no screenshots.

Touching sprite and touching colour spent most of their time in generic
per-point calls: a matrix lookup, an effect check and three levels of
method dispatch for every pixel. Drawables with a standard skin and no
shape-changing effect now go through inlined loops that read silhouette
data directly. The arithmetic is the same as getLocalPosition, including
float32 rounding, so results are bit-for-bit identical. Custom skins,
custom silhouettes and fisheye, whirl, pixelate and mosaic keep the
generic path.

Candidate collection caches each drawable's integer bounds until its
transform or hull changes, rejects non-overlapping drawables before the
skin checks, and reuses candidate records instead of allocating one per
overlap.

Smaller SVG MIPs are rasterised on a shared scratch canvas, so creating
one no longer forces the silhouette to read back the skin's own canvas.

Version 0.1.0 to 0.1.1.
@Mistium
Mistium merged commit 79c33f8 into develop Nov 12, 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