Repository navigation
perf(cli): upgrade binds jscodeshift to each parser once per codemod - #11721
Conversation
runCodeCodemod called `jscodeshift.withParser(parser)` for every file it read. Each call builds a new copy of jscodeshift's helpers (its builders, node types and template tags), and every copy is alike, so a run over a large app spent its time building copies. A run now binds each parser it needs once and hands every file of that parser the same jscodeshift. Each file's transform still gets its own parse-error marker around it.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
There was a problem hiding this comment.
Requesting changes for one test claim; the parser cache itself checks out.
run-codemod.test.mjs:91-94and the description's Testing section say the test matches a run that binds a fresh jscodeshift for every file, but that reference run goes through the samerunCodeCodemodcache and binds once per parser (2 binds for the 5 files), so the assertion compares the change with itself: build the reference from onerunCodeCodemodcall per file and compare the written files, or drop the assertion, its comment and the sentence.
The existing codemod and upgrade suites pass on this head, the new call-count assertion fails on main as described, and a true per-file run of the test's fixture writes the same files as the shared run.
Claims and files checked
- Claim: each per-file
withParsercall built a new, identical copy of jscodeshift's helpers. Verified: jscodeshift'swithParserrebuilds builders, types, templates, filters and mappings on every call. - Claim: about 150 ms saved per upgrade of a 239-file app. Consistent, not reproduced: one bind costs about 0.1 ms, and the 0.6.8 range runs about seven code codemods over every file.
- Claim:
runCodeCodemodbinds each parser once per run and hands every file of that parser the same jscodeshift. Verified:run-codemod.mjs:490-515. - Claim: each file still gets its own parse-error marker that wraps the shared jscodeshift without changing it. Verified:
run-codemod.mjs:525; the wrapper only traps calls. - Claim: output and written files do not change. Verified: the codemod and upgrade suites pass, and a per-file run of the fixture writes the same bytes.
- Claim: the timing table and the 80-run identity replay. Not rerun: the measured app is not in the repository.
- Claim: no public API, product behavior or policy change. Verified: no export, command, option or default changes.
- Claim: the new test binds each parser once and fails on the old code. Verified: it fails on
mainwith 5 binds and passes here with 2. - Claim: the new test gives the same result as a run that binds a fresh jscodeshift for every file. False: the reference run binds once per parser.
- Claim: the
assets/codemods,api/upgradeandupgradesuites pass (109 files, 1,288 tests). Verified: same counts. - Claim:
check:changesetsand Prettier pass. Verified. - Changeset: once per codemod instead of once per file, about 150 ms faster on a 240-file app, output unchanged. Verified, with the file count rounded from the 239-file measurement.
- File
packages/cli/assets/codemods/run-codemod.mjs: clear. - File
packages/cli/assets/codemods/run-codemod.test.mjs: the finding above. - File
.changeset/codemod-parser-once.md: clear. - File
scripts/vitest-node-durations.json: clear.
PR Analysis Report🔎 Preview ·
|
…inds afresh for each file
There was a problem hiding this comment.
Approved pending CI: CI has not run on this head yet. The CI workflow found no room under the CI ceiling and its required checks were cancelled, so re-run it to get their results; Lint, CLI Smoke Test and Codemod Verify passed.
The test now compares the shared-parser run with one runCodeCodemod call per file, each of which binds a fresh jscodeshift, so the earlier test finding is fixed. The bind-count assertion fails on main (5 binds) and passes here (2), and the existing codemod and upgrade suites pass.
Claims and files checked
- Claim: each per-file
withParsercall built a new, identical copy of jscodeshift's helpers. Verified: jscodeshift'swithParserbuilds a new core and copies builders, types, templates, filters and mappings onto it on every call. - Claim: that copying costs about 150 ms of each
upgraderun on a 239-file app. Consistent, not reproduced: one bind costs 0.1–0.3 ms, and the measured app is not in the repository. - Claim: every file's transform still gets a jscodeshift bound to that file's parser. Verified:
run-codemod.mjs:514-515. - Claim:
runCodeCodemodbinds each parser once per run and shares it across that parser's files. Verified:run-codemod.mjs:490-515; 2 binds for the five-file fixture. - Claim: each file keeps its own parse-error marker, which wraps the shared jscodeshift without changing it. Verified:
run-codemod.mjs:525; the wrapper only traps calls. - Claim: no difference in output or in the files
upgradewrites. Verified: the codemod and upgrade suites pass, and the shared run writes the same bytes as per-file runs. - Claim: the timing table and the 80-run identity replay. Not rerun: the measured app is not in the repository.
- Claim: no public API, product behaviour or policy change, and no internal context. Verified: no export, command, option or default changes, and the added text is clean.
- Claim: the new test binds each parser once and fails on the old code. Verified: it fails on
mainwith 5 binds and passes here with 2. - Claim: the new test writes the same files and names the same changed files as one run per file, each binding afresh. Verified: the reference makes one
runCodeCodemodcall per file (5 binds) and both assertions pass. - Claim: the codemod,
api/upgradeandupgradecommand suites pass (109 files, 1,288 tests). Verified: same counts on this head. - Claim: the full CLI suite passed at the first revision, and the second revision changes only the new test. The first part is not rerun; the second is verified.
- Claim:
check:changesetsand Prettier pass. Verified. - Changeset:
upgradebinds once per parser per codemod instead of once per file. Verified. - Changeset: about 150 ms faster on a 240-file app. Consistent with the description's 239-file measurement, not reproduced.
- Changeset: output does not change. Verified.
- File
.changeset/codemod-parser-once.md: clear. - File
packages/cli/assets/codemods/run-codemod.mjs: clear. - File
packages/cli/assets/codemods/run-codemod.test.mjs: clear; the earlier test finding is fixed. - File
scripts/vitest-node-durations.json: clear.
There was a problem hiding this comment.
Approved pending CI: CI has not run on this head yet. The CI workflow found no room under the CI ceiling and its required checks were cancelled, so re-run it to get their results; Lint, CLI Smoke Test and Codemod Verify passed.
This revision only merges main with no conflict, so the parser cache and its test are unchanged since the last review. The new test still fails against the base runner (5 binds) and passes here (2), and the codemod and upgrade suites pass.
Claims and files checked
- Claim: each per-file
withParsercall built a new, identical copy of jscodeshift's helpers. Verified: jscodeshift'swithParsercopies builders, types, templates, filters and mappings onto a new function on every call. - Claim: that copying costs about 150 ms of each
upgraderun on a 239-file app. Consistent, not reproduced: one warm bind costs about 0.09 ms and--from 0.6.8runs eight Core code codemods per file; the measured app is not in the repository. - Claim: every file's transform still gets a jscodeshift bound to that file's parser. Verified:
run-codemod.mjs:514-515. - Claim:
runCodeCodemodbinds each parser once per run and shares it across that parser's files. Verified:run-codemod.mjs:490-502; 2 binds for the five-file fixture. - Claim: each file keeps its own parse-error marker, which wraps the shared jscodeshift without changing it. Verified:
run-codemod.mjs:525; the wrapper only traps calls. - Claim: no difference in output or in the files
upgradewrites. Verified: the shared run writes the same bytes as one-file runs, and the codemod and upgrade suites pass. - Claim: the timing table and the 80-run identity replay. Not rerun: the measured app is not in the repository.
- Claim: no public API, product behaviour or policy change, and no internal context. Verified: no export, command, option or default changes, and the added text is clean.
- Claim: the new test binds each parser once and fails on the old code. Verified: it fails on the base runner with 5 binds and passes here with 2.
- Claim: the new test writes the same files and names the same changed files as one run per file, each binding afresh. Verified:
run-codemod.test.mjs:123-133. - Claim: the codemod,
api/upgradeandupgradecommand suites pass (109 files, 1,288 tests). Verified: same counts on this head. - Claim: the full CLI suite passed at the first revision, and the second revision changes only the new test. The first part is not rerun; the second is verified.
- Claim:
check:changesetsand Prettier pass. Verified. - Changeset:
upgradebinds once per parser per codemod instead of once per file. Verified. - Changeset: about 150 ms faster on a 240-file app. Consistent with the description's 239-file measurement, not reproduced.
- Changeset: output does not change. Verified.
- File
.changeset/codemod-parser-once.md: clear. - File
packages/cli/assets/codemods/run-codemod.mjs: clear. - File
packages/cli/assets/codemods/run-codemod.test.mjs: clear. - File
scripts/vitest-node-durations.json: clear.
Maintainer impact
upgradespends a large share of a run building copies of jscodeshift. For every file a code codemod reads, the runner calledjscodeshift.withParser(parser), which builds a new copy of jscodeshift's helpers (its builders, node types and template tags). Every copy is alike. Over a 239-file Next.js app, that copying (jscodeshift'senrichCore) takes about 150 ms of eachupgraderun.Intended invariant
Every
upgraderun prints the same output and leaves the same files: each file's transform gets a jscodeshift bound to the file's parser, as before.Change
assets/codemods/run-codemod.mjs:runCodeCodemodbinds each parser it needs (tsxorbabel) once per run and gives every file of that parser the same jscodeshift. Each file's transform still gets its own parse-error marker around it (markingSourceParseErrors), which wraps the shared jscodeshift without changing it.Declared difference
None in output or in the files
upgradewrites.Evidence
Wall time, warm, a fresh process per call, base and change interleaved, n=11, median of the paired difference (95% CI):
upgrade --from 0.6.8 --path app --jsonover a 239-file Next.js app: 1,844 → 1,701 ms, −154 ms [−172, −133], −7.7%upgrade --from 0.6.8 --path lib --jsonover the app's 35-filelib: 1,520 → 1,291 ms, −237 ms [−252, −215], −15%upgrade --list --json(control, scans nothing): +0.5%, CI spans 0Identity: every run above prints the same stdout and stderr and exits the same. A replay of 40
upgradeargument forms (every mix of--from0.1.5 to 0.7.5,--pathapp, src, tests, lib and.,--apply,--json,--list,--help), each run on a fresh copy of the app, 20 page templates, aliband atestsfolder, in a Core-only project and in one with a large TypeScript integration: 80 of 80 runs print the same stdout and stderr, exit the same, and leave the same files (28 of them change files).Scope
Components: CLI upgrade codemod runner
Testing
assets/codemods/run-codemod.test.mjs: a run over five files of two parsers binds each parser once (it fails on the old code, which binds once per file), and it writes the same files and names the same changed files as one run per file, each of which binds a fresh jscodeshift.assets/codemods,api/upgradeandupgradecommand suites pass (109 files, 1,288 tests).check:changesetsand Prettier pass.