Files
AILang/bench/run.sh
T
Brummel 65e280bb70 Bench: GC overhead via bump-allocator comparison
Adds --alloc=<gc|bump> to ail build/run. Bump path links a 256MB
no-free arena C stub instead of libgc; IR is byte-identical except
for the @GC_malloc → @bump_malloc symbol swap. Bench harness times
two allocation-heavy workloads (list cons/sum and balanced tree
build/walk) under both modes.

Numbers (RUNS=5, median of 4):
  bench_list_sum   gc 0.141s  bump 0.048s  +194%
  bench_tree_walk  gc 0.103s  bump 0.041s  +151%

Bucket: large. ~60% of runtime is Boehm on these workloads —
upper bound for any realistic program. Both fixtures hold the
heap fully live, so the cost we're seeing is Boehm's allocate
path itself, not collection work; that fact narrows the design
space for the GC discussion.

- crates/ailang-codegen: AllocStrategy enum, three callsites and
  the IR header parameterised.
- crates/ail/src/main.rs: --alloc flag plumbed; bump runtime
  located + compiled on demand.
- runtime/bump.c: 256MB static arena, abort-on-overflow.
- examples/bench_list_sum, bench_tree_walk: accumulator-form
  fixtures (textbook recursive sum was constructor-blocked).
