diff --git a/crates/solverforge-solver/benches/selector_cursor_gate.rs b/crates/solverforge-solver/benches/selector_cursor_gate.rs index 68b4835e..c963fecb 100644 --- a/crates/solverforge-solver/benches/selector_cursor_gate.rs +++ b/crates/solverforge-solver/benches/selector_cursor_gate.rs @@ -46,6 +46,29 @@ static LIVE_BYTES: AtomicIsize = AtomicIsize::new(0); static PEAK_LIVE_BYTES: AtomicIsize = AtomicIsize::new(0); static ITERATIONS: OnceLock = OnceLock::new(); +/* Data placed ahead of the rest of the bench's read-only data, sized per +gate layout (`SOLVERFORGE_BENCH_DATA_PAD`, bytes). Cache simulation depends +on where the linker places data, so each layout moves it by a different +amount on both sides and the gate's medians sample several placements. */ +#[used] +static DATA_PAD: [u8; DATA_PAD_LEN] = [1; DATA_PAD_LEN]; + +const DATA_PAD_LEN: usize = match option_env!("SOLVERFORGE_BENCH_DATA_PAD") { + Some(len) => parse_data_pad(len.as_bytes()), + None => 0, +}; + +const fn parse_data_pad(digits: &[u8]) -> usize { + let mut value = 0; + let mut position = 0; + while position < digits.len() { + assert!(digits[position].is_ascii_digit(), "data pad is a decimal byte count"); + value = value * 10 + (digits[position] - b'0') as usize; + position += 1; + } + value +} + #[global_allocator] static GLOBAL_ALLOCATOR: MeasuringAllocator = MeasuringAllocator; diff --git a/scripts/bench-zero-regression.sh b/scripts/bench-zero-regression.sh index 04be9ced..e6fabe41 100755 --- a/scripts/bench-zero-regression.sh +++ b/scripts/bench-zero-regression.sh @@ -34,10 +34,20 @@ BENCH_ISOLATE=${BENCH_ISOLATE:-1} # The simulation repeats exactly for one binary, but it depends on where the # linker places each function, and that placement follows symbol hashes: any # change to a generic type name reorders functions whose bodies are untouched. -# Each side is therefore built in SIM_LAYOUTS layouts that differ only in -# `-C metadata`, and every simulated metric is taken as its median across -# them; layout 0 is the binary the timed trials run. SIM_JOBS simulations run -# at once, which does not change their counts. +# It also depends on where the linker places data: on v0.31.4, 896 bytes of +# unused read-only data moved construction_first_fit's data-L1 misses -15% on +# one layout and +0.5% on another. Each side is therefore built in +# SIM_LAYOUTS layouts that differ in `-C metadata` and in a read-only pad of +# (layout * 1984) mod 4096 bytes compiled into the bench +# (SOLVERFORGE_BENCH_DATA_PAD), the same for both sides, so the layouts sample +# code and data placement; every simulated metric is taken as its median +# across them, and layout 0 is the binary the timed trials run. SIM_JOBS +# simulations run at once, which does not change their counts. +# construction_first_fit's data-L1 misses spread 3.9% across placements of +# one source: the medians of two identical-code sides over 5 placements +# differed by more than 5.6% in 5% of resamples, over 21 by more than 2.5%. +# It is built in FIRST_FIT_SIM_LAYOUTS layouts (21); the extra layouts only +# feed the simulation. # Across layouts of one source, simulated instructions, data-L1 misses and # last-level misses hold within a fraction of a percent, so they are graded. # Simulated instruction-L1 misses and branch mispredictions are reported only: @@ -58,6 +68,7 @@ BENCH_ISOLATE=${BENCH_ISOLATE:-1} # by 32 bytes read +10% wall time (ruin) and +9.9% hardware branch misses # (nearby_swap), while the medians over five layouts moved +1.2% and +1.0%. SIM_LAYOUTS=${SIM_LAYOUTS:-5} +FIRST_FIT_SIM_LAYOUTS=${FIRST_FIT_SIM_LAYOUTS:-21} SIM_JOBS=${SIM_JOBS:-8} # Each trial's hardware counters are the mean of PERF_REPEATS process runs. # cache-misses counts last-level misses, and the last-level cache is shared @@ -108,8 +119,9 @@ if [[ $BENCH_ISOLATE == 1 ]] && ! sudo -n true 2>/dev/null; then echo "BENCH_ISOLATE=1 needs passwordless sudo to isolate CPU ${BENCH_CPU}" >&2 exit 2 fi -if ! [[ $SIM_LAYOUTS =~ ^[1-9][0-9]*$ && $SIM_JOBS =~ ^[1-9][0-9]*$ ]]; then - echo "SIM_LAYOUTS and SIM_JOBS must be positive integers" >&2 +if ! [[ $SIM_LAYOUTS =~ ^[1-9][0-9]*$ && $SIM_JOBS =~ ^[1-9][0-9]*$ \ + && $FIRST_FIT_SIM_LAYOUTS =~ ^[1-9][0-9]*$ ]] || ((FIRST_FIT_SIM_LAYOUTS < SIM_LAYOUTS)); then + echo "SIM_LAYOUTS, FIRST_FIT_SIM_LAYOUTS (>= SIM_LAYOUTS) and SIM_JOBS must be positive integers" >&2 exit 2 fi if [[ $BASELINE_CURSOR_API != 0 && $BASELINE_CURSOR_API != 1 ]]; then @@ -283,6 +295,14 @@ for case_name in "${CASES[@]}"; do fi done +sim_layouts_for_case() { + if [[ $1 == construction_first_fit ]]; then + printf '%s\n' "$FIRST_FIT_SIM_LAYOUTS" + else + printf '%s\n' "$SIM_LAYOUTS" + fi +} + build_case_binary() { local side=$1 local case_name=$2 @@ -303,6 +323,7 @@ build_case_binary() { feature_args=(--features candidate) fi SOLVERFORGE_BENCH_CASE="$case_name" CARGO_TARGET_DIR="$target" RUSTFLAGS="$rustflags" \ + SOLVERFORGE_BENCH_DATA_PAD=$((layout * 1984 % 4096)) \ cargo "+$TOOLCHAIN" build --manifest-path "$manifest" --release --locked \ "${feature_args[@]}" >/dev/null local binary @@ -312,7 +333,8 @@ build_case_binary() { } for case_name in "${CASES[@]}"; do - for ((layout = 0; layout < SIM_LAYOUTS; layout++)); do + case_layouts=$(sim_layouts_for_case "$case_name") + for ((layout = 0; layout < case_layouts; layout++)); do build_case_binary baseline "$case_name" "$layout" build_case_binary candidate "$case_name" "$layout" done @@ -337,7 +359,8 @@ simulate() { } running=0 for case_name in "${CASES[@]}"; do - for ((layout = 0; layout < SIM_LAYOUTS; layout++)); do + case_layouts=$(sim_layouts_for_case "$case_name") + for ((layout = 0; layout < case_layouts; layout++)); do for side in baseline candidate; do simulate "$side" "$case_name" "$layout" & running=$((running + 1)) @@ -401,7 +424,7 @@ if [[ $BENCH_ISOLATE == 1 ]]; then fi cat >"$RESULTS/environment.json" <