Browse documentation

Code Placement

For contributors judging whether a benchmark delta is work the code does or where the linker put it.

Executive Summary

  • Placement moves benchmarks — Where a binary's code sits changes its speed without changing its work, by up to 15% on PR micro-benchmarks for a change that only moves code (#1917)
  • Code layouts — scripts/layout/ld.bfd links a program again with its code at other addresses and the same instructions; the PR comparison runs each build over five of them (Benchmarks, ADR 0243)
  • Hardware counters — Where perf can count, the AWFY driver records instructions retired and cache misses per sample, which tell work from placement directly

Why placement matters

The time of a Goccia binary depends on where its code sits in memory as well as on what it executes (#1917). A change can move unrelated code and change its instruction-cache and branch-predictor behaviour, and two byte-identical copies of one binary can run a few percent apart depending on where the kernel puts the file's pages. A time delta of a few percent from one binary per side does not show that the change did more or less work. Adding a procedure that nothing calls near the start of Goccia.VM marked 122 of 455 PR micro-benchmarks, by up to 15%, when each build ran as one binary (ADR 0243).

Code layouts

scripts/layout/ld.bfd links a program again with its code at other addresses, without compiling anything. FPC puts each procedure in a section of its own; for each seed, the wrapper makes one procedure section, the one whose name hashes lowest with the seed, ask for a 4096-byte alignment, and keeps .text where it starts in the program as linked, so the linker pads 0 to 4095 bytes in front of that procedure. Code from there on moves and code before it stays, as when a change inserts code at one place. Each object's instruction bytes are the same, relocated operands such as call displacements take the new addresses, and the padding never runs, so a layout does the same work as the program at other addresses. The same objects with the same seed give the same binary. FPC calls it when the build passes -FDscripts/layout:

./build.pas --prod benchmarkrunner
GOCCIA_FPC_FLAGS=-FDscripts/layout GOCCIA_LAYOUT_SEEDS="1 2 3 4" \
./build.pas --prod benchmarkrunner
# build/GocciaBenchmarkRunner and build/GocciaBenchmarkRunner-layout1 to -layout4

The second command compiles only the program file again and links it five times. It needs Linux (ELF64 objects) and node. scripts/benchmark-ab.js takes a build as several binaries, --baseline and --candidate once per layout, and runs repetition r of both builds on layout r; the PR comparison runs five layouts this way. A layout of GocciaRunner (./build.pas --prod runner with the same variables) is a placement control for the AWFY driver, which takes one binary a side: main against its own layout shows how far placement alone moves each AWFY benchmark on that machine.

Hardware counters

Where the hardware counts events, the AWFY driver separates work from placement directly. Four driver options do it:

  • --counters records Linux perf_event counters for every Goccia sample, in user space: instructions retired, cycles, branch misses, L1 instruction-cache misses and iTLB misses. The report keeps them per sample and summarizes them per engine (engineStats.<engine>.counters, median/min/max) and as candidate/baseline ratios (summary.counterRatios). Instructions retired do not depend on placement: they repeat to within a few thousand per run, so their delta is the change in work. A time delta with no instruction delta and a moved L1i miss count is placement. Where perf is missing, the platform is not Linux, or perf_event_paranoid forbids user-space counting, the run goes on without counters and metadata.counters.reason says why. Five events fit in the hardware counters at once; a count perf had to multiplex is flagged multiplexed, since it is scaled rather than exact.
  • --fresh-copies runs every repetition from a new copy of each Goccia binary, so that each repetition samples a new page placement instead of measuring one.
  • --cpu <n> pins every sample to one CPU with taskset; pick one whose SMT sibling is idle, since the sibling shares the instruction cache.
  • --engine-order alternate (the default; --alternate-order is the same) reverses the engine order on every other repetition, and --warmup <n> runs unrecorded repetitions first.

scripts/awfy-comment.js turns such a report into the PR comment's table, with Instructions and L1i misses columns next to the time delta when both Goccia binaries were counted. The PR workflow runs its AWFY report with --counters, but GitHub's hosted runners cannot count hardware events, so its comment has no counter columns and its footnote says why; the numbers come from a local run on a machine that can count them.

node scripts/awfy-driver.js \
--awfy-dir /path/to/are-we-fast-yet/benchmarks/JavaScript \
--engines goccia \
--goccia-baseline build-main/GocciaRunner \
--goccia-candidate build/GocciaRunner \
--benchmark Richards:10 --benchmark Bounce:200 --benchmark Mandelbrot:500 \
--repetitions 12 --warmup 1 \
--counters --fresh-copies --alternate-order --cpu 2 \
--output tmp/ab.json
node scripts/awfy-comment.js tmp/ab.json

Read the instruction delta first. A per-opcode change that removes work shows fewer instructions in the benchmarks that run the opcode and none elsewhere; its time deltas elsewhere are placement.