Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Perf research: use scope analysis to circumvent type information for some cases#6217
-
Type information is computed lazily by TS. What this means is that the more type information a rule accesses, the slower that rule is. Right now all lint rules directly use TS's APIs to ask TS to compute types before operating on those types and asking questions of those types. I wonder if we can improve the performance here by using the scope analysis information that's already calculated to get some basic types from the AST. For example: constx=1;constresult=x+1; For For example instead of doing (pseudocode):
we could potentially circumvent some work by doing something like:
It's a lot more code, for sure, but it would save us from having to dip into the types for a number of cases. Some of the more complex cases (like looking for promises, where we need to deeply inspect the object properties) could be a lot faster if we can circumvent ever asking for the type because we know it's definitely a number. It would probably be a bit awkward to use any utilities that are built here, but it could work well in conjunction with#6013 if we had our own type API surface to automate some of the short-circuiting for people. |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 1 comment 1 reply
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Hi,@bradzacher What do you think about providing fast predicates type checking utilities? exportfunctionisAny(node:TSESTree.Node,service:ParserServices):boolean{constscopeAnalysisType=getScopeAnalysisType(node);if([SCOPE_ANALYSIS_TYPE.Literal,SCOPE_ANALYSIS_TYPE.FunctionExpression,SCOPE_ANALYSIS_TYPE.ArrayExpression,//...].includes(scopeAnalysisType)){returnfalse;}consttype=services.getTypeAtLocation(node);returnisTypeFlagSet(type,ts.TypeFlags.Any);}exportfunctionisNullable(node:TSESTree.Node):boolean{if([SCOPE_ANALYSIS_TYPE.FunctionExpression,SCOPE_ANALYSIS_TYPE.ArrayExpression,//...].includes(scopeAnalysisType)){returnfalse;}consttype=ts.getType(node);returnisTypeFlagSet(type,ts.TypeFlags.Undefined|ts.TypeFlags.Null);//...}
// some-rule.tsif(isAny(node)){context.report({ ...});}else{consttype=services.getTypeAtLocation(node);// ...} In this way,
exportfunctionisPromise(node:TSESTree.Node):boolean{//constfunctionDef=/* .. */;if(functionDef.async)returntrue;consttype=ts.getType(node);//...} |
BetaWas this translation helpful?Give feedback.
All reactions
-
@JoshuaKGoldberg and I were chatting about this the other day. Theres probably some scope to do something like For example we could do something like return Theres a cost to this, ofc, and it would be a decent chunk of work to build. |
BetaWas this translation helpful?Give feedback.
All reactions
👍 2
This discussion was converted from issue #6216 on December 15, 2022 02:43.