Skip to content

Make Dedither 3–5× faster in a background worker, with repeatable results - #242

Merged
vycdev merged 4 commits into
developfrom
t3/dedither-worker
Nov 29, 2026
Merged

vycdev merged 4 commits into
developfrom
t3/dedither-worker

Conversation

@vycdev

@vycdev vycdev commented Nov 29, 2026

Copy link
Copy Markdown
Owner

Phase 0 of the WebAssembly pilot in #68: a JS-only change, with no Rust or WASM yet.

Dedithering a 12 MP photo took over 5 s, and the work ran on the page itself, inline in DeditherPanel.tsx. Its loop built a fresh Map for every pixel. Ties between equally common neighbor colors went to Math.random(), so the same image and settings gave a different result each time, and nothing could pin the output in a test (or compare it against a future WASM kernel).

Now the kernel lives in React-free src/lib/dedither.ts. It packs pixels into Uint32 words, counts candidates in a fixed 8-slot table, and rewrites the image in place, using two saved rows instead of a new copy for every pass. A worker (src/workers/dedither.worker.ts plus useDeditherWorker) runs it in chunks of about 256K pixels and reports progress after each chunk. Ties are broken by a hash of the pixel's position and pass, so output is deterministic and doesn't depend on chunk size. Every pixel without a tie behaves exactly as before. Image decode and the PNG encode stay on the main thread, because the image-edit guard tests hook toBlob there. A dedither started before switching to 3D still finishes and commits, as the old loop did.

Before → after

These were measured in the real app: production builds of develop and this branch, headless Chromium 148, Apple M6. Times run from the Apply click until the result is committed, as the median of 3 runs.

Image Before After
Rocco, 1000×835, 1 pass 0.33 s 0.23 s
3888×2916 photo (tests/assets/3888x2916p.jpg), 1 pass 5.3 s 1.5 s
Same photo, 3 passes 12.8 s 2.6 s

The rest of the wait is decode and PNG encode, which both versions share. Holding one copy of the pixels instead of three also lowers peak memory on large images.

Verification

  • tests/dedither.test.ts:
    • 100 goldens (SHA-256 of the output) over the logo, Rocco, ordered-dither, 3-color noise and alpha fixtures, plus 1×1, 1×7, 7×1 and 2×2 images. They cover weights 1/4/8/9 and passes 1/3 (and 10 for small images).
    • A verbatim copy of the old loop (tests/helpers/legacyDedither.ts) must match byte for byte when given the same tie function.
    • With its original Math.random, the old loop must match on every pixel that isn't a tie (over 1,000 ties are exercised), and at each tie the new pick must be one of the tied colors. Changing same >= weight to same > weight makes this test fail.
    • Further tests cover chunk-size independence, pinned tie-hash vectors, the worker's progress messages, and buffer transfer.
  • tests/e2e/dedither.spec.ts (@smoke):
    • In the browser, Rocco is decoded to the exact same input pixels, and two applies produce the golden output. This test fails against develop.
    • A second test switches to 3D mid-run and checks that the result still lands. It failed before the fix in 5dd7f9c.
  • npm run lint, npx tsc -b, npm test (1279 passed, 1 pre-existing skip) and npm run test:e2e:all (all 8 suites, 260 tests) are green locally.
  • Docs: dedithering-cleanup.md and the dedither diagram label now describe deterministic ties, in all 12 languages, with diagram measurements refitted.

Untested

  • Firefox, WebKit, and the Tauri desktop app. The worker pattern matches the existing workers, which already run there.

Reviewed with sol-loop: 1 round with GPT-6.2 Sol (the user asked for one round). Sol requested changes. S1 (an unmount race when switching to 3D) is fixed, by restoring the old finish-and-commit behavior. S2 (drop the tie-distribution check) was declined: the check runs on fixed inputs, so it is deterministic, and it guards against a hash that favors one candidate.

Created with Claude Opus 5.5 in Claude Code; review by GPT-6.2 Sol.

🤖 Generated with Claude Code

vycdev and others added 4 commits November 29, 2026 10:42
Move the dedither kernel from DeditherPanel into React-free src/lib/dedither.ts.
It no longer builds a Map for every pixel and rewrites the image in place with two
saved rows. A worker runs it in row chunks and reports progress between chunks.
Ties between equally common neighbor colors are broken by a hash of the pixel
position and pass instead of Math.random, so identical inputs give identical output.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Switching to 3D unmounts the 2D panels. The old inline loop kept running and
committed its result; the worker hook cancelled it on unmount instead, so the
result was dropped or kept depending on timing. Stop cancelling on unmount.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vycdev
vycdev merged commit cde7bc3 into develop Nov 29, 2026
14 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