Skip to content

Count the compiler's instructions with cachegrind in the benchmarks - #9254

Merged
cknitt merged 1 commit into
alloc-benchmarksfrom
instruction-benchmarks
Nov 7, 2026
Merged

cknitt merged 1 commit into
alloc-benchmarksfrom
instruction-benchmarks

Conversation

@cknitt

@cknitt cknitt commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Stacked on #9253. Adds instruction counts next to the allocated words, to catch slowdowns that don't allocate, such as a scan of a list that already exists, and makes both counts exact: two builds of the same compiler give identical numbers.

How it works

  • Runner: the benchmarks job (renamed from alloc-benchmarks) runs on ubuntu-26.04 (x64), since bsc crashes under valgrind on arm64. It uses the OPAM environment and dune cache of the first linux-x64 build job.

  • What runs under cachegrind: after installing valgrind, the job measures with --instructions. A subset runs under valgrind --tool=cachegrind --cache-sim=no, and the instructions are read from cachegrind's I refs line:

    • the 10 stress inputs;
    • the 8 parse/format runs;
    • the 15 largest runtime and Belt files, chosen by size in the PR's checkout so that both sides measure the same files.

    OCAMLRUNPARAM=v=0x400 still reports the allocated words under valgrind, so these runs give both counts. The other runtime and Belt files run natively, for allocated words only.

Why the numbers are exact

Two things made the counts of the same compiler differ between base and head:

  • The garbage collector. The checkouts' paths change the allocated words by a few. That moves the points where the collector runs, and how much of a major cycle is done before the program exits. On the first CI run that changed stress/InlineChain.res by −2.95% of its instructions, with 17 more words allocated. So the runs under cachegrind use OCAMLRUNPARAM=v=0x400,d=1,s=64M:

    • a minor heap larger than any benchmark allocates (35M words at most), so the collector doesn't run and the instructions are the compiler's own (InlineChain: 151M instead of 228M);
    • d=1, because OCaml reserves a minor heap's worth of address space per possible domain (128 by default), more than valgrind can give.

    The cost of collecting stays in the allocated words, which these settings don't change.

  • The paths. The runtime and Belt files are compiled in place with absolute paths in their arguments, which changed their instructions by up to 0.18% (JsxDOM.res), and every allocation count by a few words. So base and head are built (runtime and Belt) and measured one after the other from the same path, /bench (sudo mount --bind). The stress inputs and data files always go to the same temporary directory.

Repeated runs of the same binary differ by at most 0.003% (a temporary experiment step measured base and head twice each). With both fixes, the last CI run gave identical totals on both sides: 456,989,400 allocated words and 10,227,032,665 instructions.

More stress inputs

Four generated inputs join the six from #9253, for constructs whose compile time and allocations grow faster than the size of the input. The growth figures are from local runs, doubling the input:

Input Allocated words Doubling the size
stress/ObjectFields.res: a 400-field object, every field read 21.1M ~3.5× (quadratic)
stress/TypeAliasChain.res: 60 parameterized aliases, each on the previous one 22.5M ~7.7× (cubic)
stress/RefsInLoop.res: 300 refs updated in one loop 17.4M ~3.3×, for an input that grows linearly
stress/NestedClosures.res: 100 nested closures, each using all outer variables 11.8M ~4× (the input grows quadratically too)

Each stays below the minor heap of the runs under cachegrind, so the collector doesn't run there either.

The comment

The title is now "Compiler benchmarks", with one section per count. Each section has:

  • the total;
  • the regression and improvement headlines;
  • the table (10 largest changes, the rest folded);
  • the "(inputs changed)" note.

The title's symbol shows the worse of the two sections; otherwise only regression and improvement headlines have one. The instructions section is left out when neither measurement has instruction counts.

When a step fails before the comparison, the comment says "The benchmarks couldn't run for pull request …" with a link to the run, instead of keeping the report of an earlier commit.

Names

