Skip to content

Commit

Permalink
Merge pull request #12064 from Microsoft/cacheSignatureInstantiations
Browse files Browse the repository at this point in the history
Cache generic signature instantiations
  • Loading branch information
ahejlsberg authored Nov 8, 2016
2 parents ddc4ae7 + 4c1e416 commit 8f568d4
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 13 deletions.
8 changes: 7 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4232,7 +4232,7 @@ namespace ts {
for (const baseSig of baseSignatures) {
const typeParamCount = baseSig.typeParameters ? baseSig.typeParameters.length : 0;
if (typeParamCount === typeArgCount) {
const sig = typeParamCount ? getSignatureInstantiation(baseSig, typeArguments) : cloneSignature(baseSig);
const sig = typeParamCount ? createSignatureInstantiation(baseSig, typeArguments) : cloneSignature(baseSig);
sig.typeParameters = classType.localTypeParameters;
sig.resolvedReturnType = classType;
result.push(sig);
Expand Down Expand Up @@ -4982,6 +4982,12 @@ namespace ts {
}

function getSignatureInstantiation(signature: Signature, typeArguments: Type[]): Signature {
const instantiations = signature.instantiations || (signature.instantiations = createMap<Signature>());
const id = getTypeListId(typeArguments);
return instantiations[id] || (instantiations[id] = createSignatureInstantiation(signature, typeArguments));
}

function createSignatureInstantiation(signature: Signature, typeArguments: Type[]): Signature {
return instantiateSignature(signature, createTypeMapper(signature.typeParameters, typeArguments), /*eraseTypeParameters*/ true);
}

Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2923,6 +2923,8 @@ namespace ts {
isolatedSignatureType?: ObjectType; // A manufactured type that just contains the signature for purposes of signature comparison
/* @internal */
typePredicate?: TypePredicate;
/* @internal */
instantiations?: Map<Signature>; // Generic signature instantiation cache
}

export const enum IndexKind {
Expand Down
4 changes: 0 additions & 4 deletions tests/baselines/reference/promisePermutations.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ tests/cases/compiler/promisePermutations.ts(106,19): error TS2345: Argument of t
Types of parameters 'cb' and 'value' are incompatible.
Type 'string' is not assignable to type '<T>(a: T) => T'.
tests/cases/compiler/promisePermutations.ts(109,19): error TS2345: Argument of type '(cb: <T>(a: T) => T) => IPromise<string>' is not assignable to parameter of type '(value: string) => IPromise<string>'.
Types of parameters 'cb' and 'value' are incompatible.
Type 'string' is not assignable to type '<T>(a: T) => T'.
tests/cases/compiler/promisePermutations.ts(110,19): error TS2345: Argument of type '(cb: <T>(a: T) => T) => Promise<string>' is not assignable to parameter of type '(value: string) => Promise<string>'.
Types of parameters 'cb' and 'value' are incompatible.
Type 'string' is not assignable to type '<T>(a: T) => T'.
Expand Down Expand Up @@ -229,8 +227,6 @@ tests/cases/compiler/promisePermutations.ts(160,21): error TS2345: Argument of t
var s7a = r7.then(testFunction7, testFunction7, testFunction7); // error
~~~~~~~~~~~~~
!!! error TS2345: Argument of type '(cb: <T>(a: T) => T) => IPromise<string>' is not assignable to parameter of type '(value: string) => IPromise<string>'.
!!! error TS2345: Types of parameters 'cb' and 'value' are incompatible.
!!! error TS2345: Type 'string' is not assignable to type '<T>(a: T) => T'.
var s7b = r7.then(testFunction7P, testFunction7P, testFunction7P); // error
~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '(cb: <T>(a: T) => T) => Promise<string>' is not assignable to parameter of type '(value: string) => Promise<string>'.
Expand Down
4 changes: 0 additions & 4 deletions tests/baselines/reference/promisePermutations2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ tests/cases/compiler/promisePermutations2.ts(105,19): error TS2345: Argument of
Types of parameters 'cb' and 'value' are incompatible.
Type 'string' is not assignable to type '<T>(a: T) => T'.
tests/cases/compiler/promisePermutations2.ts(108,19): error TS2345: Argument of type '(cb: <T>(a: T) => T) => IPromise<string>' is not assignable to parameter of type '(value: string) => IPromise<string>'.
Types of parameters 'cb' and 'value' are incompatible.
Type 'string' is not assignable to type '<T>(a: T) => T'.
tests/cases/compiler/promisePermutations2.ts(109,19): error TS2345: Argument of type '(cb: <T>(a: T) => T) => Promise<string>' is not assignable to parameter of type '(value: string) => Promise<string>'.
Types of parameters 'cb' and 'value' are incompatible.
Type 'string' is not assignable to type '<T>(a: T) => T'.
Expand Down Expand Up @@ -228,8 +226,6 @@ tests/cases/compiler/promisePermutations2.ts(159,21): error TS2345: Argument of
var s7a = r7.then(testFunction7, testFunction7, testFunction7); // error
~~~~~~~~~~~~~
!!! error TS2345: Argument of type '(cb: <T>(a: T) => T) => IPromise<string>' is not assignable to parameter of type '(value: string) => IPromise<string>'.
!!! error TS2345: Types of parameters 'cb' and 'value' are incompatible.
!!! error TS2345: Type 'string' is not assignable to type '<T>(a: T) => T'.
var s7b = r7.then(testFunction7P, testFunction7P, testFunction7P); // error
~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '(cb: <T>(a: T) => T) => Promise<string>' is not assignable to parameter of type '(value: string) => Promise<string>'.
Expand Down
4 changes: 0 additions & 4 deletions tests/baselines/reference/promisePermutations3.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ tests/cases/compiler/promisePermutations3.ts(105,19): error TS2345: Argument of
Types of parameters 'cb' and 'value' are incompatible.
Type 'string' is not assignable to type '<T>(a: T) => T'.
tests/cases/compiler/promisePermutations3.ts(108,19): error TS2345: Argument of type '(cb: <T>(a: T) => T) => IPromise<string>' is not assignable to parameter of type '(value: string) => IPromise<string>'.
Types of parameters 'cb' and 'value' are incompatible.
Type 'string' is not assignable to type '<T>(a: T) => T'.
tests/cases/compiler/promisePermutations3.ts(109,19): error TS2345: Argument of type '(cb: <T>(a: T) => T) => Promise<string>' is not assignable to parameter of type '(value: string) => Promise<string>'.
Types of parameters 'cb' and 'value' are incompatible.
Type 'string' is not assignable to type '<T>(a: T) => T'.
Expand Down Expand Up @@ -240,8 +238,6 @@ tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of
var s7a = r7.then(testFunction7, testFunction7, testFunction7); // error
~~~~~~~~~~~~~
!!! error TS2345: Argument of type '(cb: <T>(a: T) => T) => IPromise<string>' is not assignable to parameter of type '(value: string) => IPromise<string>'.
!!! error TS2345: Types of parameters 'cb' and 'value' are incompatible.
!!! error TS2345: Type 'string' is not assignable to type '<T>(a: T) => T'.
var s7b = r7.then(testFunction7P, testFunction7P, testFunction7P); // error
~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '(cb: <T>(a: T) => T) => Promise<string>' is not assignable to parameter of type '(value: string) => Promise<string>'.
Expand Down

0 comments on commit 8f568d4

Please sign in to comment.