diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index 3e9db00..57abdeb 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" @@ -41,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 @@ -84,15 +81,27 @@ 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 + # 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 +125,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..33e3858 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 @@ -77,18 +74,22 @@ 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 - - name: testing wasm - run: node --run test:rs -- --node - - name: building run: node --run build @@ -97,6 +98,49 @@ 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: + 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 +277,8 @@ jobs: id-token: write # to enable use of OIDC for npm provenance needs: + - check_deps + - test_rs - test_node - test_bun - test_deno diff --git a/bin/bench-compare.mjs b/bin/bench-compare.mjs index 686e334..26abdda 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,29 @@ 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; ' + + '`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.', ); 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..3ca2c70 100755 --- a/bin/build.sh +++ b/bin/build.sh @@ -1,4 +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")/.." @@ -6,22 +9,77 @@ cd "$(dirname "$0")/.." TARGET=wasm32-unknown-unknown WASM="target/$TARGET/release/rregex.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 +} + +seconds() { + echo "$(($1 / 1000)).$(printf '%03d' $(($1 % 1000)))s" +} + +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")" +} + +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 +} + +# 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..." -cargo build --release --target "$TARGET" +timed "cargo build" cargo build --release --target "$TARGET" echo " 🔗 Generating 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 +timed "wasm-bindgen" bindings + +# 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" bundle +wait "$WASM_OPT" -echo " 🗜️ Optimizing wasm..." -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 +TOTAL=$(($(now_ms) - STARTED)) -echo " 📦 Bundling..." -pnpm exec tsdown --silent +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!" +echo " ✅ Done in $(seconds "$TOTAL")!" 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/install.sh b/bin/install.sh index 5d4b15a..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,9 +44,21 @@ 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 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 new file mode 100755 index 0000000..bac8d28 --- /dev/null +++ b/bin/wasm-opt.sh @@ -0,0 +1,79 @@ +#!/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. +# It looks for it in PATH and in target/binaryen (`pnpm setup` installs it +# there). Without one, it 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=( + -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 || true + echo "$PWD/target/binaryen/binaryen-version_$VERSION/bin/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) +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, run \`pnpm setup\`)" + 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 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"), },