-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Speed up type checking by explicitly annotating some type parameter variances #5168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Speed up type checking by explicitly annotating some type parameter variances #5168
Conversation
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 2ea835e:
|
✅ Deploy Preview for redux-starter-kit-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
.... huh. wow. this is already clearly over my head :) First question is what this does to our existing type-tests, then we can go from there. Either way, though: regardless of whether this works or not and whether we merge it, thank you for putting in serious investigation time, trying to optimize perf here, and filing this PR! |
|
well nothing obviously broke :) that's either a very good sign, or a sign that we're not testing things sufficiently :) @akaltar can you use the PR build to do some revised perf comparisons? Also, I'd love to see before and after flamegraphs! |
|
regarding types vs interfaces, see #4467 - we found that usage of interfaces created type portability issues for the types listed. |
|
Would it be against the project conventions to export a couple of these types? If not, I'll just convert them back to type aliases. Interfaces can have much better caching characteristics according to the TypeScript performance guide, hence why I've changed them, but they might not be strictly necessary for most of the optimizations here to work, so I'll check. Also while initially developing I was manually keeping the type files in sync, and I was seeing much better results, so I'm doing a couple more passes to see how good I can get it, while also testing in a production app to see more real-world results |
|
We try not to expose them to userland, as they are not guaranteed to be stable. // index.ts
import {UnportableType} from "./foo"
export declare namespace InternalDoNotUse {
export {UnportableType}
} |
I'd rather try this if it's an acceptable solution, as for example |
…named RTKInternalDoNotUse




Why
In certain cases, type inference could be slow and reach typescript depth limits. Eg #3214
How
By using typescript performance tracing on some examples from the above issue, I've manged to track down the slowness to checking the variances of various type parameters.
I used TypeSlayer's Type search to find the types where the variance was checked, and using the result data from the performance trace, I filled in the variances.
Since not all types of variance can be marked explicitly, I skipped some of them or added a different specifier. Eg
infor bivariant.I'm not 100% sure if this is correct, but many combinations produced type errors in various places, so based on my current understanding a wrong variance would cause type errors elsewhere.
I'm also not sure if this catches all common occurrences of this issue as I was using very simple examples. I will test for robustness + effectiveness by running the changes through a proprietary project and reporting the results.
I'm also not sure if this is the ideal way to solve this problem, I'd assume that there are other simplifications one could make to the types that would result in the built-in variance checking to get faster, but I couldn't find those and don't know enough about what's slow in the current version to do that
I think this could be a breaking change for some users:
What
EndpointBuilderfrom a type alias to an interfaceinoutorin outvariance specifiers to some type properties