Skip to content

Add an experimental WebAssembly Dedither with identical output (#68) - #245

Merged
vycdev merged 4 commits into
developfrom
t3/wasm-dedither
Nov 30, 2026
Merged

vycdev merged 4 commits into
developfrom
t3/wasm-dedither

Conversation

@vycdev

@vycdev vycdev commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

Step 1 of the WebAssembly pilot in #68. Dedither is the first kernel to get a Rust/WebAssembly version, behind an experimental switch. #242 already made the JavaScript version deterministic and moved it to a worker, so the WebAssembly output can be checked byte for byte against the same goldens.

What changes for users: a new WebAssembly acceleration switch under Experimental in Settings, off by default. When it is on, Dedither runs the Rust kernel. The output is identical, so the switch never changes results. If the WebAssembly file can't load or run, Dedither uses JavaScript, still finishes, and logs one console warning. Nothing else changes.

How it's built

  • Crate: wasm/ is a separate cargo workspace, not shared with src-tauri. kromacut-wasm/src/dedither.rs is a plain-Rust port of src/lib/dedither.ts (same scan order, tie hash, and in-place two-row algorithm), with native cargo tests. lib.rs is only the wasm-bindgen wrapper (wasm-bindgen = "=0.2.130").
  • Build script: scripts/build-wasm.mjs (npm run build:wasm) runs plain cargo build --release --target wasm32-unknown-unknown and wasm-bindgen --target web into src/wasm/pkg/, which is gitignored. No wasm-pack. It refuses a wasm-bindgen-cli whose version differs from the crate's in wasm/Cargo.lock.
  • Build policy: npm run build now runs it and fails without the toolchain, unless KROMACUT_SKIP_WASM=1 is set. That produces a JS-only build: the loader finds no kernels and the worker uses JavaScript. npm run dev works either way.
  • Worker:
    • The job logic moved into a testable src/lib/deditherJob.ts, with the WebAssembly run adapter in src/lib/deditherWasm.ts.
    • The adapter copies the pixels into WebAssembly memory once, steps in the same 256K-pixel row chunks (which also lets V8 tier up between calls), and copies the result back only when the run finishes. So a failure mid-run can redo the job in JavaScript from the untouched input.
    • Vite workers build as IIFE, which can't code-split. So the 7 KB glue is bundled into the worker, and only the .wasm file is fetched lazily.
  • CI: a composite action, .github/actions/setup-wasm, installs Rust 1.100.0 with wasm32-unknown-unknown, wasm-bindgen-cli 0.2.130 (taiki-e/install-action), and a shared cargo cache that only the ubuntu-24.04 checks and e2e jobs use. deploy-pages and release use the action with cache: 'false', building from scratch as they did before. In release it runs before Rust stable, so the desktop app keeps building on stable. The build script runs cargo +$KROMACUT_WASM_TOOLCHAIN --target-dir wasm/target, so the pin applies only to the WebAssembly build. checks also runs the crate's native tests. The rust_tests matrix (Windows, macOS, Linux) now builds the kernels too, so a release-only platform breakage shows up on PRs.

Before → after

All runs used production builds in headless Chromium 148 on an Apple M6. Times go from the Dedither Apply click until the result is committed; each value is the median of 5 runs.

Image develop (JS) This PR, switch off This PR, switch on (WASM)
Rocco 1000×835, 1 pass 0.18 s 0.19 s 0.17 s
3888×2916 photo, 1 pass 1.58 s 1.60 s 1.50 s
3888×2916 photo, 3 passes 3.04 s 3.09 s 2.30 s
  • WebKit 26.4 (Playwright), median of 3, switch off → on:

    • Rocco: 0.14 s → 0.14 s
    • Photo, 1 pass: 1.35 s → 1.20 s
    • Photo, 3 passes: 2.00 s → 1.89 s
  • The dedither step alone, in Node/V8: a fresh instance each time, including compile, instantiate and both copies.

    • Rocco: 34 → 18 ms
    • Photo, 1 pass: 698 → 302 ms
    • Photo, 3 passes: 2147 → 1084 ms

    That's about 2×. End to end the gain is smaller, because image decoding and PNG encoding are shared by both versions.

Bundle size

develop This PR
kromacut_wasm_bg.wasm (fetched only when the switch is on and a dedither runs) none 27.2 KB (11.1 KB gzip)
dedither.worker.js 2.0 KB (1.1 KB gzip) 6.8 KB (2.8 KB gzip)
Main index-*.js chunks +225 B

Verification

  • tests/deditherWasm.test.ts:
    • The WebAssembly kernel matches all 100 Dedither goldens byte for byte, and JavaScript on 400 random tie-heavy images with random chunk sizes.
    • It reports the same rows and leaves the input untouched until done.
    • A job gives the golden with the same progress messages.
    • A job falls back to JavaScript, still producing the golden, when the kernels are missing, fail to load, or trap mid-run. Progress never repeats.
    • Changing >= to > in the Rust keep rule makes 3 of these tests fail.
    • The test skips locally when src/wasm/pkg isn't built, and fails under CI.
  • Rust: cargo test (6 tests), cargo clippy -D warnings and cargo fmt --check are clean in wasm/.
  • tests/e2e/dedither.spec.ts: a @smoke test turns the switch on in Settings and checks that the .wasm is requested only once a dedither runs and that the output is the golden with no fallback warning. Another serves the .wasm as a 404 and checks the golden output and a warning naming the 404. A third never answers the .wasm request: the loader gives up after 10 s and the job finishes in JavaScript with the golden output. That test fails if the deadline is removed.
  • Local checks, all green:
    • npm run lint, npx tsc -b, check:i18n and test:docs.
    • npm test: 1286 passed, 1 pre-existing skip.
    • npm run test:e2e:all on port 47391 at 29daabc: all 8 suites passed (263 tests), with no port reported as already in use. The last commit only tightens one assertion in dedither.spec.ts, which passes (5/5).
    • landing.spec.ts now expects Shift+Tab from Close to wrap to the new switch, which is the dialog's last control.
  • Desktop: cargo test --locked in src-tauri passes (42 tests), and npx tauri build --no-bundle builds the release desktop binary with the .wasm embedded.
  • Build paths: KROMACUT_SKIP_WASM=1 npm run build succeeds with no .wasm in dist. Without wasm-bindgen-cli, npm run build:wasm fails with an install hint.
  • Docs: the settings guide has a new Experimental WebAssembly Acceleration section in all 12 languages, and the README lists the new build prerequisites.

Untested

  • Firefox (it doesn't launch in this sandbox), real Safari, and the desktop app at runtime. The Tauri CSP already allows 'wasm-unsafe-eval', and the build embeds the .wasm.
  • release.yml and deploy-pages.yml only run on release, so they haven't run. Every step they use does run on this PR: the same composite action, the same build script on Windows, macOS and Linux (rust_tests), and npm run build (checks).
  • With the switch on, peak memory in the worker at the 16384² import limit is about 2 GiB instead of 1 GiB. If WebAssembly memory can't grow, Dedither falls back to JavaScript. This is a follow-up to measure.

Council review

Rounds: 2. Gemini was unavailable (every Gemini-capable provider is disabled here), so the council had two members.

Delegate Round 1 Round 2
Claude Opus 5.5 6/10 9/10
GPT-6.2 Sol 8/10 9/10

Round 2 verdict: both approve.

Fixed in round 1:

  • build-wasm.mjs could not read the wasm-bindgen version from a CRLF Cargo.lock, as Windows checkouts have it. Both Windows release builds would have failed (blocker).
  • --target-dir is now explicit, so a CARGO_TARGET_DIR override can't package a stale binary.
  • PRs now build the kernels on Windows and macOS.
  • Release, Pages and rust_tests no longer restore the ubuntu-24.04 cache onto other images.
  • A stalled .wasm download now times out after 10 s and falls back to JavaScript instead of holding the edit open.
  • The fallback tests now check the reason in the warning.
  • Vite's dev watcher ignores wasm/target.

Declined, as follow-ups:

  • About 2 GiB in the worker at 16384² on the WASM path. The switch is opt-in, and memory-growth failure falls back cleanly.
  • The MAX_U32 clamps are harmless.
  • With localStorage blocked, the switch can show on while jobs run JavaScript. The output is identical, and Multi-plate already behaved this way before this PR.
  • A test that stalls the body after the headers arrive. Playwright's route.fulfill can't hold a body open, and Sol verified both stall paths reject in Chromium.

Created with Claude Opus 5.5 in Claude Code; council reviews by Claude Opus 5.5 and GPT-6.2 Sol.

🤖 Generated with Claude Code

vycdev and others added 4 commits November 30, 2026 12:32
Port the dedither kernel to Rust in a separate wasm/ crate, built with plain
cargo and wasm-bindgen-cli (npm run build:wasm, part of npm run build unless
KROMACUT_SKIP_WASM=1). Its output matches the JavaScript kernel byte for byte.
A new experimental switch in Settings, off by default, makes the dedither
worker try WebAssembly first; it falls back to JavaScript if the kernels are
missing or fail to load or run. CI builds the kernels with a pinned toolchain.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… download

- build-wasm.mjs reads the wasm-bindgen version from a CRLF Cargo.lock, as
  Windows checkouts have it, and passes an explicit --target-dir so a
  CARGO_TARGET_DIR override cannot package a stale kernel.
- CI builds the kernels on Windows, macOS and Linux in rust_tests, as release
  does. Release, Pages and that job build without the shared cache, which the
  ubuntu-24.04 checks job saves.
- The loader gives up on a .wasm download after 10 s, so a stalled fetch falls
  back to JavaScript instead of holding the edit open.
- Vite's dev watcher ignores wasm/target.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vycdev
vycdev merged commit 1075a65 into develop Nov 30, 2026
14 checks passed
vycdev added a commit that referenced this pull request Nov 30, 2026
- Treat the stored-session read as pending until it answers, so a read
  slower than startup's 4 s wait never shows the drop zone over an image
  about to be restored (smoke test holds the read past startup).
- Try a sample waits for a file that is still being checked.
- Move focus to Choose image after Remove image, and to Choose file
  after Try a sample, instead of losing it to the page.
- Only file drags light up the drop zone, and moving over the card's
  children keeps the highlight.
- Keep the card reachable and its buttons wrapping in short or narrow
  panes.
- Docs: qualify recovery, say what happens to the sample and that Remove
  image shows the drop zone, in all 12 languages; zh-CN/ja spacing.
- Load the sample in the WebAssembly dedither specs from #245.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
vycdev added a commit that referenced this pull request Nov 30, 2026
- Treat the stored-session read as pending until it answers, so a read
  slower than startup's 4 s wait never shows the drop zone over an image
  about to be restored (smoke test holds the read past startup).
- Try a sample waits for a file that is still being checked.
- Move focus to Choose image after Remove image, and to Choose file
  after Try a sample, instead of losing it to the page.
- Only file drags light up the drop zone, and moving over the card's
  children keeps the highlight.
- Keep the card reachable and its buttons wrapping in short or narrow
  panes.
- Docs: qualify recovery, say what happens to the sample and that Remove
  image shows the drop zone, in all 12 languages; zh-CN/ja spacing.
- Load the sample in the WebAssembly dedither specs from #245.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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