Merge pull request #18279 from Microsoft/canonicalSignatures

Optimize strict generic signature checking performance
This commit is contained in:
Anders Hejlsberg 2017-09-06 21:26:39 +01:00 committed by GitHub
commit afdd9b5061
2 changed files with 28 additions and 6 deletions

View file

@ -6632,11 +6632,30 @@ namespace ts {
}
function getErasedSignature(signature: Signature): Signature {
if (!signature.typeParameters) return signature;
if (!signature.erasedSignatureCache) {
signature.erasedSignatureCache = instantiateSignature(signature, createTypeEraser(signature.typeParameters), /*eraseTypeParameters*/ true);
}
return signature.erasedSignatureCache;
return signature.typeParameters ?
signature.erasedSignatureCache || (signature.erasedSignatureCache = createErasedSignature(signature)) :
signature;
}
function createErasedSignature(signature: Signature) {
// Create an instantiation of the signature where all type arguments are the any type.
return instantiateSignature(signature, createTypeEraser(signature.typeParameters), /*eraseTypeParameters*/ true);
}
function getCanonicalSignature(signature: Signature): Signature {
return signature.typeParameters ?
signature.canonicalSignatureCache || (signature.canonicalSignatureCache = createCanonicalSignature(signature)) :
signature;
}
function createCanonicalSignature(signature: Signature) {
// Create an instantiation of the signature where each unconstrained type parameter is replaced with
// its original. When a generic class or interface is instantiated, each generic method in the class or
// interface is instantiated with a fresh set of cloned type parameters (which we need to handle scenarios
// where different generations of the same type parameter are in scope). This leads to a lot of new type
// identities, and potentially a lot of work comparing those identities, so here we create an instantiation
// that uses the original type identities for all unconstrained type parameters.
return getSignatureInstantiation(signature, map(signature.typeParameters, tp => tp.target && !getConstraintOfTypeParameter(tp.target) ? tp.target : tp));
}
function getOrCreateTypeFromSignature(signature: Signature): ObjectType {
@ -8473,7 +8492,8 @@ namespace ts {
return Ternary.False;
}
if (source.typeParameters) {
if (source.typeParameters && source.typeParameters !== target.typeParameters) {
target = getCanonicalSignature(target);
source = instantiateSignatureInContextOf(source, target, /*contextualMapper*/ undefined, compareTypes);
}

View file

@ -3447,6 +3447,8 @@ namespace ts {
/* @internal */
erasedSignatureCache?: Signature; // Erased version of signature (deferred)
/* @internal */
canonicalSignatureCache?: Signature; // Canonical version of signature (deferred)
/* @internal */
isolatedSignatureType?: ObjectType; // A manufactured type that just contains the signature for purposes of signature comparison
/* @internal */
typePredicate?: TypePredicate;