Repository navigation
Make Dedither 3–5× faster in a background worker, with repeatable results - #242
Merged
Merged
Conversation
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]>
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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
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 freshMapfor every pixel. Ties between equally common neighbor colors went toMath.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 intoUint32words, 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.tsplususeDeditherWorker) 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 hooktoBlobthere. 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
developand this branch, headless Chromium 148, Apple M6. Times run from the Apply click until the result is committed, as the median of 3 runs.tests/assets/3888x2916p.jpg), 1 passThe 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:tests/helpers/legacyDedither.ts) must match byte for byte when given the same tie function.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. Changingsame >= weighttosame > weightmakes this test fail.tests/e2e/dedither.spec.ts(@smoke):develop.npm run lint,npx tsc -b,npm test(1279 passed, 1 pre-existing skip) andnpm run test:e2e:all(all 8 suites, 260 tests) are green locally.dedithering-cleanup.mdand the dedither diagram label now describe deterministic ties, in all 12 languages, with diagram measurements refitted.Untested
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