- bench/run.sh: harness with Python timing helper (Arch's
  /usr/bin/time isn't part of the base install).

No language-level changes; default --alloc=gc, all 141 workspace
tests green, all 5 IR snapshots unchanged, 11 prior fixtures
produce identical stdout.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 00:06:20 +02:00

170 lines
5.8 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# GC-overhead bench harness (Bench iter).
#
# Builds each fixture twice — `--alloc=gc` (Boehm conservative GC) and
# `--alloc=bump` (no-free 256 MB arena from `runtime/bump.c`). Runs each
# binary N times, drops the slowest run, takes the median wall time.
# The bump number minus the gc number is the upper-bound cost of GC.
#
# Output: a table with gc-median, bump-median, overhead %, and max RSS
# for both modes. Designed to be captured verbatim into a JOURNAL entry.
#
# Requirements: bash, /usr/bin/time -v (GNU coreutils), bc, sort, awk,
# a release-mode `ail` binary.
#
# Usage: bench/run.sh [-n RUNS]
# -n RUNS number of timed runs per binary (default 5; min 3 so we
# can drop the slowest and still take a median over 4).
set -euo pipefail
RUNS=5
while getopts "n:" opt; do
case $opt in
n) RUNS="$OPTARG" ;;
*) echo "usage: $0 [-n RUNS]" >&2; exit 2 ;;
esac
done
if (( RUNS < 3 )); then
echo "RUNS must be >= 3" >&2
exit 2
fi
# Anchor at the workspace root regardless of CWD.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
cd "$ROOT"
# We measure wall-clock + max RSS via a small Python helper that wraps
# the binary, calls `time.monotonic()` around `os.waitpid`, and reads
# `getrusage(RUSAGE_CHILDREN).ru_maxrss` (KB on Linux). This avoids a
# /usr/bin/time dependency (Arch / minimal containers often don't have
# the GNU coreutils `time` binary installed) without sacrificing
# either signal: monotonic clocks for wall time, kernel-reported peak
# resident set for RSS.
PY="$(command -v python3 || true)"
if [[ -z "$PY" ]]; then
echo "error: python3 is required for the timing helper" >&2
exit 2
fi
# Build the release `ail` binary if needed.
echo ">>> ensuring release ail binary"
cargo build --release -p ail >/dev/null
AIL="$ROOT/target/release/ail"
[[ -x "$AIL" ]] || { echo "ail binary missing: $AIL" >&2; exit 1; }
OUTDIR="$ROOT/target/bench"
mkdir -p "$OUTDIR"
# Compile both modes for both fixtures up front so the bench loop only
# measures runtime, not build time.
fixtures=(bench_list_sum bench_tree_walk)
modes=(gc bump)
echo ">>> compiling fixtures (-O2)"
for f in "${fixtures[@]}"; do
src="$ROOT/examples/$f.ail.json"
[[ -f "$src" ]] || { echo "missing fixture: $src" >&2; exit 1; }
for m in "${modes[@]}"; do
bin="$OUTDIR/${f}_${m}"
echo " $f --alloc=$m -> $bin"
"$AIL" build --opt=-O2 --alloc="$m" "$src" -o "$bin" >/dev/null
done
done
# Time one binary one time. Wraps the binary in a Python helper that
# measures wall-clock via time.monotonic() and max RSS (KB) via
# getrusage(RUSAGE_CHILDREN).ru_maxrss after the child exits. Stdout
# of the binary is discarded; we already verified correctness via a
# smoke run earlier. Output: "wall_seconds rss_kb" on a single line.
time_one() {
local bin="$1"
"$PY" -c '
import os, resource, subprocess, sys, time
bin_path = sys.argv[1]
t0 = time.monotonic()
p = subprocess.Popen([bin_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
p.wait()
t1 = time.monotonic()
ru = resource.getrusage(resource.RUSAGE_CHILDREN)
# ru_maxrss is in KB on Linux. We want the peak across only this child;
# RUSAGE_CHILDREN is cumulative across all children of the helper, but
# the helper only spawns this one child per invocation, so the value is
# this run.
print(f"{t1 - t0:.3f} {ru.ru_maxrss}")
sys.exit(0 if p.returncode == 0 else 1)
' "$bin"
}
# Run a binary RUNS times, drop the slowest run by wall time, return
# median of the rest as `wall rss` (rss = max across the kept runs).
median_run() {
local bin="$1"
local times=()
local rsses=()
for ((i = 0; i < RUNS; i++)); do
read -r w r < <(time_one "$bin")
times+=("$w")
rsses+=("$r")
done
# Compute index of slowest (largest wall) and drop it.
local slowest_idx=0
for ((i = 1; i < ${#times[@]}; i++)); do
if [[ $(awk -v a="${times[$i]}" -v b="${times[$slowest_idx]}" 'BEGIN { print (a > b) ? 1 : 0 }') == 1 ]]; then
slowest_idx=$i
fi
done
local kept_t=()
local kept_r=()
for ((i = 0; i < ${#times[@]}; i++)); do
if [[ $i -ne $slowest_idx ]]; then
kept_t+=("${times[$i]}")
kept_r+=("${rsses[$i]}")
fi
done
# Median wall over kept runs.
local sorted_t
sorted_t=$(printf "%s\n" "${kept_t[@]}" | sort -g)
local n=${#kept_t[@]}
local mid=$((n / 2))
local median_t
if (( n % 2 == 1 )); then
median_t=$(echo "$sorted_t" | sed -n "$((mid + 1))p")
else
local a b
a=$(echo "$sorted_t" | sed -n "${mid}p")
b=$(echo "$sorted_t" | sed -n "$((mid + 1))p")
median_t=$(awk -v a="$a" -v b="$b" 'BEGIN { printf "%.3f", (a + b) / 2 }')
fi
# Max RSS across kept runs (peak memory is the natural per-run agg).
local max_r=0
for r in "${kept_r[@]}"; do
if (( r > max_r )); then max_r=$r; fi
done
printf "%s %s\n" "$median_t" "$max_r"
}
echo
echo ">>> timing (RUNS=$RUNS, drop slowest, median of $((RUNS - 1)))"
echo
# Header.
printf "%-22s | %12s | %12s | %12s | %14s | %14s\n" \
"workload" "gc median(s)" "bump median(s)" "overhead %" "gc max RSS(KB)" "bump max RSS(KB)"
printf -- "-----------------------+--------------+--------------+--------------+----------------+----------------\n"
for f in "${fixtures[@]}"; do
read -r gc_t gc_r < <(median_run "$OUTDIR/${f}_gc")
read -r bp_t bp_r < <(median_run "$OUTDIR/${f}_bump")
# overhead = (gc - bump) / bump * 100. Negative would mean GC
# faster, which would itself be a finding worth reporting.
overhead=$(awk -v g="$gc_t" -v b="$bp_t" 'BEGIN { printf "%.1f", (g - b) / b * 100 }')
printf "%-22s | %12s | %12s | %12s | %14s | %14s\n" \
"$f" "$gc_t" "$bp_t" "$overhead" "$gc_r" "$bp_r"
done
echo
echo ">>> done"