Skip to content

build: faster build (native wasm-opt, cached wasm-opt, parallel tsdown) and CI - #142

Merged
2fd merged 9 commits into
masterfrom
2fd/build-performance
Nov 1, 2026
Merged

2fd merged 9 commits into
masterfrom
2fd/build-performance

Conversation

@2fd

@2fd 2fd commented Nov 1, 2026 •

Copy link
Copy Markdown
Owner

Why

wasm-opt from the binaryen npm package is an emscripten (JS) build that runs on one thread. On a clean local build it took 8.3s out of ~17s. On a JS-only rebuild it took 8.3s out of ~9s. In CI, the Rust tests and a stale cargo cache were also on the critical path of the build job.

What

Build (bin/)

  • wasm-opt.sh uses a native wasm-opt when its version matches the binaryen npm package (version_132). It looks in PATH first, then in target/binaryen. If there is none, it uses the npm package. It skips node_modules/*, because pnpm run and node --run put node_modules/.bin first in PATH. WASM_OPT_NPM=1 forces the npm package.
  • wasm-opt.sh keeps the last output in target/wasm-opt. The cache key is the input, the binaryen version and the script itself, so a rebuild without Rust changes skips wasm-opt.
  • install-binaryen.sh <dir> downloads the matching binaryen GitHub release for the current OS and arch and checks its .sha256. install.sh (pnpm setup) uses it to install into target/binaryen, so local builds and CI get the native binary.
  • tsdown.config.ts no longer generates the .d.ts files of core (cjs), commonjs and cf. They export exactly what core exports, so build.sh copies core.d.mts to them. tsdown went from 6 to 3 declaration builds, and lib/ is identical byte for byte.
  • install.sh uses cargo binstall (prebuilt binaries) for wasm-bindgen-cli and wasm-pack when it is installed.
  • build.sh runs tsdown in parallel with wasm-opt, because tsdown only reads js/ and never bundles lib/rregex.wasm. It prints the time of each step and the total. When BUILD_TIMINGS=<file> is set, it also writes them as JSON.

CI

  • test:rs moved to its own job, test_rs, which runs in parallel with build. publish still needs it.
  • actions/cache is replaced by Swatinem/rust-cache@v2. The old cache key was only Cargo.lock, so a new Rust on the runner restored a stale target/, and an exact cache hit never saved a new one. The rust-cache key includes the rustc version. The release build job and the benchmark share shared-key: release.
  • target/binaryen is cached with actions/cache, keyed by the binaryen version, because rust-cache does not keep it.
  • check-regex-deps.sh moved to its own job, check_deps, which runs in parallel with build. publish still needs it.
  • The benchmark comment shows a Build step / head table (bench-compare.mjs --build-time).

Verification (local, macOS arm64, 14 cores)

before after
wasm-opt -O … 8.35s (npm, 112% CPU) 0.60s (native, ~700% CPU)
bin/build.sh, cargo warm 9.0s 1.03s
bin/build.sh, no Rust changes 9.0s 0.84s (wasm-opt reused)
output — all 14 files in lib/ identical (cmp)
  • The npm fallback path works, and so does WASM_OPT_NPM=1.
  • The wasm-opt cache works on a hit, on a miss, and when wasm-opt fails: a failure does not poison the cache.
  • tsdown gives identical output when lib/rregex.wasm does not exist yet.
  • install.sh installs binaryen once, verifies the sha256, and skips the download when binaryen is already there.

A first CI run had a checking wasm-opt step that ran the npm wasm-opt on the same input. The output was identical with cmp on Linux x86_64. I removed the step afterwards because it took ~15s on the build job.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

⏱️ Benchmark results

Comparing head e2e1ae7 against base rregex@1.13.4 (npm latest) on Node v26.13.0, Chrome 156 and Firefox 158 (5 rounds × 250 ms per case). Changes within ±10% are treated as noise.

Note

No performance regressions detected.

Benchmark Node v26.13.0
Δ time
Chrome 156
Δ time
Firefox 158
Δ time
email: new ➖ +0.2% ➖ -0.6% ➖ 0.0%
email: isMatch ➖ +0.3% ➖ -0.8% ➖ 0.0%
email: find ➖ -0.6% ➖ -0.2% ➖ -0.4%
email: findAll ➖ +0.5% ➖ -0.3% ➖ -1.1%
email: replaceAll ➖ -0.7% ➖ -1.6% ➖ 0.0%
ip: new ➖ -0.2% ➖ +0.2% ➖ -1.7%
ip: isMatch ➖ -0.2% ➖ +0.3% ➖ -0.8%
ip: find ➖ -0.5% ➖ -0.2% ➖ -0.4%
ip: findAll ➖ +0.4% ➖ +0.0% ➖ 0.0%
ip: replaceAll ➖ -0.0% ➖ +0.0% ➖ +6.8%
uri: new ➖ -0.2% ➖ +0.9% ➖ 0.0%
uri: isMatch ➖ -0.1% ➖ +0.1% ➖ -0.4%
uri: find ➖ -0.5% ➖ +0.2% ➖ 0.0%
uri: findAll ➖ +0.0% ➖ -0.4% ➖ -4.8%
uri: replaceAll ➖ +0.1% ➖ -1.2% ➖ +3.3%
set: new ➖ -0.0% ➖ +1.4% ➖ +0.4%
set: matches ➖ +0.1% ➖ +0.7% ➖ -3.2%
Node v26.13.0: times
Benchmark base head RegExp Δ time ±
➖ email: new 998.10 µs (1,002 ops/s) 1.00 ms (998 ops/s) — +0.2% 0.7%
➖ email: isMatch 136.74 µs (7,313 ops/s) 136.71 µs (7,315 ops/s) 360.34 µs (2,775 ops/s) +0.3% 0.3%
➖ email: find 138.64 µs (7,213 ops/s) 137.78 µs (7,258 ops/s) 359.96 µs (2,778 ops/s) -0.6% 0.5%
➖ email: findAll 251.30 µs (3,979 ops/s) 251.10 µs (3,982 ops/s) 6.67 ms (150 ops/s) +0.5% 0.7%
➖ email: replaceAll 535.87 µs (1,866 ops/s) 535.90 µs (1,866 ops/s) 6.65 ms (150 ops/s) -0.7% 1.1%
➖ ip: new 188.12 µs (5,316 ops/s) 187.88 µs (5,323 ops/s) — -0.2% 0.8%
➖ ip: isMatch 684.72 µs (1,460 ops/s) 685.50 µs (1,459 ops/s) 65.63 µs (15,236 ops/s) -0.2% 0.7%
➖ ip: find 714.07 µs (1,400 ops/s) 708.26 µs (1,412 ops/s) 65.81 µs (15,195 ops/s) -0.5% 2.2%
➖ ip: findAll 710.40 µs (1,408 ops/s) 712.05 µs (1,404 ops/s) 66.90 µs (14,947 ops/s) +0.4% 0.7%
➖ ip: replaceAll 1.04 ms (966 ops/s) 1.04 ms (960 ops/s) 65.72 µs (15,216 ops/s) -0.0% 2.5%
➖ uri: new 520.33 µs (1,922 ops/s) 523.06 µs (1,912 ops/s) — -0.2% 0.5%
➖ uri: isMatch 130.59 µs (7,657 ops/s) 130.23 µs (7,679 ops/s) 595 ns (1,679,673 ops/s) -0.1% 0.3%
➖ uri: find 134.61 µs (7,429 ops/s) 134.80 µs (7,418 ops/s) 620 ns (1,611,803 ops/s) -0.5% 0.6%
➖ uri: findAll 670.88 µs (1,491 ops/s) 670.58 µs (1,491 ops/s) 5.61 ms (178 ops/s) +0.0% 2.1%
➖ uri: replaceAll 791.40 µs (1,264 ops/s) 782.13 µs (1,279 ops/s) 5.59 ms (179 ops/s) +0.1% 0.8%
➖ set: new 1.30 ms (767 ops/s) 1.30 ms (767 ops/s) — -0.0% 0.5%
➖ set: matches 1.95 ms (512 ops/s) 1.96 ms (511 ops/s) 430.85 µs (2,321 ops/s) +0.1% 1.1%
Chrome 156: times
Benchmark base head RegExp Δ time ±
➖ email: new 1.01 ms (992 ops/s) 1.00 ms (997 ops/s) — -0.6% 4.6%
➖ email: isMatch 1.69 ms (590 ops/s) 1.68 ms (595 ops/s) 88.33 µs (11,321 ops/s) -0.8% 1.4%
➖ email: find 1.69 ms (592 ops/s) 1.68 ms (594 ops/s) 87.92 µs (11,374 ops/s) -0.2% 1.0%
➖ email: findAll 1.81 ms (554 ops/s) 1.80 ms (555 ops/s) 2.16 ms (463 ops/s) -0.3% 5.4%
➖ email: replaceAll 3.43 ms (291 ops/s) 3.40 ms (294 ops/s) 2.16 ms (464 ops/s) -1.6% 3.3%
➖ ip: new 184.16 µs (5,430 ops/s) 186.83 µs (5,352 ops/s) — +0.2% 4.4%
➖ ip: isMatch 2.25 ms (444 ops/s) 2.26 ms (443 ops/s) 66.48 µs (15,041 ops/s) +0.3% 1.6%
➖ ip: find 2.29 ms (437 ops/s) 2.30 ms (434 ops/s) 66.43 µs (15,053 ops/s) -0.2% 2.0%
➖ ip: findAll 2.29 ms (437 ops/s) 2.27 ms (441 ops/s) 68.02 µs (14,701 ops/s) +0.0% 4.3%
➖ ip: replaceAll 3.86 ms (259 ops/s) 3.93 ms (254 ops/s) 65.83 µs (15,190 ops/s) +0.0% 3.8%
➖ uri: new 525.62 µs (1,902 ops/s) 525.83 µs (1,902 ops/s) — +0.9% 4.4%
➖ uri: isMatch 1.69 ms (593 ops/s) 1.67 ms (599 ops/s) 141 ns (7,093,189 ops/s) +0.1% 5.8%
➖ uri: find 1.68 ms (596 ops/s) 1.68 ms (595 ops/s) 153 ns (6,548,361 ops/s) +0.2% 1.9%
➖ uri: findAll 2.35 ms (426 ops/s) 2.34 ms (428 ops/s) 2.03 ms (493 ops/s) -0.4% 1.0%
➖ uri: replaceAll 3.66 ms (273 ops/s) 3.60 ms (278 ops/s) 2.01 ms (497 ops/s) -1.2% 4.6%
➖ set: new 1.29 ms (772 ops/s) 1.32 ms (760 ops/s) — +1.4% 0.5%
➖ set: matches 3.65 ms (274 ops/s) 3.68 ms (272 ops/s) 172.21 µs (5,807 ops/s) +0.7% 1.9%
Firefox 158: times
Benchmark base head RegExp Δ time ±
➖ email: new 1.19 ms (841 ops/s) 1.19 ms (841 ops/s) — 0.0% 7.1%
➖ email: isMatch 191.31 µs (5,227 ops/s) 191.31 µs (5,227 ops/s) 89.84 µs (11,130 ops/s) 0.0% 7.4%
➖ email: find 193.60 µs (5,165 ops/s) 192.84 µs (5,186 ops/s) 86.62 µs (11,545 ops/s) -0.4% 6.0%
➖ email: findAll 307.60 µs (3,251 ops/s) 302.88 µs (3,302 ops/s) 2.16 ms (464 ops/s) -1.1% 4.0%
➖ email: replaceAll 907.14 µs (1,102 ops/s) 907.14 µs (1,102 ops/s) 2.14 ms (467 ops/s) 0.0% 5.9%
➖ ip: new 248.02 µs (4,032 ops/s) 247.02 µs (4,048 ops/s) — -1.7% 6.7%
➖ ip: isMatch 1.04 ms (965 ops/s) 1.03 ms (969 ops/s) 63.00 µs (15,872 ops/s) -0.8% 4.2%
➖ ip: find 1.04 ms (960 ops/s) 1.04 ms (964 ops/s) 63.76 µs (15,684 ops/s) -0.4% 3.9%
➖ ip: findAll 1.05 ms (956 ops/s) 1.04 ms (960 ops/s) 64.26 µs (15,561 ops/s) 0.0% 3.6%
➖ ip: replaceAll 1.67 ms (598 ops/s) 1.76 ms (567 ops/s) 63.51 µs (15,746 ops/s) +6.8% 23.6%
➖ uri: new 627.50 µs (1,594 ops/s) 627.50 µs (1,594 ops/s) — 0.0% 3.7%
➖ uri: isMatch 184.59 µs (5,417 ops/s) 185.27 µs (5,398 ops/s) 123 ns (8,126,464 ops/s) -0.4% 3.8%
➖ uri: find 190.55 µs (5,248 ops/s) 188.24 µs (5,312 ops/s) 135 ns (7,395,113 ops/s) 0.0% 4.1%
➖ uri: findAll 933.82 µs (1,071 ops/s) 889.29 µs (1,124 ops/s) 2.08 ms (480 ops/s) -4.8% 6.8%
➖ uri: replaceAll 1.26 ms (794 ops/s) 1.30 ms (768 ops/s) 2.07 ms (482 ops/s) +3.3% 23.0%
➖ set: new 1.53 ms (653 ops/s) 1.54 ms (651 ops/s) — +0.4% 3.9%
➖ set: matches 3.15 ms (317 ops/s) 3.09 ms (324 ops/s) 155.64 µs (6,425 ops/s) -3.2% 13.1%
Artifact base head Δ size
rregex.wasm 1194.8 KiB 1194.8 KiB 0.0%
Build step head
cargo build 2.72 s
wasm-bindgen 0.32 s
wasm-opt 2.94 s
tsdown 4.56 s
Total 7.61 s

Δ time is the median of the per-round head/base ratios (lower is better). RegExp is the time of the equivalent native RegExp call in the same runtime (new has no equivalent, RegExp compiles on first use). ± is the worst relative margin of error seen across rounds. The build time of cargo build depends on the state of the CI cache; wasm-opt and tsdown run in parallel, so the total is less than the sum. Shared CI runners are noisy: re-run the job before you act on a small regression.

Workflow run · updated for e2e1ae7

@2fd 2fd changed the title build: use a native wasm-opt and report the build time in the benchmark build: faster build (native wasm-opt, cached wasm-opt, parallel tsdown) and CI Nov 1, 2026
2fd and others added 6 commits November 1, 2026 01:52
The `binaryen` npm package is an emscripten (JS) build of wasm-opt and runs
on one thread: ~8.3s of a ~9s rebuild. bin/wasm-opt.sh now prefers a native
wasm-opt with the same version as the npm package from PATH (~0.6s, same
output byte for byte) and falls back to the npm package.

- bin/install-binaryen.sh downloads the matching binaryen release and
  checks its sha256; both workflows put it in PATH
- the release workflow checks that the native and the npm wasm-opt give the
  same wasm
- bin/build.sh prints the time of each step and writes them to
  $BUILD_TIMINGS; the benchmark report shows them

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The native and the npm wasm-opt gave the same wasm on Linux x86_64 in CI.
bin/wasm-opt.sh only uses a native wasm-opt with the same version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
bin/install.sh installs it in target/binaryen and bin/wasm-opt.sh looks for
it there after PATH, so local builds also use the native wasm-opt. The
workflows already run `setup`, so their own install step goes away.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
bin/wasm-opt.sh keeps the last output in target/wasm-opt, keyed by the
input, the binaryen version and the script itself. A rebuild that does not
change the Rust code skips wasm-opt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tsdown only reads js/ and never bundles lib/rregex.wasm, so both steps run
in parallel. Each step still reports its own time; the total is the wall
time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- `test:rs` builds the crate in the test profile before the build. It now
  runs in the `test_rs` job, next to the build; `publish` still needs it.
- The cargo cache key was only Cargo.lock: a new Rust on the runner
  restored a stale target/ and an exact hit never saved a new one.
  Swatinem/rust-cache adds the rustc version to the key and keeps only the
  dependencies. The release build and the benchmark share the key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@2fd
2fd force-pushed the 2fd/build-performance branch from e3baeac to a35e502 Compare November 1, 2026 04:53
2fd and others added 3 commits November 1, 2026 02:34
core (cjs), commonjs and cf export exactly what core exports, so tsdown
generated the same .d.ts four times. bin/build.sh now copies core.d.mts to
the other three; lib/ is byte for byte the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…their own job

- rust-cache does not keep target/binaryen, so `setup` downloaded it on
  every run; actions/cache keeps it, keyed by the binaryen version.
- bin/check-regex-deps.sh asks crates.io for publish dates before the build.
  It now runs in the `check_deps` job, next to the build; `publish` needs it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n available

`cargo install` compiles each one in ~1-2 min; cargo-binstall downloads the
prebuilt binary. Without it, bin/install.sh still uses `cargo install`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@2fd
2fd merged commit e2e1ae7 into master Nov 1, 2026
17 checks passed
@2fd
2fd deleted the 2fd/build-performance branch November 1, 2026 18:03
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