From 7b1226611939333bc2660a47da914263bc7635f6 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sun, 8 Nov 2026 21:23:16 +0000 Subject: [PATCH 1/3] Benchmark project builds and output sizes in CI The benchmarks only replayed single bsc calls, so the build system's own work never showed. They now also build tests/tests on one worker domain (clean, no-op, leaf edit, shared edit) and measure the .cmi/.cmti/.cmt/.cmj sizes and the playground bundle; rewatch reports its workers' allocations, which the runtime's exit report leaves out, under REWATCH_REPORT_WORKER_ALLOCATIONS. Review: Why build the base's tests/tests on both sides? The base compiler builds them already, so a pull request adding tests with new syntax can't break the base measurement. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gGPsF3fHZn9rBxA7Hm Signed-off-by: Christoph Knittel --- .github/workflows/ci.yml | 12 +- rewatch-ocaml/README.md | 5 + rewatch-ocaml/process.ml | 24 ++- rewatch-ocaml/process.mli | 4 + rewatch-ocaml/rescript_ocaml.ml | 10 + rewatch-ocaml/tests/run.sh | 16 ++ scripts/compilerBenchmarks.js | 281 +++++++++++++++++++++++++++-- scripts/compilerBenchmarks.test.js | 34 ++++ 8 files changed, 363 insertions(+), 23 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 55c20df7f2..5295d3733c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -428,6 +428,12 @@ jobs: git worktree add --detach "$RUNNER_TEMP/base" HEAD^1 opam exec -- dune build --root "$RUNNER_TEMP/base" --display quiet + # For the size of the bundle only; the playground is tested elsewhere. + - name: Build the playground compilers + run: | + opam exec -- make playground-compiler + opam exec -- make -C "$RUNNER_TEMP/base" playground-compiler + - name: Measure and compare env: RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} @@ -437,12 +443,14 @@ jobs: # little, and the instructions of the runtime and Belt files by up # to 0.2%. The runtime and Belt are built there too, since the # replayed builds would otherwise write interfaces that don't - # match those of the files they don't replay yet. + # match those of the files they don't replay yet. Both build the + # base's tests/tests, which the base's compiler builds already. measure() { sudo mkdir -p /bench sudo mount --bind "$1" /bench node scripts/compilerBenchmarks.js prepare /bench - node scripts/compilerBenchmarks.js measure /bench "$2" --instructions + node scripts/compilerBenchmarks.js measure /bench "$2" --instructions \ + --project "$RUNNER_TEMP/base" --playground sudo umount /bench } measure "$RUNNER_TEMP/base" "$RUNNER_TEMP/base.json" diff --git a/rewatch-ocaml/README.md b/rewatch-ocaml/README.md index e3f25b27ea..48b47847bf 100644 --- a/rewatch-ocaml/README.md +++ b/rewatch-ocaml/README.md @@ -97,6 +97,11 @@ to choose another count: a positive integer, lowered to the machine's limit of `max(12, min(32, CPUs))`. The main domain schedules dependencies; workers run compiler requests and publish what they produce. +The runtime's allocation report at exit (`OCAMLRUNPARAM=v=0x400`) only counts +the main domain. With `REWATCH_REPORT_WORKER_ALLOCATIONS` set, the build also +prints `worker_allocated_words: `, the words its workers allocated, which +the compiler benchmarks (`scripts/compilerBenchmarks.js`) add to it. + The driver gives each request fresh compiler state (flags, warnings, caches, predefined types, identifier counters) and restores it on every exit, so requests on different domains don't interfere and repeated requests produce diff --git a/rewatch-ocaml/process.ml b/rewatch-ocaml/process.ml index 737071ccb6..4de93fb3e8 100644 --- a/rewatch-ocaml/process.ml +++ b/rewatch-ocaml/process.ml @@ -226,6 +226,20 @@ let rec worker_loop pool = memory. 8M added memory for little more speed. *) let worker_minor_heap_words = 4 * 1024 * 1024 +(* The words that finished workers allocated. The runtime's own report at exit + (OCAMLRUNPARAM=v=0x400) counts only the main domain, so the compiler + benchmarks add these to it. Allocated words are counted as the runtime + counts them: minor plus major, without the promoted ones. *) +let worker_allocated_words_total = Atomic.make 0 + +let record_worker_allocations () = + let minor, promoted, major = Gc.counters () in + ignore + (Atomic.fetch_and_add worker_allocated_words_total + (int_of_float (minor +. major -. promoted))) + +let worker_allocated_words () = Atomic.get worker_allocated_words_total + let create_worker_pool ~max_jobs notifier = let pool = { @@ -251,10 +265,12 @@ let create_worker_pool ~max_jobs notifier = before re-raising the first error. *) let run_worker () = Gc.set {(Gc.get ()) with minor_heap_size = worker_minor_heap_words}; - try worker_loop pool - with exn -> - Child.with_lock pool.mutex (fun () -> - if Option.is_none pool.worker_error then pool.worker_error <- Some exn) + (try worker_loop pool + with exn -> + Child.with_lock pool.mutex (fun () -> + if Option.is_none pool.worker_error then + pool.worker_error <- Some exn)); + record_worker_allocations () in let rec start_workers remaining = if remaining > 0 then ( diff --git a/rewatch-ocaml/process.mli b/rewatch-ocaml/process.mli index 4ba7c22b5c..e9e7a783aa 100644 --- a/rewatch-ocaml/process.mli +++ b/rewatch-ocaml/process.mli @@ -14,6 +14,10 @@ val succeeded : result -> bool val status_string : Unix.process_status -> string val default_max_jobs : int +val worker_allocated_words : unit -> int +(** The words that the worker domains of finished worker pools allocated, + which the runtime's report at exit leaves out. *) + val task : ?env:Spawn.Env.t -> ?on_result:(result -> result) -> job -> task val concurrent_task : cancel:(unit -> unit) -> diff --git a/rewatch-ocaml/rescript_ocaml.ml b/rewatch-ocaml/rescript_ocaml.ml index 0c7b37f3f5..63db448aca 100644 --- a/rewatch-ocaml/rescript_ocaml.ml +++ b/rewatch-ocaml/rescript_ocaml.ml @@ -64,8 +64,18 @@ let run = function | command -> with_termination_handlers (fun ~poll -> run_command ~poll command) +(* For the compiler benchmarks (scripts/compilerBenchmarks.js): the runtime's + report at exit only counts the main domain's allocations, and the compiler + runs on worker domains. *) +let report_worker_allocations () = + if Option.is_some (Sys.getenv_opt "REWATCH_REPORT_WORKER_ALLOCATIONS") then + at_exit (fun () -> + Printf.eprintf "worker_allocated_words: %d\n%!" + (Process.worker_allocated_words ())) + let main () = Platform.configure_standard_streams (); + report_worker_allocations (); try match Cli.eval Sys.argv with | Cli.Run command -> run command diff --git a/rewatch-ocaml/tests/run.sh b/rewatch-ocaml/tests/run.sh index 4e705fd049..7afd5b2caf 100644 --- a/rewatch-ocaml/tests/run.sh +++ b/rewatch-ocaml/tests/run.sh @@ -2380,3 +2380,19 @@ for attempt in first second; do exit 1 fi done + +# The compiler benchmarks add the words the worker domains allocated, which +# the runtime's report at exit leaves out, to that report. +worker_allocations="$work/worker-allocations" +mkdir -p "$worker_allocations/src" +printf '%s\n' \ + '{"name":"worker-allocations","sources":"src","package-specs":{"module":"esmodule","in-source":true,"suffix":".mjs"}}' \ + >"$worker_allocations/rescript.json" +printf 'let x = 1\n' >"$worker_allocations/src/A.res" +env REWATCH_REPORT_WORKER_ALLOCATIONS=1 REWATCH_COMPILER_DOMAINS=1 \ + "$port" build "$worker_allocations" >"$worker_allocations/build.log" 2>&1 +worker_words=$(sed -n 's/^worker_allocated_words: \([0-9]*\)$/\1/p' "$worker_allocations/build.log") +if ! test "${worker_words:-0}" -gt 0; then + cat "$worker_allocations/build.log" >&2 + exit 1 +fi diff --git a/scripts/compilerBenchmarks.js b/scripts/compilerBenchmarks.js index 78601fd6f0..5a3c253c6c 100644 --- a/scripts/compilerBenchmarks.js +++ b/scripts/compilerBenchmarks.js @@ -20,9 +20,12 @@ // benchmarks, since valgrind runs a program 20 to 50 times slower. This // catches slowdowns that don't allocate, such as a scan of a list that // already exists. +// - the sizes of the compiler's outputs (.cmi, .cmti, .cmt and .cmj files, +// and the playground's compiler), which are exact. // // node scripts/compilerBenchmarks.js prepare // node scripts/compilerBenchmarks.js measure [--instructions] +// [--project ] [--playground] // node scripts/compilerBenchmarks.js compare // [--threshold ] [--summary ] // @@ -35,7 +38,11 @@ // (the build never runs the formatter). With `--instructions`, the generated // inputs, the data files and the largest runtime and Belt files run under // cachegrind. The generated inputs and the data files come from the checkout -// of this script, so both measurements use the same ones. +// of this script, so both measurements use the same ones. With `--project`, +// it also builds tests/tests of the given checkout with the build system, +// clean, with nothing to do and after two edits (see measureProjectBuild); +// the builds after the clean one run under cachegrind with `--instructions`. +// With `--playground`, it measures the size of the playground's compiler. // // `compare` reports the changes between two measurements as Markdown, // improvements and regressions alike, one section per count, and exits with @@ -86,13 +93,27 @@ const instructionPackageFiles = 15; */ const cachegrindRuntimeParams = "v=0x400,d=1,s=64M"; +/** + * The runtime parameters of the builds under cachegrind: as above, but the + * build compiles on a worker domain besides its main one (d=2). The worker + * sets a minor heap of its own, so the collector runs there; both + * measurements build from the same paths, so it runs at the same points. + */ +const cachegrindBuildRuntimeParams = "v=0x400,d=2,s=64M"; + /** * `inputs` is a hash of everything besides the compiler that the counts - * depend on. `instructions` is only measured for some benchmarks. + * depend on. `instructions` is only measured for some benchmarks, and + * `bytes`, the size of an output, only for outputs. * - * @typedef {{ allocated: number, instructions?: number, inputs: string }} Measurement + * @typedef {{ + * allocated?: number, + * instructions?: number, + * bytes?: number, + * inputs: string, + * }} Measurement * @typedef {Record} Results - * @typedef {"allocated" | "instructions"} Metric + * @typedef {"allocated" | "instructions" | "bytes"} Metric */ /** @@ -104,7 +125,12 @@ const cachegrindRuntimeParams = "v=0x400,d=1,s=64M"; */ export function parseAllocatedWords(stderr) { const match = /^allocated_words: (\d+)$/m.exec(stderr); - return match ? Number(match[1]) : undefined; + if (!match) return undefined; + // The build system compiles on worker domains, whose allocations the + // runtime leaves out; it reports them itself when asked to + // (REWATCH_REPORT_WORKER_ALLOCATIONS). + const workers = /^worker_allocated_words: (\d+)$/m.exec(stderr); + return Number(match[1]) + (workers ? Number(workers[1]) : 0); } /** @@ -127,9 +153,17 @@ export function parseInstructions(stderr) { * @param {string[]} args * @param {string} cwd * @param {boolean} instructions + * @param {{ env?: Record, runtimeParams?: string }} [options] + * extra environment variables, and the runtime parameters under cachegrind * @returns {{ allocated: number, instructions?: number }} */ -function measureRun(exe, args, cwd, instructions) { +function measureRun( + exe, + args, + cwd, + instructions, + { env = {}, runtimeParams = cachegrindRuntimeParams } = {}, +) { const [command, commandArgs] = instructions ? [ "valgrind", @@ -148,7 +182,8 @@ function measureRun(exe, args, cwd, instructions) { maxBuffer: 256 * 1024 * 1024, env: { ...process.env, - OCAMLRUNPARAM: instructions ? cachegrindRuntimeParams : "v=0x400", + ...env, + OCAMLRUNPARAM: instructions ? runtimeParams : "v=0x400", }, }); const stderr = result.stderr ?? ""; @@ -356,12 +391,182 @@ export function prepare(checkout) { } } +/** + * The playground's compiler that `make playground-compiler` builds, relative + * to a checkout's root. + */ +const playgroundCompiler = path.join("packages", "playground", "compiler.js"); + +/** The interface and artifact files whose sizes are measured. */ +const outputExtensions = [".cmi", ".cmti", ".cmt", ".cmj"]; + +/** + * The files in a directory and its subdirectories, sorted. Symbolic links + * aren't followed: the projects link to the runtime, whose outputs are + * measured on their own. + * + * @param {string} dir + * @returns {string[]} + */ +function listFiles(dir) { + return fs + .readdirSync(dir, { withFileTypes: true }) + .sort((a, b) => a.name.localeCompare(b.name)) + .flatMap(entry => { + const file = path.join(dir, entry.name); + if (entry.isDirectory()) return listFiles(file); + return entry.isFile() ? [file] : []; + }); +} + +/** + * The total size of the files in a directory and its subdirectories, by + * extension. + * + * @param {string} dir + * @returns {Record} + */ +function outputSizes(dir) { + /** @type {Record} */ + const sizes = Object.fromEntries(outputExtensions.map(ext => [ext, 0])); + for (const file of listFiles(dir)) { + const ext = path.extname(file); + if (ext in sizes) sizes[ext] += fs.statSync(file).size; + } + return sizes; +} + +/** + * Copies the ReScript sources of a directory and its subdirectories, and + * nothing else: the outputs the tests check in are written by the builds. + * + * @param {string} from + * @param {string} to + */ +function copySources(from, to) { + fs.cpSync(from, to, { + recursive: true, + filter: source => + fs.statSync(source).isDirectory() || + source.endsWith(".res") || + source.endsWith(".resi"), + }); +} + +/** The project the build benchmarks build, relative to a checkout. */ +const benchmarkProject = path.join("tests", "tests"); + +/** + * The module of the project that about 200 others use, and that has no + * interface file, so that an edit changes its interface. + */ +const sharedModule = path.join("src", "test_utils.res"); + +/** + * Builds tests/tests of `projectCheckout` with the build system of + * `checkout`, on one worker domain so that the counts don't depend on how + * the work is shared: a clean build, a build with nothing to do, and the + * builds after an edit of a module that nothing uses and of one that many + * do. Adds the sizes of the outputs of the clean build. + * + * The project is copied to a directory of its own, with the same name in + * every measurement, so that no path of the checkout enters the counts. + * Both measurements build the same sources, those of the base, which its + * compiler builds already; warnings are no errors there, so that a new + * warning doesn't fail the build. + * + * @param {string} checkout + * @param {string} projectCheckout + * @param {boolean} instructions + * @param {Results} results + */ +function measureProjectBuild(checkout, projectCheckout, instructions, results) { + const rescript = path.join(checkout, relativeBinDir, "rescript.exe"); + const projectDir = path.join(projectCheckout, benchmarkProject); + const workDir = path.join(os.tmpdir(), "compiler-bench-project"); + fs.rmSync(workDir, { recursive: true, force: true }); + try { + copySources(path.join(projectDir, "src"), path.join(workDir, "src")); + const config = JSON.parse( + fs.readFileSync(path.join(projectDir, "rescript.json"), "utf8"), + ); + config["compiler-flags"] = (config["compiler-flags"] ?? []).filter( + (/** @type {string} */ flag) => !flag.startsWith("-warn-error"), + ); + fs.writeFileSync( + path.join(workDir, "rescript.json"), + `${JSON.stringify(config, null, 2)}\n`, + ); + const scope = path.join(workDir, "node_modules", "@rescript"); + const react = path.join( + projectCheckout, + "tests", + "dependencies", + "rescript-react", + ); + copySources(path.join(react, "src"), path.join(scope, "react", "src")); + fs.copyFileSync( + path.join(react, "rescript.json"), + path.join(scope, "react", "rescript.json"), + ); + fs.symlinkSync( + path.join(checkout, "packages", "@rescript", "runtime"), + path.join(scope, "runtime"), + "junction", + ); + + const sources = listFiles(workDir).filter( + file => file.endsWith(".res") || file.endsWith(".resi"), + ); + const inputs = hashFiles(sources); + // The largest test, which no other module uses. + const leafModule = sources + .filter(file => path.dirname(file) === path.join(workDir, "src")) + .filter(file => file.endsWith("_test.res")) + .map(file => ({ file, size: fs.statSync(file).size })) + .sort((a, b) => b.size - a.size || a.file.localeCompare(b.file))[0]?.file; + if (!leafModule || !fs.existsSync(path.join(workDir, sharedModule))) { + throw new Error(`The modules to edit are missing from ${projectDir}.`); + } + + /** @param {boolean} cachegrind */ + const build = cachegrind => + measureRun(rescript, ["build"], workDir, cachegrind, { + env: { + REWATCH_COMPILER_DOMAINS: "1", + REWATCH_REPORT_WORKER_ALLOCATIONS: "1", + }, + runtimeParams: cachegrindBuildRuntimeParams, + }); + /** @param {string} file */ + const edit = file => fs.appendFileSync(file, "\nlet benchmarkEdit = 1\n"); + + // The clean build runs under cachegrind for no measurement: it would + // take minutes, and the replayed builds above cover its compiler work. + results["build/clean"] = { ...build(false), inputs }; + const outputs = outputSizes(workDir); + for (const ext of outputExtensions) { + results[`output/tests/*${ext}`] = { bytes: outputs[ext], inputs }; + } + results["build/no-op"] = { ...build(instructions), inputs }; + edit(leafModule); + results["build/leaf-edit"] = { ...build(instructions), inputs }; + edit(path.join(workDir, sharedModule)); + results["build/shared-edit"] = { ...build(instructions), inputs }; + } finally { + fs.rmSync(workDir, { recursive: true, force: true }); + } +} + /** * @param {string} checkout - * @param {{ instructions: boolean }} options + * @param {{ instructions: boolean, project?: string, playground?: boolean }} options + * `project` is the checkout whose tests/tests the build benchmarks build; + * without it, there are none. `playground` adds the size of the + * playground's compiler, which must be built. * @returns {Results} */ -export function measure(checkout, { instructions }) { +export function measure(checkout, { instructions, project, playground }) { const bins = path.join(checkout, relativeBinDir); const bsc = path.join(bins, "bsc.exe"); const rescript = path.join(bins, "rescript.exe"); @@ -450,6 +655,31 @@ export function measure(checkout, { instructions }) { fs.rmSync(workDir, { recursive: true, force: true }); } + // The outputs of the runtime and Belt, which the replayed builds wrote + // again. + for (const pkg of packages) { + const outputs = outputSizes( + path.join(checkout, pkg.dir, "lib", "rescript"), + ); + for (const ext of outputExtensions) { + results[`output/${path.basename(pkg.dir)}/*${ext}`] = { + bytes: outputs[ext], + inputs: packageInputs, + }; + } + } + + if (playground) { + results["output/playground/compiler.js"] = { + bytes: fs.statSync(path.join(checkout, playgroundCompiler)).size, + inputs: "", + }; + } + + if (project) { + measureProjectBuild(checkout, project, instructions, results); + } + return results; } @@ -548,6 +778,13 @@ const metricWords = { less: (n, threshold) => `${n === 1 ? "1 benchmark executes" : `${n} benchmarks execute`} more than ${threshold}% fewer instructions`, }, + bytes: { + title: "Output size", + more: (n, threshold) => + `${n === 1 ? "1 output is" : `${n} outputs are`} more than ${threshold}% larger`, + less: (n, threshold) => + `${n === 1 ? "1 output is" : `${n} outputs are`} more than ${threshold}% smaller`, + }, }; /** @@ -681,8 +918,8 @@ export function renderSummary(comparisons, threshold, visibleRows = 10) { * @param {number} threshold */ export function compareAll(base, head, threshold) { - return /** @type {Metric[]} */ (["allocated", "instructions"]).map(metric => - compareResults(base, head, threshold, metric), + return /** @type {Metric[]} */ (["allocated", "instructions", "bytes"]).map( + metric => compareResults(base, head, threshold, metric), ); } @@ -693,13 +930,22 @@ function main(args) { prepare(path.resolve(rest[0])); return 0; } - if ( - command === "measure" && - (rest.length === 2 || (rest.length === 3 && rest[2] === "--instructions")) - ) { - const [checkout, out] = rest; + if (command === "measure" && rest.length >= 2) { + const [checkout, out, ...options] = rest; + let instructions = false; + let playground = false; + let project; + for (let i = 0; i < options.length; i++) { + if (options[i] === "--instructions") instructions = true; + else if (options[i] === "--playground") playground = true; + else if (options[i] === "--project" && i + 1 < options.length) { + project = path.resolve(options[++i]); + } else throw new Error(`Unknown option ${options[i]}`); + } const results = measure(path.resolve(checkout), { - instructions: rest.length === 3, + instructions, + project, + playground, }); fs.writeFileSync(out, `${JSON.stringify(results, null, 2)}\n`); console.log(`Measured ${Object.keys(results).length} benchmarks.`); @@ -725,6 +971,7 @@ function main(args) { console.error( "Usage: compilerBenchmarks.js prepare \n" + " compilerBenchmarks.js measure [--instructions]\n" + + " [--project ] [--playground]\n" + " compilerBenchmarks.js compare [--threshold ]\n" + " [--summary ]", ); diff --git a/scripts/compilerBenchmarks.test.js b/scripts/compilerBenchmarks.test.js index 222f8fc4c0..1e9325455c 100644 --- a/scripts/compilerBenchmarks.test.js +++ b/scripts/compilerBenchmarks.test.js @@ -28,6 +28,13 @@ test("reads the allocated words from the runtime's statistics", () => { assert.equal(parseAllocatedWords("minor_words: 1\n"), undefined); }); +test("adds the words the build system's workers allocated", () => { + assert.equal( + parseAllocatedWords(`worker_allocated_words: 1000\n${stats}`), + 629461, + ); +}); + test("reads the instructions from cachegrind's report", () => { assert.equal(parseInstructions(`${stats}${cachegrind}`), 1234567890); assert.equal(parseInstructions(stats), undefined); @@ -286,6 +293,33 @@ test("words an improvement in instructions", () => { ); }); +test("compares output sizes in a section of their own", () => { + const base = { + a: { allocated: 1000, inputs: "x" }, + "output/runtime/*.cmt": { bytes: 2000, inputs: "x" }, + "output/runtime/*.cmi": { bytes: 1000, inputs: "x" }, + }; + const head = { + a: { allocated: 1000, inputs: "x" }, + "output/runtime/*.cmt": { bytes: 2100, inputs: "x" }, + "output/runtime/*.cmi": { bytes: 1000, inputs: "x" }, + }; + const [allocated, , bytes] = compareAll(base, head, 1); + assert.equal(allocated.compared, 1); + assert.equal(bytes.compared, 2); + const summary = renderSummary(compareAll(base, head, 1), 1); + assert.match(summary, /^### Output size$/m); + assert.doesNotMatch(summary, /Instructions/); + assert.match( + summary, + /\*\*⚠️ Regression: 1 output is more than 1% larger\.\*\*/, + ); + assert.match( + summary, + /\| `output\/runtime\/\*\.cmt` \| 2,000 \| 2,100 \| \+5\.00% \|/, + ); +}); + test("folds all but the 10 largest changes away", () => { /** @type {Record} */ const base = {}; From 78a68c5e99ee5e5e63440adcbbb358927922f393 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sun, 8 Nov 2026 21:33:27 +0000 Subject: [PATCH 2/3] Count instructions for the small builds only Under cachegrind the shared-edit build took about 40 s a side and its instructions differed by 9.5% between base and head of this pull request: with two domains collecting, they depend on how the domains interleave. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gGPsF3fHZn9rBxA7Hm Signed-off-by: Christoph Knittel --- scripts/compilerBenchmarks.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/scripts/compilerBenchmarks.js b/scripts/compilerBenchmarks.js index 5a3c253c6c..e7658fa892 100644 --- a/scripts/compilerBenchmarks.js +++ b/scripts/compilerBenchmarks.js @@ -41,7 +41,7 @@ // of this script, so both measurements use the same ones. With `--project`, // it also builds tests/tests of the given checkout with the build system, // clean, with nothing to do and after two edits (see measureProjectBuild); -// the builds after the clean one run under cachegrind with `--instructions`. +// the no-op and leaf-edit builds run under cachegrind with `--instructions`. // With `--playground`, it measures the size of the playground's compiler. // // `compare` reports the changes between two measurements as Markdown, @@ -541,8 +541,11 @@ function measureProjectBuild(checkout, projectCheckout, instructions, results) { /** @param {string} file */ const edit = file => fs.appendFileSync(file, "\nlet benchmarkEdit = 1\n"); - // The clean build runs under cachegrind for no measurement: it would - // take minutes, and the replayed builds above cover its compiler work. + // Only the builds that compile little run under cachegrind. The others + // would take minutes, the replayed builds above cover their compiler + // work, and with two domains collecting, their instructions depend on + // how the domains interleave: the shared edit's differed by 9.5% in a + // pull request that didn't change what it compiles. results["build/clean"] = { ...build(false), inputs }; const outputs = outputSizes(workDir); for (const ext of outputExtensions) { @@ -552,7 +555,7 @@ function measureProjectBuild(checkout, projectCheckout, instructions, results) { edit(leafModule); results["build/leaf-edit"] = { ...build(instructions), inputs }; edit(path.join(workDir, sharedModule)); - results["build/shared-edit"] = { ...build(instructions), inputs }; + results["build/shared-edit"] = { ...build(false), inputs }; } finally { fs.rmSync(workDir, { recursive: true, force: true }); } From ded1cf367da9439fd177645e1ef515e0a32f39c1 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Mon, 9 Nov 2026 06:41:59 +0000 Subject: [PATCH 3/3] Don't alert on builds that count the main domain only A base whose build system doesn't report its workers' allocations counts only the main domain, which the PR adding the report showed as regressions of up to +7900%; those rows are now marked as having other inputs. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gGPsF3fHZn9rBxA7Hm Signed-off-by: Christoph Knittel --- scripts/compilerBenchmarks.js | 51 ++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/scripts/compilerBenchmarks.js b/scripts/compilerBenchmarks.js index e7658fa892..08f4493af7 100644 --- a/scripts/compilerBenchmarks.js +++ b/scripts/compilerBenchmarks.js @@ -155,7 +155,7 @@ export function parseInstructions(stderr) { * @param {boolean} instructions * @param {{ env?: Record, runtimeParams?: string }} [options] * extra environment variables, and the runtime parameters under cachegrind - * @returns {{ allocated: number, instructions?: number }} + * @returns {{ allocated: number, instructions?: number, workersReported?: boolean }} */ function measureRun( exe, @@ -201,9 +201,13 @@ function measureRun( `${command} ${commandArgs.join(" ")} failed in ${cwd} (${reason}):\n${stderr}`, ); } - return executed === undefined - ? { allocated } - : { allocated, instructions: executed }; + const counts = + executed === undefined + ? { allocated } + : { allocated, instructions: executed }; + return /^worker_allocated_words: /m.test(stderr) + ? { ...counts, workersReported: true } + : counts; } /** @@ -529,15 +533,30 @@ function measureProjectBuild(checkout, projectCheckout, instructions, results) { throw new Error(`The modules to edit are missing from ${projectDir}.`); } + // A build system that doesn't report its workers' allocations, such as + // the base of the pull request that added the report, counts only the + // main domain's: the counts measure something else, so they are marked + // as having other inputs and not alerted on. /** @param {boolean} cachegrind */ - const build = cachegrind => - measureRun(rescript, ["build"], workDir, cachegrind, { - env: { - REWATCH_COMPILER_DOMAINS: "1", - REWATCH_REPORT_WORKER_ALLOCATIONS: "1", + const build = cachegrind => { + const { workersReported, ...counts } = measureRun( + rescript, + ["build"], + workDir, + cachegrind, + { + env: { + REWATCH_COMPILER_DOMAINS: "1", + REWATCH_REPORT_WORKER_ALLOCATIONS: "1", + }, + runtimeParams: cachegrindBuildRuntimeParams, }, - runtimeParams: cachegrindBuildRuntimeParams, - }); + ); + return { + ...counts, + inputs: workersReported ? inputs : `${inputs}-main-domain-only`, + }; + }; /** @param {string} file */ const edit = file => fs.appendFileSync(file, "\nlet benchmarkEdit = 1\n"); @@ -546,16 +565,16 @@ function measureProjectBuild(checkout, projectCheckout, instructions, results) { // work, and with two domains collecting, their instructions depend on // how the domains interleave: the shared edit's differed by 9.5% in a // pull request that didn't change what it compiles. - results["build/clean"] = { ...build(false), inputs }; + results["build/clean"] = build(false); const outputs = outputSizes(workDir); for (const ext of outputExtensions) { results[`output/tests/*${ext}`] = { bytes: outputs[ext], inputs }; } - results["build/no-op"] = { ...build(instructions), inputs }; + results["build/no-op"] = build(instructions); edit(leafModule); - results["build/leaf-edit"] = { ...build(instructions), inputs }; + results["build/leaf-edit"] = build(instructions); edit(path.join(workDir, sharedModule)); - results["build/shared-edit"] = { ...build(false), inputs }; + results["build/shared-edit"] = build(false); } finally { fs.rmSync(workDir, { recursive: true, force: true }); } @@ -884,7 +903,7 @@ function renderSection(comparison, threshold, visibleRows) { if (listed.some(c => c.inputsChanged)) { lines.push( "", - "(inputs changed): this pull request changes runtime or Belt sources, so these benchmarks compile different code than on the base, and their changes don't count as regressions.", + "(inputs changed): this pull request changes runtime or Belt sources, or the base's build system doesn't report its workers' allocations, so these benchmarks measure something different than on the base, and their changes don't count as regressions.", ); } }