Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down
74 changes: 60 additions & 14 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
23 changes: 20 additions & 3 deletions bin/bench-compare.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
// Usage:
// node bin/bench-compare.mjs --base <lib dir> --head <lib dir> \
// [--base-label <text>] [--head-label <text>] \
// [--runtime <json>]... \
// [--runtime <json>]... [--build-time <json>] \
// [--markdown <file>] [--json <file>] \
// [--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=<file>).
//
// 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
Expand Down Expand Up @@ -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' },
Expand All @@ -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. */
Expand Down Expand Up @@ -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(
'<sub>Δ 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.</sub>',
);

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');
Expand Down
84 changes: 71 additions & 13 deletions bin/build.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,85 @@
#!/bin/bash
# Builds lib/. Prints the time of each step. With BUILD_TIMINGS=<file>, it also
# writes them to <file> 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")/.."

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 <name> <command...>: 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")!"
40 changes: 40 additions & 0 deletions bin/install-binaryen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash
# Usage: bin/install-binaryen.sh <dir>
#
# Downloads the native binaryen release with the same version as the
# `binaryen` npm package to <dir>, 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"
Loading