#!/usr/bin/env bash # SPDX-License-Identifier: MIT # A/B a base revision against the working tree under several code layouts. # # 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 (methodology, # "Measuring speed"; gameboy-rust-fast's ab.sh). Each side is built under each layout # (RUSTFLAGS) and timed with ab.py (alternating, best of ROUNDS, one pinned CPU); a change # is kept when it wins on every layout. # # scripts/ab.sh [BASE_REV (default HEAD)] # env: ROUNDS (default 6), STEPS (10,000,000), CPU (30), LAYOUTS ("default nf5 nf6"), # FEATURES_B (cargo features for the working tree's build only), ENV_B (NAME=VALUE # for its runs only: SH2_BLOCK_CACHE=1 measures the cached interpreter) set -euo pipefail ROOT=$(cd "$(dirname "$0")/.." && pwd) BASE=${1:-HEAD} ROUNDS=${ROUNDS:-6} STEPS=${STEPS:-10000000} CPU=${CPU:-30} 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") B="$ROOT/target/ab-base" [ -d "$B" ] || git -C "$ROOT" worktree add -q --detach "$B" "$rev" git -C "$B" checkout -q -f --detach "$rev" build() { # tree layout features -> binary local tree=$1 layout=$2 features=$3 target="$1/target/ab-$2" RUSTFLAGS="${FLAGS[$layout]}" CARGO_TARGET_DIR="$target" nice -n 19 \ cargo build --release -q -j 2 --manifest-path "$tree/Cargo.toml" --bin sh2-bench ${features:+--features "$features"} >&2 \ || return 1 echo "$target/release/sh2-bench" } echo "base $rev vs working tree; $ROUNDS rounds, $STEPS steps, cpu $CPU, load $(cut -d' ' -f1-3 /proc/loadavg)" for layout in $LAYOUTS; do a=$(build "$B" "$layout" "") || { echo "base build failed"; exit 1; } b=$(build "$ROOT" "$layout" "${FEATURES_B:-}") || { echo "working-tree build failed"; exit 1; } line=$(nice -n 0 python3 "$ROOT/scripts/ab.py" "$a" "$b" --rounds "$ROUNDS" --steps "$STEPS" --cpu "$CPU" \ ${ENV_B:+--env-b "$ENV_B"} | tr '\n' ';') echo "$layout: $line load $(cut -d' ' -f1 /proc/loadavg)" done