Skip to content

Commit 8f568d4

Browse files
authored
Merge pull request #12064 from Microsoft/cacheSignatureInstantiations
Cache generic signature instantiations
2 parents ddc4ae7 + 4c1e416 commit 8f568d4

File tree

5 files changed

+9
-13
lines changed

5 files changed

+9
-13
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4232,7 +4232,7 @@ namespace ts {
42324232
for (const baseSig of baseSignatures) {
42334233
const typeParamCount = baseSig.typeParameters ? baseSig.typeParameters.length : 0;
42344234
if (typeParamCount === typeArgCount) {
4235-
const sig = typeParamCount ? getSignatureInstantiation(baseSig, typeArguments) : cloneSignature(baseSig);
4235+
const sig = typeParamCount ? createSignatureInstantiation(baseSig, typeArguments) : cloneSignature(baseSig);
42364236
sig.typeParameters = classType.localTypeParameters;
42374237
sig.resolvedReturnType = classType;
42384238
result.push(sig);
@@ -4982,6 +4982,12 @@ namespace ts {
49824982
}
49834983

49844984
function getSignatureInstantiation(signature: Signature, typeArguments: Type[]): Signature {
4985+
const instantiations = signature.instantiations || (signature.instantiations = createMap<Signature>());
4986+
const id = getTypeListId(typeArguments);
4987+
return instantiations[id] || (instantiations[id] = createSignatureInstantiation(signature, typeArguments));
4988+
}
4989+
4990+
function createSignatureInstantiation(signature: Signature, typeArguments: Type[]): Signature {
49854991
return instantiateSignature(signature, createTypeMapper(signature.typeParameters, typeArguments), /*eraseTypeParameters*/ true);
49864992
}
49874993

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2923,6 +2923,8 @@ namespace ts {
29232923
isolatedSignatureType?: ObjectType; // A manufactured type that just contains the signature for purposes of signature comparison
29242924
/* @internal */
29252925
typePredicate?: TypePredicate;
2926+
/* @internal */
2927+
instantiations?: Map<Signature>; // Generic signature instantiation cache
29262928
}
29272929

29282930
export const enum IndexKind {

tests/baselines/reference/promisePermutations.errors.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ tests/cases/compiler/promisePermutations.ts(106,19): error TS2345: Argument of t
2525
Types of parameters 'cb' and 'value' are incompatible.
2626
Type 'string' is not assignable to type '<T>(a: T) => T'.
2727
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>'.
28-
Types of parameters 'cb' and 'value' are incompatible.
29-
Type 'string' is not assignable to type '<T>(a: T) => T'.
3028
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>'.
3129
Types of parameters 'cb' and 'value' are incompatible.
3230
Type 'string' is not assignable to type '<T>(a: T) => T'.
@@ -229,8 +227,6 @@ tests/cases/compiler/promisePermutations.ts(160,21): error TS2345: Argument of t
229227
var s7a = r7.then(testFunction7, testFunction7, testFunction7); // error
230228
~~~~~~~~~~~~~
231229
!!! error TS2345: Argument of type '(cb: <T>(a: T) => T) => IPromise<string>' is not assignable to parameter of type '(value: string) => IPromise<string>'.
232-
!!! error TS2345: Types of parameters 'cb' and 'value' are incompatible.
233-
!!! error TS2345: Type 'string' is not assignable to type '<T>(a: T) => T'.
234230
var s7b = r7.then(testFunction7P, testFunction7P, testFunction7P); // error
235231
~~~~~~~~~~~~~~
236232
!!! error TS2345: Argument of type '(cb: <T>(a: T) => T) => Promise<string>' is not assignable to parameter of type '(value: string) => Promise<string>'.

tests/baselines/reference/promisePermutations2.errors.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ tests/cases/compiler/promisePermutations2.ts(105,19): error TS2345: Argument of
2525
Types of parameters 'cb' and 'value' are incompatible.
2626
Type 'string' is not assignable to type '<T>(a: T) => T'.
2727
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>'.
28-
Types of parameters 'cb' and 'value' are incompatible.
29-
Type 'string' is not assignable to type '<T>(a: T) => T'.
3028
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>'.
3129
Types of parameters 'cb' and 'value' are incompatible.
3230
Type 'string' is not assignable to type '<T>(a: T) => T'.
@@ -228,8 +226,6 @@ tests/cases/compiler/promisePermutations2.ts(159,21): error TS2345: Argument of
228226
var s7a = r7.then(testFunction7, testFunction7, testFunction7); // error
229227
~~~~~~~~~~~~~
230228
!!! error TS2345: Argument of type '(cb: <T>(a: T) => T) => IPromise<string>' is not assignable to parameter of type '(value: string) => IPromise<string>'.
231-
!!! error TS2345: Types of parameters 'cb' and 'value' are incompatible.
232-
!!! error TS2345: Type 'string' is not assignable to type '<T>(a: T) => T'.
233229
var s7b = r7.then(testFunction7P, testFunction7P, testFunction7P); // error
234230
~~~~~~~~~~~~~~
235231
!!! error TS2345: Argument of type '(cb: <T>(a: T) => T) => Promise<string>' is not assignable to parameter of type '(value: string) => Promise<string>'.

tests/baselines/reference/promisePermutations3.errors.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ tests/cases/compiler/promisePermutations3.ts(105,19): error TS2345: Argument of
2828
Types of parameters 'cb' and 'value' are incompatible.
2929
Type 'string' is not assignable to type '<T>(a: T) => T'.
3030
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>'.
31-
Types of parameters 'cb' and 'value' are incompatible.
32-
Type 'string' is not assignable to type '<T>(a: T) => T'.
3331
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>'.
3432
Types of parameters 'cb' and 'value' are incompatible.
3533
Type 'string' is not assignable to type '<T>(a: T) => T'.
@@ -240,8 +238,6 @@ tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of
240238
var s7a = r7.then(testFunction7, testFunction7, testFunction7); // error
241239
~~~~~~~~~~~~~
242240
!!! error TS2345: Argument of type '(cb: <T>(a: T) => T) => IPromise<string>' is not assignable to parameter of type '(value: string) => IPromise<string>'.
243-
!!! error TS2345: Types of parameters 'cb' and 'value' are incompatible.
244-
!!! error TS2345: Type 'string' is not assignable to type '<T>(a: T) => T'.
245241
var s7b = r7.then(testFunction7P, testFunction7P, testFunction7P); // error
246242
~~~~~~~~~~~~~~
247243
!!! error TS2345: Argument of type '(cb: <T>(a: T) => T) => Promise<string>' is not assignable to parameter of type '(value: string) => Promise<string>'.

0 commit comments

Comments
 (0)