Skip to content

Commit f871455

Browse files
committed
Skip resolving an enclosing call that is already resolved
Resolving a call nested in an argument of another call first computes its contextual type, so that the enclosing call is resolved first, as in source order. Checking in source order has nearly always resolved or begun resolving the enclosing call by then, and computing the contextual type again only to find that cost a walk through the enclosing call for every nested call. The enclosing call is now found directly, and the contextual type only computed when it is not yet resolving. PERF-PLACEHOLDER
1 parent a354916 commit f871455

1 file changed

Lines changed: 28 additions & 15 deletions

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8829,45 +8829,48 @@ func (c *Checker) isSymbolOrSymbolForCall(node *ast.Node) bool {
88298829
return globalESSymbol == c.resolveName(left, "Symbol", ast.SymbolFlagsValue, nil /*nameNotFoundMessage*/, false /*isUse*/, false)
88308830
}
88318831

8832-
// isContextuallyTypedByEnclosingCall reports whether the contextual type of
8833-
// an expression may come from an argument of an enclosing call. Other
8834-
// contexts are not resolved ahead of the expression, since computing them
8835-
// may evaluate expressions that checking only evaluates otherwise, such as
8836-
// the target of a destructuring assignment.
8837-
func isContextuallyTypedByEnclosingCall(node *ast.Node) bool {
8832+
// getContextuallyTypingCall returns the enclosing call an argument of which
8833+
// may give an expression its contextual type, or nil. Other contexts are not
8834+
// resolved ahead of the expression, since computing them may evaluate
8835+
// expressions that checking only evaluates otherwise, such as the target of a
8836+
// destructuring assignment.
8837+
func getContextuallyTypingCall(node *ast.Node) *ast.Node {
88388838
for {
88398839
parent := node.Parent
88408840
switch parent.Kind {
88418841
case ast.KindCallExpression, ast.KindNewExpression:
8842-
return node != parent.Expression()
8842+
if node == parent.Expression() {
8843+
return nil
8844+
}
8845+
return parent
88438846
case ast.KindParenthesizedExpression, ast.KindSpreadElement, ast.KindArrayLiteralExpression,
88448847
ast.KindObjectLiteralExpression, ast.KindPropertyAssignment, ast.KindAwaitExpression:
88458848
node = parent
88468849
continue
88478850
case ast.KindConditionalExpression:
88488851
if node == parent.AsConditionalExpression().Condition {
8849-
return false
8852+
return nil
88508853
}
88518854
case ast.KindBinaryExpression:
88528855
switch parent.AsBinaryExpression().OperatorToken.Kind {
88538856
case ast.KindBarBarToken, ast.KindAmpersandAmpersandToken, ast.KindQuestionQuestionToken, ast.KindCommaToken:
88548857
if node != parent.AsBinaryExpression().Right {
8855-
return false
8858+
return nil
88568859
}
88578860
default:
8858-
return false
8861+
return nil
88598862
}
88608863
case ast.KindArrowFunction:
88618864
if node != parent.Body() {
8862-
return false
8865+
return nil
88638866
}
88648867
case ast.KindReturnStatement:
88658868
parent = ast.GetContainingFunction(parent)
88668869
if parent == nil || !ast.IsFunctionExpressionOrArrowFunction(parent) {
8867-
return false
8870+
return nil
88688871
}
88698872
default:
8870-
return false
8873+
return nil
88718874
}
88728875
node = parent
88738876
}
@@ -8916,8 +8919,10 @@ func (c *Checker) getResolvedSignatureUncached(node *ast.Node, candidatesOutArra
89168919
// type parameters this call references. Resolved from within this call
89178920
// instead, the enclosing call sees this one marked as resolving, and
89188921
// the result depends on which call was queried first.
8919-
if (ast.IsCallExpression(node) || ast.IsNewExpression(node)) && isContextuallyTypedByEnclosingCall(node) {
8920-
c.getContextualType(node, ContextFlagsNone)
8922+
if ast.IsCallExpression(node) || ast.IsNewExpression(node) {
8923+
if call := getContextuallyTypingCall(node); call != nil && !c.isSignatureResolvingOrResolved(call) {
8924+
c.getContextualType(node, ContextFlagsNone)
8925+
}
89218926
}
89228927
cached = links.resolvedSignature.get(c)
89238928
if cached == c.resolvingSignature && c.isSignatureResolutionHidden(links) {
@@ -8983,6 +8988,14 @@ type signatureResolving struct {
89838988

89848989
// isSignatureResolutionHidden reports whether the resolution of the call
89858990
// with the given links is in progress below what is visible; see yieldCycle.
8991+
// isSignatureResolvingOrResolved reports whether the signature of a call is
8992+
// resolved, or being resolved by a resolution visible to the current one.
8993+
func (c *Checker) isSignatureResolvingOrResolved(call *ast.Node) bool {
8994+
links := c.signatureLinks.Get(call)
8995+
s := links.resolvedSignature.get(c)
8996+
return s != nil && !(s == c.resolvingSignature && c.isSignatureResolutionHidden(links))
8997+
}
8998+
89868999
func (c *Checker) isSignatureResolutionHidden(links *SignatureLinks) bool {
89879000
if c.visibleStart == 0 {
89889001
return false

0 commit comments

Comments
 (0)