From 58dc1e85c4621adbd5038d6afeebc58cdf176b2e Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 29 Sep 2019 16:13:24 -0700 Subject: [PATCH] Shared caching for identical function types in relationship checking --- src/compiler/checker.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0731ccbf04c..ba4bcbbe92d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16092,6 +16092,21 @@ namespace ts { return result; } + function isSimpleFunctionType(type: Type) { + const signature = getSingleCallSignature(type); + return signature && signature.parameters.length <= 2 && !signature.thisParameter && + !signature.hasRestParameter && !getTypePredicateOfSignature(signature); + } + + function getFunctionTypeId(type: ResolvedType): string { + const signature = type.callSignatures[0]; + let result = "" + signature.minArgumentCount; + for (const p of signature.parameters) { + result += "@" + getTypeOfSymbol(p).id + p.escapedName; + } + return result + ":" + getReturnTypeOfSignature(signature).id; + } + /** * To improve caching, the relation key for two generic types uses the target's id plus ids of the type parameters. * For other cases, the types ids are used. @@ -16106,6 +16121,9 @@ namespace ts { const typeParameters: Type[] = []; return getTypeReferenceId(source, typeParameters) + "," + getTypeReferenceId(target, typeParameters); } + if (isSimpleFunctionType(source) && isSimpleFunctionType(target)) { + return getFunctionTypeId(source) + "," + getFunctionTypeId(target); + } return source.id + "," + target.id; }