#!/usr/bin/env node // corpus builder. run once to (re)generate the frozen benchmark corpus. // // the corpus is checked in so every arm of every A/B measures byte-identical // input. regenerating it invalidates comparisons against older reports, which // is why the manifest records a content hash per file and the A/B runner // refuses to compare reports built from different corpus hashes. // // four families, each answering a different question: // // micro/ one short snippet per language, 200-900 bytes. the docs-site // case: many tiny highlights where fixed per-call cost // (buffer allocation, pipeline setup, closure dispatch) // dominates and the scanning loop barely runs. // // fixtures/ concatenation of a language's own test fixtures. grammar // feature dense: probe paths, escapes, edge cases. these are // the cold paths a "make the common case fast" change is most // likely to silently break or slow down. // // real/ production shaped source. sourced from this repo where the // language is one we actually write here, hand authored under // corpus/seed/ otherwise. these carry the hot path weighting a // real consumer sees, and are the headline numbers. // // scale/ ~200KB per language, built by cycling that language's // distinct files. deliberately synthetic: it answers "how does // cost grow with input length" (allocation, superlinear // scans, table pressure), not "how fast is real code". do not // quote scale numbers as user-facing wins. // // sized/ the same language at ~1KB, ~10KB and ~100KB. the other // families each fix a shape and vary the language; this one // fixes the language and varies the size, which is the axis a // reader of the published comparison charts actually cares // about ("is it still fast on a big file?"). built by cycling // whole units, so the same synthetic caveat as scale/ applies // to the two upper tiers. // // upstream/ sample files vendored from shikijs/textmate-grammars-themes, // which is where shiki's own benchmark gets its inputs. we did // not choose these files, which is the entire point: it is the // one family that cannot have been selected to flatter us. // fetched by bin/fetch-upstream-corpus.mjs, pinned to a commit. // // --only rebuilds just those languages and keeps every other entry byte for byte // so a new language does not move the inputs earlier published runs measured import { createHash } from "node:crypto"; import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync, } from "node:fs"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; const here = dirname(fileURLToPath(import.meta.url)); const perf_dir = resolve(here, ".."); const repo_root = resolve(perf_dir, "../../.."); const corpus_dir = join(perf_dir, "corpus"); const seed_dir = join(corpus_dir, "seed"); const argv = process.argv.slice(2); const only_at = argv.indexOf("--only"); const ONLY = only_at >= 0 ? new Set((argv[only_at + 1] ?? "").split(",").filter(Boolean)) : null; if (ONLY?.size === 0) throw new Error("--only needs a comma separated list of languages"); // language -> extension used for the generated fixture bundle. the fixture // inputs themselves are detected by "not a .js file", which holds for every // language's test directory convention (some use `.output.js` next to // `.`, the js family uses `.js` next to `.txt`). const FIXTURE_EXT = { bash: "sh", c: "c", cpp: "cpp", css: "css", diff: "diff", "diff-basic": "txt", dockerfile: "dockerfile", dotenv: "env", go: "go", graphql: "graphql", html: "html", http: "http", ini: "ini", javascript: "js", json: "json", jsonc: "jsonc", markdown: "md", powershell: "ps1", python: "py", rust: "rs", shellsession: "sh-session", sql: "sql", svelte: "svelte", toml: "toml", tsx: "tsx", typescript: "ts", whitespace: "txt", yaml: "yaml", }; // an entry whose sources have since been deleted from the repo is FROZEN: its // last generated output under corpus/real/ is carried forward verbatim, so a // rebuild neither drops the workload nor changes its bytes. const FROZEN = "frozen"; // real-world sources pulled from this repo. [lang, out_name, [paths...]]. // concatenated in order. paths are repo relative. const REPO_REAL = [ [ "typescript", "core-runtime.ts", ["lib/core/src/tokenizer.ts", "lib/core/src/scan.ts", "lib/core/src/compiler.ts"], ], [ "typescript", "core-frames.ts", [ "lib/core/src/frame_track.ts", "lib/core/src/fidelity.ts", "lib/core/src/matched_bracket.ts", "lib/core/src/compound_compose.ts", ], ], ["typescript", "core-types.ts", ["lib/core/src/types.ts"]], ["javascript", "bench-suite.js", FROZEN], ["markdown", "architecture.md", FROZEN], ["markdown", "grammar-docs.md", FROZEN], ["json", "compiled-grammar.json", FROZEN], [ "css", "site.css", [ "lib/_site/src/app.css", "lib/_site/src/lib/styles/docs.css", "lib/_site/src/lib/styles/explore.css", ], ], ["yaml", "ci.yaml", [".github/workflows/ci-tests.yml", ".github/workflows/release.yml"]], ["html", "app.html", ["lib/_site/src/app.html"]], ["svelte", "site-inspectorpanel.svelte", FROZEN], ["svelte", "site-codepanel.svelte", FROZEN], ]; function sha256(text) { return createHash("sha256").update(text).digest("hex").slice(0, 16); } function read_if(path) { const full = join(repo_root, path); if (!existsSync(full)) { console.warn(` skip (missing): ${path}`); return null; } return readFileSync(full, "utf8"); } // concatenating svelte components would produce a file with several