From 53b556998e67b57529a7764ce4a6ef2de5baf8dd Mon Sep 17 00:00:00 2001 From: frami Date: Sun, 1 Nov 2026 01:07:51 -0300 Subject: [PATCH 1/9] build: use a native wasm-opt and report the build time in the benchmark 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 --- .github/workflows/benchmark.yml | 12 +++++-- .github/workflows/release.yml | 12 +++++++ bin/bench-compare.mjs | 20 ++++++++++-- bin/build.sh | 56 ++++++++++++++++++++++++++++----- bin/install-binaryen.sh | 40 +++++++++++++++++++++++ bin/wasm-opt.sh | 50 +++++++++++++++++++++++++++++ 6 files changed, 177 insertions(+), 13 deletions(-) create mode 100755 bin/install-binaryen.sh create mode 100755 bin/wasm-opt.sh diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index 3e9db00..8f2944b 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -16,6 +16,8 @@ on: - "package.json" - "pnpm-lock.yaml" - "bin/build.sh" + - "bin/wasm-opt.sh" + - "bin/install-binaryen.sh" - "js/**" - "tsdown.config.ts" - "bin/bench-*.mjs" @@ -89,10 +91,15 @@ jobs: pnpm install --frozen-lockfile node --run setup - # check that the lib is complete + # native wasm-opt, see the release workflow + - name: installing binaryen + run: ./bin/install-binaryen.sh "$RUNNER_TEMP/binaryen" >> "$GITHUB_PATH" + + # check that the lib is complete; the build time goes to the report - name: building head run: | - node --run build + mkdir -p "$RUNNER_TEMP/benchmark" + BUILD_TIMINGS="$RUNNER_TEMP/benchmark/build.json" node --run build test -f lib/esm.mjs -a -f lib/rregex.wasm # Chrome and Firefox come with the runner image; the runtimes run one @@ -116,6 +123,7 @@ jobs: --head-label "\`${HEAD_SHA::7}\`" \ --runtime "$REPORT_DIR/chrome.json" \ --runtime "$REPORT_DIR/firefox.json" \ + --build-time "$REPORT_DIR/build.json" \ --markdown "$REPORT_DIR/report.md" \ --json "$REPORT_DIR/results.json" cat "$REPORT_DIR/report.md" >> "$GITHUB_STEP_SUMMARY" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 06fa6d7..0796bc1 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -83,6 +83,11 @@ jobs: - name: installing run: pnpm install --frozen-lockfile + # native wasm-opt with the version of the binaryen npm package, used by + # bin/wasm-opt.sh: same output, ~10x faster than the npm (JS) build + - name: installing binaryen + run: ./bin/install-binaryen.sh "$RUNNER_TEMP/binaryen" >> "$GITHUB_PATH" + - name: setup run: node --run setup @@ -97,6 +102,13 @@ jobs: name: lib path: lib/ + # the native wasm-opt must give the same wasm as the npm package + - name: checking wasm-opt + run: | + wasm-opt --version + WASM_OPT_NPM=1 ./bin/wasm-opt.sh js/wasm/rregex_bg.wasm target/rregex.npm.wasm + cmp target/rregex.npm.wasm lib/rregex.wasm + test_node: runs-on: ubuntu-24.04 diff --git a/bin/bench-compare.mjs b/bin/bench-compare.mjs index 686e334..334ed27 100644 --- a/bin/bench-compare.mjs +++ b/bin/bench-compare.mjs @@ -6,12 +6,13 @@ // Usage: // node bin/bench-compare.mjs --base --head \ // [--base-label ] [--head-label ] \ -// [--runtime ]... \ +// [--runtime ]... [--build-time ] \ // [--markdown ] [--json ] \ // [--rounds 5] [--time 250] [--threshold 10] // // The cases run in Node. Each --runtime adds the results of another runtime, -// written by bin/bench-browser.mjs, to the report. +// written by bin/bench-browser.mjs, to the report. --build-time adds the build +// time of head, written by bin/build.sh (BUILD_TIMINGS=). // // To reduce noise, base and head run next to each other for every case, the // order alternates on each round, and the reported delta is the median of the @@ -72,6 +73,7 @@ const { values: args } = parseArgs({ 'base-label': { type: 'string', default: 'base' }, 'head-label': { type: 'string', default: 'head' }, runtime: { type: 'string', multiple: true, default: [] }, + 'build-time': { type: 'string' }, markdown: { type: 'string' }, json: { type: 'string' }, rounds: { type: 'string', default: process.env.BENCH_ROUNDS ?? '5' }, @@ -96,6 +98,8 @@ const size = { }; size.delta = size.head / size.base - 1; +const build = args['build-time'] ? JSON.parse(await readFile(args['build-time'], 'utf8')) : null; + const names = [...cases(await importLib(dirs.head), data).keys()]; /** Runs one case for both builds in a new worker thread. */ @@ -206,16 +210,26 @@ lines.push('| Artifact | base | head | Δ size |'); lines.push('|:--|--:|--:|--:|'); lines.push(`| \`rregex.wasm\` | ${fmtBytes(size.base)} | ${fmtBytes(size.head)} | ${fmtDelta(size.delta)} |`); lines.push(''); + +// build time of head (base is the published package, it is not built) +if (build) { + lines.push('| Build step | head |', '|:--|--:|'); + const seconds = (ms) => `${(ms / 1000).toFixed(2)} s`; + for (const { name, ms } of build.steps) lines.push(`| \`${name}\` | ${seconds(ms)} |`); + lines.push(`| **Total** | **${seconds(build.total)}** |`, ''); +} + lines.push( 'Δ 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. ' + + (build ? 'The build time of `cargo build` depends on the state of the CI cache. ' : '') + 'Shared CI runners are noisy: re-run the job before you act on a small regression.', ); const markdown = lines.join('\n') + '\n'; -const json = { rounds: ROUNDS, time: TIME, threshold: THRESHOLD, size, runtimes }; +const json = { rounds: ROUNDS, time: TIME, threshold: THRESHOLD, size, build, runtimes }; if (args.markdown) await writeFile(args.markdown, markdown); if (args.json) await writeFile(args.json, JSON.stringify(json, null, 2) + '\n'); diff --git a/bin/build.sh b/bin/build.sh index 5fb4ed7..659c7c4 100755 --- a/bin/build.sh +++ b/bin/build.sh @@ -1,4 +1,6 @@ #!/bin/bash +# Builds lib/. Prints the time of each step. With BUILD_TIMINGS=, it also +# writes them to as JSON: { "steps": [{ "name", "ms" }], "total": ms }. set -euo pipefail cd "$(dirname "$0")/.." @@ -6,22 +8,60 @@ cd "$(dirname "$0")/.." TARGET=wasm32-unknown-unknown WASM="target/$TARGET/release/rregex.wasm" -echo " 🦀 Compiling wasm..." +# milliseconds; whole seconds where `date` has no %N +now_ms() { + local ns + ns=$(date +%s%N) + case "$ns" in + *[!0-9]*) echo $(($(date +%s) * 1000)) ;; + *) echo $((ns / 1000000)) ;; + esac +} + +STEPS=() +STARTED=$(now_ms) +STEP_NAME="" +STEP_START=0 + +step() { + end_step + echo " $1" + STEP_NAME=$2 + STEP_START=$(now_ms) +} + +end_step() { + [ -n "$STEP_NAME" ] || return 0 + local ms=$(($(now_ms) - STEP_START)) + STEPS+=("{\"name\":\"$STEP_NAME\",\"ms\":$ms}") + echo " ⏱️ $STEP_NAME: $((ms / 1000)).$(printf '%03d' $((ms % 1000)))s" + STEP_NAME="" +} + +step "🦀 Compiling wasm..." "cargo build" cargo build --release --target "$TARGET" -echo " 🔗 Generating bindings..." +step "🔗 Generating bindings..." "wasm-bindgen" rm -rf js/wasm lib wasm-bindgen --target web --omit-default-module-path \ --out-dir js/wasm --out-name rregex "$WASM" node bin/metadata.mjs -echo " 🗜️ Optimizing wasm..." +step "🗜️ Optimizing wasm..." "wasm-opt" mkdir -p lib -pnpm exec wasm-opt -O --enable-reference-types --enable-bulk-memory \ - --enable-nontrapping-float-to-int --enable-sign-ext --enable-mutable-globals \ - js/wasm/rregex_bg.wasm -o lib/rregex.wasm +./bin/wasm-opt.sh js/wasm/rregex_bg.wasm lib/rregex.wasm -echo " 📦 Bundling..." +step "📦 Bundling..." "tsdown" pnpm exec tsdown --silent -echo " ✅ Done!" +end_step +TOTAL=$(($(now_ms) - STARTED)) + +if [ -n "${BUILD_TIMINGS:-}" ]; then + ( + IFS=, + echo "{\"steps\":[${STEPS[*]}],\"total\":$TOTAL}" + ) > "$BUILD_TIMINGS" +fi + +echo " ✅ Done in $((TOTAL / 1000)).$(printf '%03d' $((TOTAL % 1000)))s!" diff --git a/bin/install-binaryen.sh b/bin/install-binaryen.sh new file mode 100755 index 0000000..3009a84 --- /dev/null +++ b/bin/install-binaryen.sh @@ -0,0 +1,40 @@ +#!/bin/bash +# Usage: bin/install-binaryen.sh +# +# Downloads the native binaryen release with the same version as the +# `binaryen` npm package to , checks its sha256 and prints its bin +# directory. bin/wasm-opt.sh uses that `wasm-opt` when it is in PATH. +set -euo pipefail + +cd "$(dirname "$0")/.." + +DIR=$1 +VERSION="version_$(node -p 'require("./node_modules/binaryen/package.json").version.split(".")[0]')" + +case "$(uname -s)-$(uname -m)" in + Linux-x86_64) PLATFORM=x86_64-linux ;; + Linux-aarch64 | Linux-arm64) PLATFORM=aarch64-linux ;; + Darwin-x86_64) PLATFORM=x86_64-macos ;; + Darwin-arm64) PLATFORM=arm64-macos ;; + *) + echo "no binaryen release for $(uname -s) $(uname -m)" >&2 + exit 1 + ;; +esac + +TARBALL="binaryen-$VERSION-$PLATFORM.tar.gz" +URL="https://github.com/WebAssembly/binaryen/releases/download/$VERSION/$TARBALL" + +mkdir -p "$DIR" +cd "$DIR" +curl -fsSLO "$URL" +curl -fsSLO "$URL.sha256" +if command -v sha256sum > /dev/null; then + sha256sum -c "$TARBALL.sha256" >&2 +else + shasum -a 256 -c "$TARBALL.sha256" >&2 +fi +tar xzf "$TARBALL" +rm "$TARBALL" "$TARBALL.sha256" + +echo "$PWD/binaryen-$VERSION/bin" diff --git a/bin/wasm-opt.sh b/bin/wasm-opt.sh new file mode 100755 index 0000000..1771c0e --- /dev/null +++ b/bin/wasm-opt.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# Usage: bin/wasm-opt.sh +# +# Optimizes the wasm with binaryen. It prefers a native `wasm-opt` with the +# same version as the `binaryen` npm package: same output, but multi-threaded +# and ~10x faster than the npm package, which is an emscripten (JS) build. +# Without one, it uses the npm package. +# +# WASM_OPT_NPM=1 always uses the npm package. +set -euo pipefail + +cd "$(dirname "$0")/.." + +FLAGS=( + -O + --enable-reference-types + --enable-bulk-memory + --enable-nontrapping-float-to-int + --enable-sign-ext + --enable-mutable-globals +) + +VERSION=$(node -p 'require("./node_modules/binaryen/package.json").version.split(".")[0]') + +# `pnpm run` and `node --run` put node_modules/.bin first in PATH, so skip it +native_wasm_opt() { + local bin + while read -r bin; do + case "$bin" in */node_modules/*) continue ;; esac + if [[ "$("$bin" --version 2> /dev/null)" == *"(version_$VERSION)"* ]]; then + echo "$bin" + return + fi + done < <(type -ap wasm-opt) +} + +NATIVE="" +if [ "${WASM_OPT_NPM:-}" != "1" ]; then + NATIVE=$(native_wasm_opt) +fi + +if [ -n "$NATIVE" ]; then + echo " native wasm-opt version_$VERSION ($NATIVE)" + "$NATIVE" "${FLAGS[@]}" "$1" -o "$2" +else + if [ "${WASM_OPT_NPM:-}" != "1" ]; then + echo " native wasm-opt version_$VERSION not found, using the npm package (slower)" + fi + pnpm exec wasm-opt "${FLAGS[@]}" "$1" -o "$2" +fi From 54443f8ba443ea5d6354499395e12e12e440e5bf Mon Sep 17 00:00:00 2001 From: frami Date: Sun, 1 Nov 2026 01:36:03 -0300 Subject: [PATCH 2/9] ci: remove the wasm-opt output check 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 --- .github/workflows/release.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0796bc1..e0aea93 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -102,13 +102,6 @@ jobs: name: lib path: lib/ - # the native wasm-opt must give the same wasm as the npm package - - name: checking wasm-opt - run: | - wasm-opt --version - WASM_OPT_NPM=1 ./bin/wasm-opt.sh js/wasm/rregex_bg.wasm target/rregex.npm.wasm - cmp target/rregex.npm.wasm lib/rregex.wasm - test_node: runs-on: ubuntu-24.04 From 7e0de1bd8ffe03d24720348e4ba53ea7e07cdc44 Mon Sep 17 00:00:00 2001 From: frami Date: Sun, 1 Nov 2026 01:38:40 -0300 Subject: [PATCH 3/9] build: install the native binaryen with `pnpm setup` 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 --- .github/workflows/benchmark.yml | 4 ---- .github/workflows/release.yml | 5 ----- bin/install.sh | 12 ++++++++++++ bin/wasm-opt.sh | 10 +++++++--- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index 8f2944b..8686191 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -91,10 +91,6 @@ jobs: pnpm install --frozen-lockfile node --run setup - # native wasm-opt, see the release workflow - - name: installing binaryen - run: ./bin/install-binaryen.sh "$RUNNER_TEMP/binaryen" >> "$GITHUB_PATH" - # check that the lib is complete; the build time goes to the report - name: building head run: | diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e0aea93..06fa6d7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -83,11 +83,6 @@ jobs: - name: installing run: pnpm install --frozen-lockfile - # native wasm-opt with the version of the binaryen npm package, used by - # bin/wasm-opt.sh: same output, ~10x faster than the npm (JS) build - - name: installing binaryen - run: ./bin/install-binaryen.sh "$RUNNER_TEMP/binaryen" >> "$GITHUB_PATH" - - name: setup run: node --run setup diff --git a/bin/install.sh b/bin/install.sh index 5d4b15a..0417c06 100755 --- a/bin/install.sh +++ b/bin/install.sh @@ -40,3 +40,15 @@ fi echo " ✅ $WASM_PACK_VERSION ($(which wasm-pack))" echo "" + +## native wasm-opt with the version of the binaryen npm package (bin/wasm-opt.sh) +BINARYEN_REQUIRED="version_$(node -p 'require("./node_modules/binaryen/package.json").version.split(".")[0]')" +WASM_OPT="target/binaryen/binaryen-$BINARYEN_REQUIRED/bin/wasm-opt" + +if [ ! -x "$WASM_OPT" ]; then + echo " 📥 installing binaryen $BINARYEN_REQUIRED" + ./bin/install-binaryen.sh target/binaryen > /dev/null +fi + +echo " ✅ $("$WASM_OPT" --version) ($WASM_OPT)" +echo "" diff --git a/bin/wasm-opt.sh b/bin/wasm-opt.sh index 1771c0e..ec494cf 100755 --- a/bin/wasm-opt.sh +++ b/bin/wasm-opt.sh @@ -4,7 +4,8 @@ # Optimizes the wasm with binaryen. It prefers a native `wasm-opt` with the # same version as the `binaryen` npm package: same output, but multi-threaded # and ~10x faster than the npm package, which is an emscripten (JS) build. -# Without one, it uses the npm package. +# It looks for it in PATH and in target/binaryen (`pnpm setup` installs it +# there). Without one, it uses the npm package. # # WASM_OPT_NPM=1 always uses the npm package. set -euo pipefail @@ -31,7 +32,10 @@ native_wasm_opt() { echo "$bin" return fi - done < <(type -ap wasm-opt) + done < <( + type -ap wasm-opt || true + echo "$PWD/target/binaryen/binaryen-version_$VERSION/bin/wasm-opt" + ) } NATIVE="" @@ -44,7 +48,7 @@ if [ -n "$NATIVE" ]; then "$NATIVE" "${FLAGS[@]}" "$1" -o "$2" else if [ "${WASM_OPT_NPM:-}" != "1" ]; then - echo " native wasm-opt version_$VERSION not found, using the npm package (slower)" + echo " native wasm-opt version_$VERSION not found, using the npm package (slower, run \`pnpm setup\`)" fi pnpm exec wasm-opt "${FLAGS[@]}" "$1" -o "$2" fi From 1521a45afe9801cfb467be59bd0e27153558109a Mon Sep 17 00:00:00 2001 From: frami Date: Sun, 1 Nov 2026 01:39:16 -0300 Subject: [PATCH 4/9] build: reuse the wasm-opt output when its input did not change 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 --- bin/wasm-opt.sh | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/bin/wasm-opt.sh b/bin/wasm-opt.sh index ec494cf..bac8d28 100755 --- a/bin/wasm-opt.sh +++ b/bin/wasm-opt.sh @@ -7,9 +7,13 @@ # It looks for it in PATH and in target/binaryen (`pnpm setup` installs it # there). Without one, it uses the npm package. # -# WASM_OPT_NPM=1 always uses the npm package. +# The last output is kept in target/wasm-opt and reused while the input does +# not change. +# +# WASM_OPT_NPM=1 always runs the npm package, without the cache. set -euo pipefail +SELF="$(cd "$(dirname "$0")" && pwd)/$(basename "$0")" cd "$(dirname "$0")/.." FLAGS=( @@ -38,6 +42,21 @@ native_wasm_opt() { ) } +sha256() { + if command -v sha256sum > /dev/null; then sha256sum; else shasum -a 256; fi | cut -d' ' -f1 +} + +# The output only depends on the input, the binaryen version and the flags in +# this file: reuse the last output when none of them changed +CACHE_DIR=target/wasm-opt +CACHE="$CACHE_DIR/$({ sha256 < "$1"; echo "$VERSION"; sha256 < "$SELF"; } | sha256).wasm" + +if [ "${WASM_OPT_NPM:-}" != "1" ] && [ -f "$CACHE" ]; then + echo " input did not change, reusing $CACHE" + cp "$CACHE" "$2" + exit 0 +fi + NATIVE="" if [ "${WASM_OPT_NPM:-}" != "1" ]; then NATIVE=$(native_wasm_opt) @@ -52,3 +71,9 @@ else fi pnpm exec wasm-opt "${FLAGS[@]}" "$1" -o "$2" fi + +if [ "${WASM_OPT_NPM:-}" != "1" ]; then + rm -rf "$CACHE_DIR" + mkdir -p "$CACHE_DIR" + cp "$2" "$CACHE" +fi From 3e6a9de92a32e95d4033032fb2d684d2b9380350 Mon Sep 17 00:00:00 2001 From: frami Date: Sun, 1 Nov 2026 01:40:40 -0300 Subject: [PATCH 5/9] build: run tsdown while wasm-opt runs 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 --- bin/bench-compare.mjs | 5 +++- bin/build.sh | 68 +++++++++++++++++++++++++------------------ 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/bin/bench-compare.mjs b/bin/bench-compare.mjs index 334ed27..26abdda 100644 --- a/bin/bench-compare.mjs +++ b/bin/bench-compare.mjs @@ -224,7 +224,10 @@ lines.push( '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. ' + - (build ? 'The build time of `cargo build` depends on the state of the CI cache. ' : '') + + (build + ? '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.', ); diff --git a/bin/build.sh b/bin/build.sh index 659c7c4..dc53922 100755 --- a/bin/build.sh +++ b/bin/build.sh @@ -1,6 +1,7 @@ #!/bin/bash # Builds lib/. Prints the time of each step. With BUILD_TIMINGS=, it also # writes them to as JSON: { "steps": [{ "name", "ms" }], "total": ms }. +# wasm-opt and tsdown run in parallel, so the total is less than the sum. set -euo pipefail cd "$(dirname "$0")/.." @@ -18,50 +19,59 @@ now_ms() { esac } -STEPS=() -STARTED=$(now_ms) -STEP_NAME="" -STEP_START=0 +seconds() { + echo "$(($1 / 1000)).$(printf '%03d' $(($1 % 1000)))s" +} -step() { - end_step - echo " $1" - STEP_NAME=$2 - STEP_START=$(now_ms) +mkdir -p target +TIMES=target/build-times.txt +: > "$TIMES" + +# timed : runs the command and records its time +timed() { + local name=$1 start ms + shift + start=$(now_ms) + "$@" + ms=$(($(now_ms) - start)) + echo "$name $ms" >> "$TIMES" + echo " ⏱️ $name: $(seconds "$ms")" } -end_step() { - [ -n "$STEP_NAME" ] || return 0 - local ms=$(($(now_ms) - STEP_START)) - STEPS+=("{\"name\":\"$STEP_NAME\",\"ms\":$ms}") - echo " ⏱️ $STEP_NAME: $((ms / 1000)).$(printf '%03d' $((ms % 1000)))s" - STEP_NAME="" +bindings() { + rm -rf js/wasm lib + wasm-bindgen --target web --omit-default-module-path \ + --out-dir js/wasm --out-name rregex "$WASM" + node bin/metadata.mjs + mkdir -p lib } -step "🦀 Compiling wasm..." "cargo build" -cargo build --release --target "$TARGET" +STARTED=$(now_ms) -step "🔗 Generating bindings..." "wasm-bindgen" -rm -rf js/wasm lib -wasm-bindgen --target web --omit-default-module-path \ - --out-dir js/wasm --out-name rregex "$WASM" -node bin/metadata.mjs +echo " 🦀 Compiling wasm..." +timed "cargo build" cargo build --release --target "$TARGET" -step "🗜️ Optimizing wasm..." "wasm-opt" -mkdir -p lib -./bin/wasm-opt.sh js/wasm/rregex_bg.wasm lib/rregex.wasm +echo " 🔗 Generating bindings..." +timed "wasm-bindgen" bindings -step "📦 Bundling..." "tsdown" -pnpm exec tsdown --silent +# tsdown only reads js/: lib/rregex.wasm is never bundled +echo " 🗜️ Optimizing wasm and 📦 bundling..." +timed "wasm-opt" ./bin/wasm-opt.sh js/wasm/rregex_bg.wasm lib/rregex.wasm & +WASM_OPT=$! +timed "tsdown" pnpm exec tsdown --silent +wait "$WASM_OPT" -end_step TOTAL=$(($(now_ms) - STARTED)) if [ -n "${BUILD_TIMINGS:-}" ]; then + STEPS=() + for name in "cargo build" "wasm-bindgen" "wasm-opt" "tsdown"; do + STEPS+=("{\"name\":\"$name\",\"ms\":$(grep "^$name " "$TIMES" | awk '{print $NF}')}") + done ( IFS=, echo "{\"steps\":[${STEPS[*]}],\"total\":$TOTAL}" ) > "$BUILD_TIMINGS" fi -echo " ✅ Done in $((TOTAL / 1000)).$(printf '%03d' $((TOTAL % 1000)))s!" +echo " ✅ Done in $(seconds "$TOTAL")!" From a35e502eb1f567ad4bcf38128c7e7fa058ee9d5d Mon Sep 17 00:00:00 2001 From: frami Date: Sun, 1 Nov 2026 01:41:29 -0300 Subject: [PATCH 6/9] ci: run the Rust tests in their own job and use Swatinem/rust-cache - `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 --- .github/workflows/benchmark.yml | 13 +++------- .github/workflows/release.yml | 45 +++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index 8686191..629f7a3 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -43,16 +43,11 @@ jobs: steps: - uses: actions/checkout@v7 - # same paths and key as the release workflow, so the cache of master is reused - - uses: actions/cache@v6 + # same shared key as the build of the release workflow, so the cache of + # master is reused + - uses: Swatinem/rust-cache@v2 with: - path: | - ~/.cargo/registry/index/ - ~/.cargo/registry/cache/ - ~/.cargo/git/db/ - target/ - key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} - restore-keys: ${{ runner.os }}-cargo- + shared-key: release # wasm-bindgen-cli must match the wasm-bindgen crate in Cargo.lock - name: reading wasm-bindgen version diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 06fa6d7..e1e39c0 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -57,15 +57,12 @@ jobs: registry-url: https://registry.npmjs.org/ cache: pnpm - - uses: actions/cache@v6 + # cargo registry and dependencies in target/. The key includes the rustc + # version, so a new Rust on the runner does not restore a stale cache. + # The benchmark workflow uses the same shared key. + - uses: Swatinem/rust-cache@v2 with: - path: | - ~/.cargo/registry/index/ - ~/.cargo/registry/cache/ - ~/.cargo/git/db/ - target/ - key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} - restore-keys: ${{ runner.os }}-cargo- + shared-key: release # wasm-bindgen-cli must match the wasm-bindgen crate in Cargo.lock - name: reading wasm-bindgen version @@ -86,9 +83,6 @@ jobs: - name: setup run: node --run setup - - name: testing wasm - run: node --run test:rs -- --node - - name: building run: node --run build @@ -97,6 +91,34 @@ jobs: name: lib path: lib/ + # the Rust tests build the crate in the test profile: in their own job, so + # the build and the jobs that need `lib` do not wait for them + test_rs: + runs-on: ubuntu-24.04 + + steps: + - uses: actions/checkout@v7 + + - uses: actions/setup-node@v7 + with: + node-version: "26" + + - uses: Swatinem/rust-cache@v2 + + # wasm-bindgen-cli must match the wasm-bindgen crate in Cargo.lock + - name: reading wasm-bindgen version + id: wasm-bindgen + run: echo "version=$(grep -A1 '^name = "wasm-bindgen"$' Cargo.lock | sed -n 's/^version = "\(.*\)"$/\1/p')" >> "$GITHUB_OUTPUT" + + - uses: taiki-e/install-action@v2 + with: + tool: wasm-pack,wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }} + + - run: rustup target add wasm32-unknown-unknown + + - name: testing wasm + run: node --run test:rs -- --node + test_node: runs-on: ubuntu-24.04 @@ -233,6 +255,7 @@ jobs: id-token: write # to enable use of OIDC for npm provenance needs: + - test_rs - test_node - test_bun - test_deno From 9c21efd1ca2cc178ff11fda6d649c7a99916c2e3 Mon Sep 17 00:00:00 2001 From: frami Date: Sun, 1 Nov 2026 02:34:24 -0300 Subject: [PATCH 7/9] build: generate the declarations that are the same as core.d.mts once 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 --- bin/build.sh | 10 +++++++++- tsdown.config.ts | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/bin/build.sh b/bin/build.sh index dc53922..3ca2c70 100755 --- a/bin/build.sh +++ b/bin/build.sh @@ -46,6 +46,14 @@ bindings() { mkdir -p lib } +# tsdown.config.ts does not generate these declarations: same as core.d.mts +bundle() { + pnpm exec tsdown --silent + for dts in core.d.cts commonjs.d.cts cf.d.mts; do + cp lib/core.d.mts "lib/$dts" + done +} + STARTED=$(now_ms) echo " 🦀 Compiling wasm..." @@ -58,7 +66,7 @@ timed "wasm-bindgen" bindings echo " 🗜️ Optimizing wasm and 📦 bundling..." timed "wasm-opt" ./bin/wasm-opt.sh js/wasm/rregex_bg.wasm lib/rregex.wasm & WASM_OPT=$! -timed "tsdown" pnpm exec tsdown --silent +timed "tsdown" bundle wait "$WASM_OPT" TOTAL=$(($(now_ms) - STARTED)) diff --git a/tsdown.config.ts b/tsdown.config.ts index 9a062bc..9e325a0 100644 --- a/tsdown.config.ts +++ b/tsdown.config.ts @@ -2,6 +2,10 @@ import { defineConfig, type UserConfig } from "tsdown"; // Every entry is one self-contained file in lib/ next to lib/rregex.wasm. // bin/build.sh generates js/wasm/ (wasm-bindgen glue and metadata) first. +// +// commonjs, core (cjs) and cf export exactly what core exports, so their +// declarations are the same as core.d.mts: bin/build.sh copies it instead of +// generating them (`dts: false`). const shared = { outDir: "lib", clean: false, @@ -29,6 +33,7 @@ export default defineConfig([ entry: { commonjs: "js/node.ts" }, platform: "node", format: "cjs", + dts: false, outExtensions: ext(".cjs", ".d.cts"), }, // Any runtime, the user loads the module @@ -44,6 +49,7 @@ export default defineConfig([ entry: { core: "js/core.ts" }, platform: "neutral", format: "cjs", + dts: false, outExtensions: ext(".cjs", ".d.cts"), }, // Browsers and bundlers @@ -60,6 +66,7 @@ export default defineConfig([ entry: { cf: "js/workerd.ts" }, platform: "neutral", format: "esm", + dts: false, deps: { onlyBundle: false, neverBundle: [/\.wasm$/] }, outExtensions: ext(".mjs", ".d.mts"), }, From 6571118fc12c3bdd1e808683f806761676e50fa5 Mon Sep 17 00:00:00 2001 From: frami Date: Sun, 1 Nov 2026 02:34:52 -0300 Subject: [PATCH 8/9] ci: cache the native binaryen and check the regex engine versions in 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 --- .github/workflows/benchmark.yml | 17 ++++++++++++++--- .github/workflows/release.yml | 29 ++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index 629f7a3..57abdeb 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -81,10 +81,21 @@ jobs: test -f .latest/lib/esm.mjs -a -f .latest/lib/rregex.wasm echo "LATEST_VERSION=$(jq -r .version <<< "$meta")" >> "$GITHUB_ENV" + - name: installing + run: pnpm install --frozen-lockfile + + # native binaryen for bin/wasm-opt.sh; rust-cache does not keep it + - name: reading binaryen version + id: binaryen + run: echo "version=$(node -p 'require("./node_modules/binaryen/package.json").version')" >> "$GITHUB_OUTPUT" + + - uses: actions/cache@v6 + with: + path: target/binaryen + key: ${{ runner.os }}-binaryen-${{ steps.binaryen.outputs.version }} + - name: setup - run: | - pnpm install --frozen-lockfile - node --run setup + run: node --run setup # check that the lib is complete; the build time goes to the report - name: building head diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e1e39c0..33e3858 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -74,12 +74,19 @@ jobs: with: tool: wasm-pack,wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }} - - name: checking regex engine versions - run: ./bin/check-regex-deps.sh - - name: installing run: pnpm install --frozen-lockfile + # native binaryen for bin/wasm-opt.sh; rust-cache does not keep it + - name: reading binaryen version + id: binaryen + run: echo "version=$(node -p 'require("./node_modules/binaryen/package.json").version')" >> "$GITHUB_OUTPUT" + + - uses: actions/cache@v6 + with: + path: target/binaryen + key: ${{ runner.os }}-binaryen-${{ steps.binaryen.outputs.version }} + - name: setup run: node --run setup @@ -91,6 +98,21 @@ jobs: name: lib path: lib/ + # asks crates.io for the publish dates: in its own job, next to the build + check_deps: + runs-on: ubuntu-24.04 + + steps: + - uses: actions/checkout@v7 + with: + sparse-checkout: | + Cargo.lock + bin/check-regex-deps.sh + sparse-checkout-cone-mode: false + + - name: checking regex engine versions + run: ./bin/check-regex-deps.sh + # the Rust tests build the crate in the test profile: in their own job, so # the build and the jobs that need `lib` do not wait for them test_rs: @@ -255,6 +277,7 @@ jobs: id-token: write # to enable use of OIDC for npm provenance needs: + - check_deps - test_rs - test_node - test_bun From e2e1ae76700a23c30b2625bd769cc86a146a211c Mon Sep 17 00:00:00 2001 From: frami Date: Sun, 1 Nov 2026 02:36:13 -0300 Subject: [PATCH 9/9] build: install wasm-bindgen-cli and wasm-pack with cargo-binstall when 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 --- bin/install.sh | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/bin/install.sh b/bin/install.sh index 0417c06..5e655d2 100755 --- a/bin/install.sh +++ b/bin/install.sh @@ -17,13 +17,23 @@ echo "" rustup target add wasm32-unknown-unknown +## prebuilt binaries with cargo-binstall when it is installed; +## `cargo install` compiles each one (~1-2 min) +cargo_install() { + if cargo binstall -V > /dev/null 2>&1; then + cargo binstall --no-confirm "$@" + else + cargo install "$@" + fi +} + ## wasm-bindgen-cli must match the wasm-bindgen crate version in Cargo.lock WASM_BINDGEN_REQUIRED=$(grep -A1 '^name = "wasm-bindgen"$' Cargo.lock | sed -n 's/^version = "\(.*\)"$/\1/p') WASM_BINDGEN_VERSION=$(wasm-bindgen --version 2> /dev/null | cut -d' ' -f2 || true) if [ "$WASM_BINDGEN_VERSION" != "$WASM_BINDGEN_REQUIRED" ]; then echo " 📥 installing wasm-bindgen-cli $WASM_BINDGEN_REQUIRED" - cargo install wasm-bindgen-cli --version "$WASM_BINDGEN_REQUIRED" --locked --force + cargo_install wasm-bindgen-cli --version "$WASM_BINDGEN_REQUIRED" --locked --force fi echo " ✅ wasm-bindgen $(wasm-bindgen --version | cut -d' ' -f2) ($(which wasm-bindgen))" @@ -34,7 +44,7 @@ WASM_PACK_VERSION=$(wasm-pack --version 2> /dev/null || true) ## wasm-pack is only used to run the Rust tests (pnpm test:rs) if [ "$WASM_PACK_VERSION" = "" ]; then echo " 📥 installing wasm-pack" - cargo install wasm-pack + cargo_install wasm-pack WASM_PACK_VERSION=$(wasm-pack --version) fi