Skip to content

perf(cli): doc types and props checks reuse parsed files (TS 5.3+) - #9517

Merged
cixzhang merged 1 commit into
meta:mainfrom
vjeux:perf/cli-source-file-cache
Nov 3, 2026
Merged

cixzhang merged 1 commit into
meta:mainfrom
vjeux:perf/cli-source-file-cache

Conversation

@vjeux

@vjeux vjeux commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Maintainer impact

The CLI's props comparison and doc types check spent about 1 s of TypeScript on every check re-parsing the same declarations.

  • Each check built a program, and each program parsed the TypeScript library's declarations, React's, and the authoring types again.
  • A process that checks more than once (a test run, a long-running caller, a check over several roots) paid for those parses every time. For example, validate-integration-gaps.test.mjs takes 78 to 91 s for 108 cases, pinned.

Intended invariant

No behavior change. A process parses each declaration file once for every program whose options parse and bind it alike, and parses it again as soon as the file changes. Results are unchanged.

Change

  • foundation/discovery/source-file-cache.mjs: keepSourceFiles(ts, host, options) wraps a compiler host's getSourceFile.
    • Key: what parses and binds a file alike: the key TypeScript's language service shares parsed files by (its document registry's), plus whether a script binds in strict mode, which that key reads only from an explicit alwaysStrict. Options that only resolve or check (paths, types, lib, the strict checks) share files.
    • Freshness: a file is kept while its stamp is the same (size, modification time, change time, inode, so a rewrite at the same size and time is parsed again).
    • Reads: a kept file still counts as read by the running operation, as a parse would.
    • Bounds: at most 8 keys of 5,000 files each, least recently used first.
    • TypeScript version: from 5.3, a program keeps its module resolutions itself rather than on the files it shares, which is what makes sharing them safe. An older TypeScript gets the host's own getSourceFile.
  • prop-drift.mjs gives its program a host that keeps files. doc-types.mjs keeps its per-check reuse and adds this behind it.
  • Changeset ([perf], @astryxdesign/cli patch): the kept files change published CLI code.

Evidence

  • Same time, pinned to two cores, 1 worker each, in two worktrees at the same base, two rounds:
file main this branch
validate-integration-gaps 78.0 s, 87.5 s 38.3 s, 35.1 s
authoring-checks 17.2 s, 20.4 s 10.9 s, 14.8 s
doc-types 9.6 s, 11.5 s 3.0 s, 3.0 s
prop-drift 12.2 s, 15.0 s 4.6 s, 4.3 s
integration-export-name 24.5 s, 33.4 s 22.8 s, 26.7 s
  • Every case passes on both sides.
  • The whole CLI node project, same time, 6 workers each: main passes 9,612; this branch passes 9,619, the extra 7 being the new unit cases. Both sides fail the same two foundation/release/cache.test.mjs cases, which also fail on main on this machine.
  • New unit cases (source-file-cache.test.mjs):
    • one parse for every host with the same options while the file is unchanged;
    • a fresh parse after a rewrite at the same size and modification time;
    • options that only resolve share files, while JSX and strict mode keep them apart;
    • a kept file counts as read;
    • a TypeScript older than 5.3 parses as before;
    • a program over kept files reads the same types as one over fresh parses, twice.

Scope

  • No intended public API, product behavior, visual direction, or policy change.
  • Product changes discovered during the work were removed or split.
  • Public text and artifacts contain no internal Meta context.

Testing

packages/cli (Vitest, node project), the whole project and the files above.

Local gates at 3ae9da4:

  • the CLI node project;
  • the CLI typecheck;
  • pnpm lint (0 errors).

