Skip to content

Optimize strict generic signature checking performance #18279

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

Merged
merged 2 commits into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down