Skip to content

Commit 51dcd90

Browse files
authored
Cache mapper instantiations (#61505)
1 parent ffd98c1 commit 51dcd90

File tree

90 files changed

+5474
-81
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+5474
-81
lines changed

src/compiler/checker.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2322,6 +2322,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
23222322
var inferenceContexts: (InferenceContext | undefined)[] = [];
23232323
var inferenceContextCount = 0;
23242324

2325+
var activeTypeMappers: TypeMapper[] = [];
2326+
var activeTypeMappersCaches: Map<string, Type>[] = [];
2327+
var activeTypeMappersCount = 0;
2328+
23252329
var emptyStringType = getStringLiteralType("");
23262330
var zeroType = getNumberLiteralType(0);
23272331
var zeroBigIntType = getBigIntLiteralType({ negative: false, base10Value: "0" });
@@ -20858,10 +20862,26 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2085820862
error(currentNode, Diagnostics.Type_instantiation_is_excessively_deep_and_possibly_infinite);
2085920863
return errorType;
2086020864
}
20865+
const index = findActiveMapper(mapper);
20866+
if (index === -1) {
20867+
pushActiveMapper(mapper);
20868+
}
20869+
const key = type.id + getAliasId(aliasSymbol, aliasTypeArguments);
20870+
const mapperCache = activeTypeMappersCaches[index !== -1 ? index : activeTypeMappersCount - 1];
20871+
const cached = mapperCache.get(key);
20872+
if (cached) {
20873+
return cached;
20874+
}
2086120875
totalInstantiationCount++;
2086220876
instantiationCount++;
2086320877
instantiationDepth++;
2086420878
const result = instantiateTypeWorker(type, mapper, aliasSymbol, aliasTypeArguments);
20879+
if (index === -1) {
20880+
popActiveMapper();
20881+
}
20882+
else {
20883+
mapperCache.set(key, result);
20884+
}
2086520885
instantiationDepth--;
2086620886
return result;
2086720887
}
@@ -27452,6 +27472,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2745227472
inference.inferredType = fallbackType && context.compareTypes(fallbackType, getTypeWithThisArgument(instantiatedConstraint, fallbackType)) ? fallbackType : instantiatedConstraint;
2745327473
}
2745427474
}
27475+
clearActiveMapperCaches();
2745527476
}
2745627477

2745727478
return inference.inferredType;
@@ -32668,6 +32689,31 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3266832689
}
3266932690
}
3267032691

32692+
function pushActiveMapper(mapper: TypeMapper) {
32693+
activeTypeMappers[activeTypeMappersCount] = mapper;
32694+
activeTypeMappersCaches[activeTypeMappersCount] = new Map();
32695+
activeTypeMappersCount++;
32696+
}
32697+
32698+
function popActiveMapper() {
32699+
activeTypeMappersCount--;
32700+
}
32701+
32702+
function findActiveMapper(mapper: TypeMapper) {
32703+
for (let i = activeTypeMappersCount - 1; i >= 0; i--) {
32704+
if (mapper === activeTypeMappers[i]) {
32705+
return i;
32706+
}
32707+
}
32708+
return -1;
32709+
}
32710+
32711+
function clearActiveMapperCaches() {
32712+
for (let i = activeTypeMappersCount - 1; i >= 0; i--) {
32713+
activeTypeMappersCaches[i].clear();
32714+
}
32715+
}
32716+
3267132717
function getContextualImportAttributeType(node: ImportAttribute) {
3267232718
return getTypeOfPropertyOfContextualType(getGlobalImportAttributesType(/*reportErrors*/ false), getNameFromImportAttribute(node));
3267332719
}

tests/baselines/reference/callsOnComplexSignatures.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
=== Performance Stats ===
44
Assignability cache: 2,500
55
Type Count: 10,000
6-
Instantiation count: 100,000
6+
Instantiation count: 50,000
77
Symbol count: 50,000
88

99
=== callsOnComplexSignatures.tsx ===

tests/baselines/reference/checkJsxChildrenCanBeTupleType.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
=== Performance Stats ===
44
Assignability cache: 2,500
55
Type Count: 10,000
6-
Instantiation count: 100,000
6+
Instantiation count: 50,000
77
Symbol count: 50,000
88

99
=== checkJsxChildrenCanBeTupleType.tsx ===

tests/baselines/reference/checkJsxChildrenProperty16.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
=== Performance Stats ===
44
Assignability cache: 2,500
55
Type Count: 10,000
6-
Instantiation count: 100,000
6+
Instantiation count: 50,000
77
Symbol count: 50,000
88

99
=== checkJsxChildrenProperty16.tsx ===

tests/baselines/reference/checkJsxUnionSFXContextualTypeInferredCorrectly.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
=== Performance Stats ===
44
Assignability cache: 2,500
55
Type Count: 10,000
6-
Instantiation count: 100,000
6+
Instantiation count: 50,000
77
Symbol count: 50,000
88

99
=== checkJsxUnionSFXContextualTypeInferredCorrectly.tsx ===

tests/baselines/reference/circularBaseConstraint.types

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
//// [tests/cases/compiler/circularBaseConstraint.ts] ////
22

3-
=== Performance Stats ===
4-
Instantiation count: 2,500
5-
63
=== circularBaseConstraint.ts ===
74
// Repro from #54610
85

tests/baselines/reference/circularlySimplifyingConditionalTypesNoCrash.types

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
//// [tests/cases/compiler/circularlySimplifyingConditionalTypesNoCrash.ts] ////
22

3-
=== Performance Stats ===
4-
Instantiation count: 2,500
5-
63
=== circularlySimplifyingConditionalTypesNoCrash.ts ===
74
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
85
>Omit : Omit<T, K>

tests/baselines/reference/conditionalTypeDiscriminatingLargeUnionRegularTypeFetchingSpeedReasonable.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Strict subtype cache: 2,500
55
Assignability cache: 10,000
66
Type Count: 10,000
7-
Instantiation count: 100,000
7+
Instantiation count: 50,000
88

99
=== conditionalTypeDiscriminatingLargeUnionRegularTypeFetchingSpeedReasonable.ts ===
1010
type BigUnion =

tests/baselines/reference/conditionalTypeDoesntSpinForever.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
=== Performance Stats ===
44
Type Count: 1,000
5-
Instantiation count: 2,500 -> 5,000
5+
Instantiation count: 2,500
66

77
=== conditionalTypeDoesntSpinForever.ts ===
88
// A *self-contained* demonstration of the problem follows...

tests/baselines/reference/conditionalTypeVarianceBigArrayConstraintsPerformance.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
=== Performance Stats ===
44
Assignability cache: 1,000
55
Type Count: 5,000
6-
Instantiation count: 100,000
6+
Instantiation count: 50,000
77
Symbol count: 50,000
88

99
=== conditionalTypeVarianceBigArrayConstraintsPerformance.ts ===

0 commit comments

Comments
 (0)