Local gates at 10cf3f9 (3ae9da4's code with the changeset, as one commit), merged with main ab90555:

  • pnpm build, then pnpm test:node: 16,087 passed, 76 skipped, 0 failed (3,260 files);
  • changeset coverage: ships to @astryxdesign/cli, named.

@vercel

vercel Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Nov 3, 2026 11:21am UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 2, 2026
@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · 10cf3f9

Storybook · Sandbox · built from this PR's head

Package size: @astryxdesign/cli 3.30 MB packed (+2.2 kB); no new dependencies.

API changes: none in this pull request

Committed API report behind this build: core 5 rows; run pnpm api:report.

Not run on this PR: add ci:run-visual, ci:run-a11y or ci:run-rtl to run them.


Generated by PR Enrichment workflow | View full report

@astracat-bot

astracat-bot Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 3, 2026, 11:30 AM UTC

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to merge. The cache is bounded, version-gated and stamp-checked. The existing prop, doc-type and integration-check suites pass with it, and the new cases cover sharing, re-parsing and the TypeScript 5.3 gate. A few optional follow-ups:

  • The rewrite case restores mtime through a Date that rounds it to the millisecond, so it would also pass with a size-and-mtime stamp; pinning a time and replacing the file through a rename, as read-record.test.mjs does, would make it test what its name says.
  • A paths read from a tsconfig sets pathsBasePath, which TypeScript's key includes, so the header's note that paths never changes the key is slightly off.
  • A long-running process now keeps about 171 MB per options key (measured over a 675-file program) for its lifetime, which may be worth stating beside the 8-key bound.

[Automated review]

const before = keep(file, ts.ScriptTarget.ES2022);
const {atime, mtime} = fs.statSync(file);
fs.writeFileSync(file, 'export interface Props {name: string}\n');
fs.utimesSync(file, atime, mtime);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: utimesSync with the Date from statSync rounds mtime to the millisecond, so the restored time is not the original, and this case would also pass with a stamp of size and mtime alone. Pinning a whole-second time before the first parse and replacing the file through a rename with that same time, as read-record.test.mjs does, keeps the modification time truly equal and the result deterministic on filesystems with coarse change times.

Comment on lines +47 to +48
* explicit `alwaysStrict`. Options that only resolve or check (`paths`,
* `types`, `lib`, the strict checks) do not change it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: inline paths share, but a paths read from a tsconfig sets pathsBasePath, which getKeyForCompilationSettings appends to the key. Configs in different folders therefore never share. Worth saying so here.

Comment on lines +121 to +124
if (seen != null && stamp !== '-' && seen.stamp === stamp) {
recordContentRead(fileName);
return seen.sourceFile;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: a hit leaves the entry where it was. Past MAX_FILES, the first-parsed files (the TypeScript library, which every program reads) are evicted first. If least recently used is the intent, as the description says, re-insert on a hit: byFile.delete(key); byFile.set(key, seen);.

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One change is needed before this can merge; the cache itself looks sound, results are identical with and without it, and the prop, doc-type and integration-check suites pass with it.

  • The changeset says these functions "parse each TypeScript declaration file once per process", but projects on TypeScript 5.0 to 5.2, which the CLI supports, parse exactly as before, and files are shared only between checks that parse them the same way, so please scope it, for example: "With TypeScript 5.3 or later, validateIntegration(), integrationComponentConflicts() and integrationCheck() from @astryxdesign/cli/api, and the commands that run them, reuse each TypeScript file a check parsed for later checks in the same process that parse it the same way, while the file is unchanged, instead of parsing it again for every check. Their results are unchanged."

Optional: on TypeScript 6 the strict-mode part of the key splits strict: false from strict: true although TypeScript 6 binds both in strict mode, so preferring ts.getAlwaysStrict(options) when it exists would let those packages share. The three earlier non-blocking notes (the freshness case's mtime, pathsBasePath in the header, and eviction order) still apply.

[Automated review]

Comment thread .changeset/cli-source-files-kept.md Outdated
'@astryxdesign/cli': patch
---

[perf] `validateIntegration()`, `integrationComponentConflicts()` and `integrationCheck()` from `@astryxdesign/cli/api`, and the commands that run them, parse each TypeScript declaration file once per process while it is unchanged, instead of once for every program a check builds. Their results are unchanged.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking: this promises once-per-process parsing for every TypeScript the CLI supports, but TypeScript 5.0 to 5.2 get the host's own getSourceFile (source-file-cache.mjs:91), files are kept only for checks that parse them the same way, and the doc-types check already parsed once per check rather than once per program. Suggested wording:

[perf] With TypeScript 5.3 or later, `validateIntegration()`, `integrationComponentConflicts()` and `integrationCheck()` from `@astryxdesign/cli/api`, and the commands that run them, reuse each TypeScript file a check parsed for later checks in the same process that parse it the same way, while the file is unchanged, instead of parsing it again for every check. Their results are unchanged.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 993c15a with your wording: the changeset now starts "With TypeScript 5.3 or later" and promises reuse only for later checks that parse a file the same way. The optional getAlwaysStrict key for TypeScript 6 and the three earlier non-blocking notes are left for a follow-up.

}
const strict =
typeof ts.getStrictOptionValue === 'function'
? ts.getStrictOptionValue(options, 'alwaysStrict')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: on TypeScript 6 the binder decides strict mode with getAlwaysStrict (alwaysStrict !== false), so a strict: false script still binds in strict mode, but this bit gives it its own key and such packages never share with the doc-types check. Preferring ts.getAlwaysStrict(options) when it exists keeps TypeScript 5's rule and shares correctly on 6; the strict: false unit case would then need to test the TypeScript 5 rule on its own.

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for rewording the changeset; it now matches the change. The cache itself still looks sound: results are identical with and without it, and the prop, doc-type and integration-check suites pass. One change is needed before this can merge:

  • The pull request title, which becomes the commit subject on main, still says the checks "parse each declaration once per process", but TypeScript 5.0 to 5.2 parse exactly as before and files are shared only between checks that parse them the same way, so please retitle it, for example "perf(cli): the props and doc types checks reuse parsed files across checks (TypeScript 5.3+)".

The earlier notes (the TypeScript 6 strict-mode key, pathsBasePath in the header, the freshness case's mtime, and eviction order) remain optional.

[Automated review]

The props comparison and the doc types check built a program for every
check, and each program parsed the same declarations again: the
TypeScript library's, React's, the authoring types. A process that
checks more than once (a test run, a long-running caller, a check over
several roots) paid for those parses every time: about 1 s of TypeScript
per check.

`keepSourceFiles` keeps each parsed file while its stamp is the same,
for every program whose options parse and bind it alike: the key
TypeScript's language service shares parsed files by, plus whether a
script binds in strict mode. Options that only resolve or check share
them. A kept file still counts as read by the running operation. From
TypeScript 5.3 a program keeps its module resolutions itself rather than
on the parsed files, which is what makes sharing them safe; an older
TypeScript parses as before. At most 8 keys of 5,000 files each are
kept.

No behavior change.
@vjeux
vjeux force-pushed the perf/cli-source-file-cache branch from 993c15a to 10cf3f9 Compare November 3, 2026 11:13
@vjeux vjeux changed the title perf(cli): the props and doc types checks parse each declaration once per process perf(cli): doc types and props checks reuse parsed files (TS 5.3+) Nov 3, 2026

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks ready to merge once it leaves draft. The retitle fixes the last blocking point: the title and the changeset now say the reuse applies from TypeScript 5.3 and only between checks that parse a file the same way. The cache itself looks sound: integrationCheck results are identical with and without it across several packages and repeats, and the prop, doc-type and integration-check suites pass.

The earlier optional notes still apply if you want a follow-up: preferring ts.getAlwaysStrict(options) on TypeScript 6 so strict: false packages share, noting that a tsconfig paths (through pathsBasePath) does change the key, making the rewrite case keep the modification time exactly equal, and moving an entry on a hit if eviction is meant to be least recently used.

[Automated review]

@vjeux
vjeux marked this pull request as ready for review November 3, 2026 11:35
@cixzhang cixzhang added the ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) label Nov 3, 2026
@cixzhang
cixzhang marked this pull request as draft November 3, 2026 11:39
@cixzhang
cixzhang marked this pull request as ready for review November 3, 2026 11:39
@cixzhang
cixzhang merged commit 4931743 into meta:main Nov 3, 2026
139 of 144 checks passed
@vjeux
vjeux deleted the perf/cli-source-file-cache branch November 3, 2026 12:07

This branch was successfully deployed

1 active deployment
Preview — 10cf3f90 Deployed Nov 3, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants