#!/usr/bin/env bash # SPDX-License-Identifier: MIT # A/B a base revision against the working tree under several code layouts # (gameboy-rust-fast's scripts/ab.sh, adapted). # # Two builds of the same source can differ by +-5 % from where the linker puts the hot blocks # alone, so one build per side measures layout luck, not the change. Each side is built under # each layout (RUSTFLAGS) and timed with ab.py (alternating, best of ROUNDS); a change is kept # only when it wins on every layout. # # scripts/ab.sh [BASE_REV] (default: HEAD) # env: ROUNDS (default 6), FRAMES (300), CPU (31), LAYOUTS ("default nf5 nf6"), BUILD_JOBS (2) # # The new side is a snapshot of this working tree (`git stash create`: tracked files only, so # `git add` a new file first) checked out at target/ab-new, so the tree can be edited while the # A/B runs; the base side is a worktree at the base revision (target/ab-base). Both have their # 6502-rust path made absolute; the base runs its own a2600-bench.rs (the working tree's when # the base predates it). set -euo pipefail ROOT=$(cd "$(dirname "$0")/.." && pwd) CPU6502=$(cd "$ROOT/../6502-rust" && pwd) BASE=${1:-HEAD} ROUNDS=${ROUNDS:-6} FRAMES=${FRAMES:-300} CPU=${CPU:-31} LAYOUTS=${LAYOUTS:-"default nf5 nf6"} declare -A FLAGS=( [default]="" [nf5]="-C llvm-args=-align-all-nofallthru-blocks=5" [nf6]="-C llvm-args=-align-all-nofallthru-blocks=6" ) rev=$(git -C "$ROOT" rev-parse --short "$BASE") snap=$(git -C "$ROOT" stash create) new_rev=$(git -C "$ROOT" rev-parse --short "${snap:-HEAD}") B="$ROOT/target/ab-base" N="$ROOT/target/ab-new" worktree() { # dir rev [ -d "$1" ] || git -C "$ROOT" worktree add -q --detach "$1" "$2" git -C "$1" checkout -q -f --detach "$2" python3 - "$1/Cargo.toml" "$CPU6502" <<'EOF' import sys path, cpu = sys.argv[1:] text = open(path).read() text = text.replace('path = "../6502-rust"', f'path = "{cpu}"') open(path, "w").write(text) EOF } worktree "$B" "$rev" worktree "$N" "$new_rev" [ -f "$B/src/bin/a2600-bench.rs" ] || cp "$ROOT/src/bin/a2600-bench.rs" "$B/src/bin/" build() { # tree layout -> binary local tree=$1 layout=$2 target="$1/target/ab-$2" RUSTFLAGS="${FLAGS[$layout]}" CARGO_TARGET_DIR="$target" nice -n 19 \ cargo build --release -q -j "${BUILD_JOBS:-2}" --manifest-path "$tree/Cargo.toml" --bin a2600-bench >&2 || return 1 echo "$target/release/a2600-bench" } echo "base $rev vs working tree (snapshot $new_rev); $ROUNDS rounds, $FRAMES frames, cpu $CPU, load $(cut -d' ' -f1 /proc/loadavg)" for layout in $LAYOUTS; do a=$(build "$B" "$layout") || { echo "base build failed"; exit 1; } b=$(build "$N" "$layout") || { echo "working-tree build failed"; exit 1; } echo "$layout:" nice -n 19 python3 "$ROOT/scripts/ab.py" "$a" "$b" --rounds "$ROUNDS" --frames "$FRAMES" --cpu "$CPU" | while IFS= read -r line; do echo " $line"; done done