perf(no-unnecessary-type-parameters): stop counting settled candidates#967
Merged
graphite-app[bot] merged 1 commit intoMay 18, 2026
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Optimizes the no-unnecessary-type-parameters rule by running the cheap AST-based filter before the expensive checker-based type-usage walk, and stopping that walk early once every remaining candidate has been proven safe.
Changes:
- Move AST reference collection /
isTypeParameterRepeatedInASTfilter ahead ofcountTypeParameterUsage, then pass only surviving candidates astargetSymbols. - Add a
remainingTargetscounter threaded throughcollectTypeParameterUsageCountsso type/signature traversal short-circuits once each target has exceeded the reportable threshold (>2 usages). - Refactor reporting loop to operate on the precomputed
candidateTypeParameterlist (node, name, symbol, references) instead of recomputing per type parameter.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
Author
Merge activity
|
#967) Improves `no-unnecessary-type-parameters` performance by avoiding the expensive recursive type-usage walk for type parameters that are already obviously safe to skip. The rule already had an AST-based fast path: ```go isTypeParameterRepeatedInAST(...) ``` but it ran after countTypeParameterUsage, which is the expensive checker-heavy part. This meant we were paying the full recursive type analysis cost even for type parameters that could be ruled out cheaply. This PR moves that cheap filtering earlier, then narrows the expensive pass to only the remaining candidates. ## Why This Is Faster The slow path is countTypeParameterUsage, which recursively walks TypeScript types and calls into the checker. On large projects like VS Code, most generic declarations are not reportable because their type parameters are used more than once in the signature. Before this change, the rule did this for every generic declaration: 1. recursively count type parameter usage with checker types 2. collect AST references 3. skip if the AST already proves the type parameter is repeated That order wastes work. If the AST can prove the type parameter appears multiple times before the function body, then the rule will not report it, so there is no reason to do the recursive checker walk first. After this change, the rule does this instead: 1. collect AST references 2. keep only type parameters that are still possible reports 3. recursively count usage only for those candidates 4. stop counting once every candidate has exceeded the reportable threshold This keeps the same behavior, but avoids a lot of type-checker and map work on declarations that are never going to produce diagnostics. ## Benchmark Measured with a shallow clone of microsoft/vscode, using 6,187 src/**/*.ts(x) files through local tsgolint headless. Environment: - rule: no-unnecessary-type-parameters Hyperfine, 3 runs: | Binary | Mean | User | System | | --- | ---: | ---: | ---: | | baseline | 11.333s ± 0.061s | 43.635s | 0.992s | | patched | 4.002s ± 0.078s | 14.775s | 0.570s | That is a 2.83x speedup for this isolated rule workload. fixes #961
8a576a5 to
bb8886b
Compare
Member
|
Thank you!! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Improves
no-unnecessary-type-parametersperformance by avoiding the expensive recursive type-usage walk for type parameters that are already obviously safe to skip.The rule already had an AST-based fast path:
but it ran after countTypeParameterUsage, which is the expensive checker-heavy part. This meant we were paying the full recursive type analysis cost even for type parameters that could be ruled out cheaply.
This PR moves that cheap filtering earlier, then narrows the expensive pass to only the remaining candidates.
Why This Is Faster
The slow path is countTypeParameterUsage, which recursively walks TypeScript types and calls into the checker. On large projects like VS Code, most generic declarations are not reportable because their type parameters are used more than once in the signature.
Before this change, the rule did this for every generic declaration:
That order wastes work. If the AST can prove the type parameter appears multiple times before the function body, then the rule will not report it, so there is no reason to do the recursive checker walk first.
After this change, the rule does this instead:
This keeps the same behavior, but avoids a lot of type-checker and map work on declarations that are never going to produce diagnostics.
Benchmark
Measured with a shallow clone of microsoft/vscode, using 6,187 src/**/*.ts(x) files through local tsgolint headless.
Environment:
Hyperfine, 3 runs:
That is a 2.83x speedup for this isolated rule workload.
fixes #961