The benchmarks count more than allocations now, so they're named after the compiler:

  • scripts/compilerBenchmarks.js and its tests (yarn test-compiler-benchmarks);
  • tests/compiler_benchmarks/data;
  • the comment tag compiler-benchmarks.

A PR that already has the comment from #9253 gets a new one; the old one stops being updated.

For example, a slowdown that doesn't allocate:

## ⚠️ Compiler benchmarks

### Allocated words

In total: 456,989,400 → 456,989,400 (±0.00%).

None of the 257 benchmarks changed by 0.1% or more.

### Instructions

In total: 10,227,032,665 → 10,540,519,769 (+3.07%).

**⚠️ Regression: 1 benchmark executes more than 1% more instructions.**

| Benchmark | Base | Head | Change |
| --- | ---: | ---: | ---: |
| `stress/LetRec.res` | 1,431,512,896 | 1,745,000,000 | +21.90% |

Cost

The job takes about 3 minutes:

  • building the base's compiler: about 30 s, mostly from the dune cache;
  • building the runtime and Belt and measuring, both sides one after the other: about 1½ minutes.

It finishes well before build-compiler and the devcontainer job.

New tests

In scripts/allocBenchmarks.test.js:

  • parsing cachegrind's I refs line;
  • comparing instructions only where both sides have them;
  • the two-section report and the title's overall status;
  • leaving the instructions section out when nothing measured them;
  • the wording of instruction regressions and improvements.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gG86E3DicyC4NfsFGP

@cknitt
cknitt added this pull request to stack #9255 November 7, 2026 07:32
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Changelog

🏠 Internal

  • Count the instructions the compiler executes on some of the CI benchmarks with cachegrind, next to the words it allocates.

Semantic Diff

1 commit · Full page

✏️ Count the compiler's instructions with cachegrind in the benchmarks

f99df21 · cknitt + Claude · Nov 07, 2026, 09:16 UTC · edited

Allocation misses slowdowns that don't allocate, such as a scan of a list that already exists. The benchmark job now runs on x64 and also counts, with cachegrind, the instructions the compiler executes on the stress inputs, the parse and format runs and the 15 largest runtime and Belt files; the comment shows both counts in a section each.

Base and head are built and measured from the same path, and the runs under cachegrind use a minor heap larger than any benchmark allocates, so two builds of the same compiler give the same counts. Four more stress inputs cover constructs whose compile time and allocations grow faster than their size, and the script, its data and the comment are named after the compiler.

@pkg-pr-new

pkg-pr-new Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9254

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9254

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9254

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9254

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9254

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9254

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9254

commit: f99df21

@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 456,989,425 → 456,989,425 (±0.00%).

None of the 257 benchmarks changed by 0.1% or more.

Instructions

In total: 10,226,962,259 → 10,226,962,259 (±0.00%).

None of the 33 benchmarks changed by 0.1% or more.

Pull request f99df21 compared with base cfdde42 in this run.

Allocation misses slowdowns that don't allocate, such as a scan of a
list that already exists. The benchmark job now runs on x64 and also
counts, with cachegrind, the instructions the compiler executes on the
stress inputs, the parse and format runs and the 15 largest runtime and
Belt files; the comment shows both counts in a section each.

Base and head are built and measured from the same path, and the runs
under cachegrind use a minor heap larger than any benchmark allocates,
so two builds of the same compiler give the same counts. Four more
stress inputs cover constructs whose compile time and allocations grow
faster than their size, and the script, its data and the comment are
named after the compiler.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gG86E3DicyC4NfsFGP
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the instruction-benchmarks branch from e4793f2 to f99df21 Compare November 7, 2026 09:17
@cknitt
cknitt requested a review from fhammerschmidt November 7, 2026 12:28
@cknitt
cknitt merged commit bf35764 into master Nov 7, 2026
27 checks passed
@cknitt
cknitt deleted the instruction-benchmarks branch November 7, 2026 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants