makepad/libs/box3d/pgo.sh
Admin eb0b7de06e box3d: broad-phase hybrid — drop the parallel batch, keep the serial BVTT
The parallel batch machinery from be21d627a only ever existed to chase a
default-on multi-threaded win that never came (washer w8 still +54% with
the hybrid on). At w1 — the only configuration this opt-in flag is for —
parallel_for runs inline, so the serial path gives the identical result
for ~340 fewer lines. Wire up the previously-dead
dynamic_tree_self_pairs/cross_pairs into a serial collect_batch_candidates
(three BVTT self/cross traversals -> canonical (a,b,child) sort -> serial
filter into move_results[0]) and delete BatchWork, BatchCtx, batch_drain_*,
BatchFilterCtx, batch_filter_*, bvtt_step, dynamic_tree_bvtt_drain/expand,
and the batch_frontier/worker_* scratch fields.

Determinism preserved exactly: OFF 0x61E35C31/step314 bit-identical, ON
0xBE99C5F7/step313 identical across workers 1/2/4. The debug SET-equality
oracle and the determinism_broad_phase_hybrid_across_worker_counts test
are unchanged and still pass; zero warnings.

PGO: pgo.sh never trained -bp=1, so an off-path-only profile laid the
hybrid branch out cold and collapsed the win to ~-5%. Add one -b=8 -bp=1
training run (neutral for the default path — counts merge, the OFF branch
stays hot) and retrain.

Corrected README numbers to measured values (hybrid-trained profile,
paired -bp toggle, washer w1): pair-finding stage 7.6k -> 3.6k ms/1000
(-52%); total ~-17% (~19.0k vs ~22.6k), which beats C (20661) and narrows
Rapier's lead from ~23% to ~12% — not the "17.7k / within 5% / -19%"
be21d627a claimed. Still default-off (w8 regresses ~+54%), opt-in
single-threaded accelerator for churn-heavy scenes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 10:08:45 +02:00

50 lines
2.3 KiB
Bash
Executable file

#!/bin/zsh
# Profile-guided-optimization build of the box3d benchmark (or any binary
# using this crate — adjust the cargo targets). Measured on the upstream
# benchmark scenes: 11-19% faster than the plain fat-LTO release build
# (large_pyramid -19%, junkyard -14%, many_pyramids -11%; Apple Silicon,
# 2026-07-05), with the determinism hash bit-identical — PGO changes code
# layout and inlining, never arithmetic.
#
# Usage: ./pgo.sh (from libs/box3d; writes target dirs under /tmp)
set -e
HERE=$(cd "$(dirname "$0")" && pwd)
ROOT=$(cd "$HERE/../.." && pwd)
PGO=/tmp/box3d-pgo
PROFDATA=$(find ~/.rustup/toolchains -name llvm-profdata | head -1)
if [ -z "$PROFDATA" ]; then
echo "llvm-profdata not found: rustup component add llvm-tools" >&2
exit 1
fi
cd "$ROOT"
rm -rf $PGO/data && mkdir -p $PGO/data
echo "== 1/3 building instrumented benchmark =="
RUSTFLAGS="-Cprofile-generate=$PGO/data" \
CARGO_PROFILE_RELEASE_LTO=fat CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1 \
cargo build --release -p makepad-box3d --example benchmark --target-dir $PGO/train
echo "== 2/3 training (one pass over the benchmark scenes) =="
for b in 0 1 2 3 4 5 6 7 8 9; do
$PGO/train/release/examples/benchmark -b=$b -r=1 -w=1 > /dev/null
done
$PGO/train/release/examples/benchmark -b=5 -r=1 -w=8 > /dev/null
# Train the narrow phase in both feature-recycling modes so the full-SAT
# path keeps a hot layout (it still runs on cache misses/refreshes when
# the tier is on, and -fr=0 stays a supported toggle).
$PGO/train/release/examples/benchmark -b=4 -r=1 -w=1 -fr=0 > /dev/null
$PGO/train/release/examples/benchmark -b=8 -r=1 -w=1 -fr=0 > /dev/null
# Train the opt-in broad-phase hybrid batch path (washer is the churn scene
# that engages it) so enabling -bp=1 gets a hot layout instead of the cold
# fallthrough it would get from an off-path-only profile. Neutral for the
# default (-bp=0) path — PGO merges counts, the OFF branch stays hot.
$PGO/train/release/examples/benchmark -b=8 -r=1 -w=1 -bp=1 > /dev/null
"$PROFDATA" merge -o $PGO/merged.profdata $PGO/data
echo "== 3/3 building PGO-optimized benchmark =="
RUSTFLAGS="-Cprofile-use=$PGO/merged.profdata" \
CARGO_PROFILE_RELEASE_LTO=fat CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1 \
cargo build --release -p makepad-box3d --example benchmark --target-dir $PGO/opt
echo "done: $PGO/opt/release/examples/benchmark"