Skip to content

Port wasm solver to C and add regression coverage#206

Open
Soccera1 wants to merge 9 commits intotiagozip:mainfrom
Soccera1:main
Open

Port wasm solver to C and add regression coverage#206
Soccera1 wants to merge 9 commits intotiagozip:mainfrom
Soccera1:main

Conversation

@Soccera1
Copy link
Copy Markdown

@Soccera1 Soccera1 commented Mar 26, 2026

What changed

  • Replaced the Rust WASM solver with a C implementation in wasm/src/c/src/cap_wasm.c, including SHA-256 hashing, hex-prefix target parsing, nonce formatting, and wasm allocator shims.
  • Removed the old Rust crate under wasm/src/rust and updated the docs/package metadata to describe the solver as C-based WASM.
  • Tightened the test suite: wasm/src/c/test-regression.cjs became a fixed regression suite, wasm/test/node.js now asserts expected nonces for the full challenge set, and wasm/test/node_odd_difficulty.js verifies the odd-length target case.
  • Updated wasm/build.js so the build runs the regression suite.

Commit breakdown

  • 629ed2f: Port wasm from Rust to C
  • 978c1f5: Remove Rust code
  • dee457e: Update tests

This was done as the rust implementation had many external dependencies, and rustc is very difficult to bootstrap. This version has no external dependencies; it can be compiled with -ffreestanding.
I have also done some unscientific testing and this is over 13% faster.

Summary by CodeRabbit

  • New Features

    • Added a full benchmark suite (Node + browser) with runner, browser UI, and an easy local server to compare solver implementations and report speedups.
  • Documentation

    • Updated docs and READMEs to describe the client solver as a C-based WebAssembly implementation.
  • Tests

    • Added regression tests validating solver correctness across multiple challenge cases.
  • Chores

    • Build pipeline updated to produce a C-based WASM solver and related build orchestration.

@Soccera1 Soccera1 marked this pull request as ready for review March 26, 2026 09:19
Copy link
Copy Markdown
Owner

@tiagozip tiagozip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks pretty good other than these three minor things! if you can move testing over to bun test and fix these i'll start benchmarking so it can be in prod

fs.rmdirSync(browserOutDir);
} catch {}
console.log(`Building C wasm...`);
execSync(`make -C "${cSrcDir}" clean`, { stdio: "inherit" });
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also a nitpick but this should have recursive: true.

- Convert wasm test scripts to `bun test`
- Rename test files to `.test.js`
- Keep node and browser solver regression coverage
@tiagozip
Copy link
Copy Markdown
Owner

do you have any benchmarks you can publish?

@tiagozip
Copy link
Copy Markdown
Owner

this seems to be slower than the current rust solver

image

@Soccera1
Copy link
Copy Markdown
Author

do you have any benchmarks you can publish?

I've pushed a benchmark. Here are my results.
2026-03-29-002920_853x134_scrot

@tiagozip
Copy link
Copy Markdown
Owner

is this on web or node/bun?

@Soccera1
Copy link
Copy Markdown
Author

Soccera1 commented Mar 28, 2026

is this on web or node/bun?

bun installed from npm

@Soccera1
Copy link
Copy Markdown
Author

On a Haswell CPU it's even more stark.

2026-03-29-004742_859x128_scrot

@tiagozip
Copy link
Copy Markdown
Owner

is this on web or node/bun?

bun installed from npm

do you have a web benchmark?

@Soccera1
Copy link
Copy Markdown
Author

I will create one

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 28, 2026

📝 Walkthrough

Walkthrough

Replaces the Rust-based WASM solver with a C-based WASM implementation, adds a Makefile-driven C build, introduces a benchmark suite (Node + browser) comparing C vs Rust baselines, updates build/test flows to run C artifacts, and adjusts docs and tests to reflect the C solver.

Changes

Cohort / File(s) Summary
Documentation
docs/guide/widget.md, docs/guide/workings.md, wasm/src/README.md
Text updated to identify the client-side solver as a C-based WASM solver instead of Rust.
C Solver Source & Build
wasm/src/c/src/cap_wasm.c, wasm/src/c/include/cap_wasm.h, wasm/src/c/Makefile
New C implementation: SHA‑256, POW solver, wasm-bindgen-compatible allocator/start, and Makefile targets to build and sync Node/browser .wasm outputs.
Build Pipeline
wasm/build.js
Replaced Rust/wasm-pack steps with make-based C build and updated test invocation patterns to use Bun tests (*.test.js).
Benchmark Suite (Node + Browser)
wasm/benchmark/README.md, wasm/benchmark/bench.js, wasm/benchmark/browser/bench.js, wasm/benchmark/browser/index.html, wasm/benchmark/build-artifacts.js, wasm/benchmark/serve-browser.js, wasm/benchmark/shared.js, wasm/benchmark/package.json, wasm/benchmark/rust/.gitignore, wasm/benchmark/...
Adds a comprehensive benchmark framework: artifact builders for head (C) and Rust baseline, Node runner, browser runner+UI, shared helpers, docs, and package scripts.
Tests & Regression
wasm/src/c/test-regression.test.js, wasm/test/node.test.js, wasm/test/node_odd_difficulty.test.js, wasm/test/node.js, wasm/src/package.json
New/renamed Bun-based tests exercising Node and browser wrappers; regression tests added for the C solver; removed/rewrote previous standalone script.
Rust Baseline Minor Edits
wasm/benchmark/rust/src/lib.rs
Whitespace/import reorder only (no semantic change).

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Builder as Build Artifacts
  participant Runner as Bench Runner (Node / Browser)
  participant WASM_head as C-based WASM (head)
  participant WASM_rust as Rust WASM (baseline)
  participant UI as Console / Browser UI

  Builder->>Builder: buildHead() (make, produce cap_wasm.wasm + loaders)
  Builder->>Builder: buildRust() (checkout baseline, build rust wasm + loaders)
  Runner->>WASM_head: init() / load solve_pow
  Runner->>WASM_rust: init() / load solve_pow
  Runner->>WASM_head: verifySolver(challengeCases)
  WASM_head-->>Runner: verification result
  Runner->>WASM_rust: verifySolver(challengeCases)
  WASM_rust-->>Runner: verification result
  Runner->>WASM_head: benchmarkSolver(iterations,warmup)
  WASM_head-->>Runner: timings + checksum
  Runner->>WASM_rust: benchmarkSolver(iterations,warmup)
  WASM_rust-->>Runner: timings + checksum
  Runner->>UI: compare checksums, compute speedup, print results
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Poem

🐰 a little hop of delight

I chewed some bytes and built a crate,
From Rust to C I changed my gait.
Benchmarks hum, the numbers sing—
Wasm wings flap, the results take wing.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 2.22% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the main change: porting the WASM solver from Rust to C and adding regression test coverage. It is clear, specific, and concise.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@Soccera1
Copy link
Copy Markdown
Author

is this on web or node/bun?

bun installed from npm

do you have a web benchmark?

Results are consistent with yours in the web benchmark I created. Not sure why. Maybe the web benchmark is not using AVX2 for my implementation whereas it is for the old one?

@Soccera1
Copy link
Copy Markdown
Author

I've found the issue. With node it's also slower.
However, it's faster with bun.

coderabbitai[bot]

This comment was marked as spam.

coderabbitai[bot]

This comment was marked as spam.

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.

2 participants