Repository navigation
perf(cli): doc types and props checks reuse parsed files (TS 5.3+) - #9517
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
PR Analysis Report🔎 Preview ·
|
|
AI review status for this pull request.
|
There was a problem hiding this comment.
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
mtimethrough aDatethat 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, asread-record.test.mjsdoes, would make it test what its name says. - A
pathsread from a tsconfig setspathsBasePath, which TypeScript's key includes, so the header's note thatpathsnever 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); |
There was a problem hiding this comment.
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.
| * explicit `alwaysStrict`. Options that only resolve or check (`paths`, | ||
| * `types`, `lib`, the strict checks) do not change it. |
There was a problem hiding this comment.
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.
| if (seen != null && stamp !== '-' && seen.stamp === stamp) { | ||
| recordContentRead(fileName); | ||
| return seen.sourceFile; | ||
| } |
There was a problem hiding this comment.
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);.
There was a problem hiding this comment.
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()andintegrationCheck()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]
| '@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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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.
fcdc30d to
993c15a
Compare
There was a problem hiding this comment.
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.
993c15a to
10cf3f9
Compare
There was a problem hiding this comment.
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]
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.
validate-integration-gaps.test.mjstakes 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'sgetSourceFile.alwaysStrict. Options that only resolve or check (paths,types,lib, the strict checks) share files.getSourceFile.prop-drift.mjsgives its program a host that keeps files.doc-types.mjskeeps its per-check reuse and adds this behind it.[perf],@astryxdesign/clipatch): the kept files change published CLI code.Evidence
foundation/release/cache.test.mjscases, which also fail on main on this machine.source-file-cache.test.mjs):Scope
Testing
packages/cli(Vitest, node project), the whole project and the files above.Local gates at 3ae9da4:
pnpm lint(0 errors).Local gates at 10cf3f9 (3ae9da4's code with the changeset, as one commit), merged with main ab90555:
pnpm build, thenpnpm test:node: 16,087 passed, 76 skipped, 0 failed (3,260 files);@astryxdesign/cli, named.