Skip to content

Cache mapper instantiations #61505

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2316,6 +2316,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
var inferenceContexts: (InferenceContext | undefined)[] = [];
var inferenceContextCount = 0;

var activeTypeMappers: TypeMapper[] = [];
var activeTypeMappersCaches: Map<string, Type>[] = [];
var activeTypeMappersCount = 0;

var emptyStringType = getStringLiteralType("");
var zeroType = getNumberLiteralType(0);
var zeroBigIntType = getBigIntLiteralType({ negative: false, base10Value: "0" });
Expand Down Expand Up @@ -20371,10 +20375,26 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
error(currentNode, Diagnostics.Type_instantiation_is_excessively_deep_and_possibly_infinite);
return errorType;
}
const index = findActiveMapper(mapper);
if (index === -1) {
pushActiveMapper(mapper);
}
const key = type.id + getAliasId(aliasSymbol, aliasTypeArguments);
const mapperCache = activeTypeMappersCaches[index !== -1 ? index : activeTypeMappersCount - 1];
const cached = mapperCache.get(key);
if (cached) {
return cached;
}
totalInstantiationCount++;
instantiationCount++;
instantiationDepth++;
const result = instantiateTypeWorker(type, mapper, aliasSymbol, aliasTypeArguments);
if (index === -1) {
popActiveMapper();
}
else {
mapperCache.set(key, result);
}
instantiationDepth--;
return result;
}
Expand Down Expand Up @@ -26947,6 +26967,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
inference.inferredType = fallbackType && context.compareTypes(fallbackType, getTypeWithThisArgument(instantiatedConstraint, fallbackType)) ? fallbackType : instantiatedConstraint;
}
}
clearActiveMapperCaches();
}

return inference.inferredType;
Expand Down Expand Up @@ -32163,6 +32184,31 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}

function pushActiveMapper(mapper: TypeMapper) {
activeTypeMappers[activeTypeMappersCount] = mapper;
activeTypeMappersCaches[activeTypeMappersCount] = new Map();
activeTypeMappersCount++;
}

function popActiveMapper() {
activeTypeMappersCount--;
}

function findActiveMapper(mapper: TypeMapper) {
for (let i = activeTypeMappersCount - 1; i >= 0; i--) {
if (mapper === activeTypeMappers[i]) {
return i;
}
}
return -1;
}

function clearActiveMapperCaches() {
for (let i = activeTypeMappersCount - 1; i >= 0; i--) {
activeTypeMappersCaches[i].clear();
}
}

function getContextualImportAttributeType(node: ImportAttribute) {
return getTypeOfPropertyOfContextualType(getGlobalImportAttributesType(/*reportErrors*/ false), getNameFromImportAttribute(node));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/callsOnComplexSignatures.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
=== Performance Stats ===
Assignability cache: 2,500
Type Count: 10,000
Instantiation count: 100,000
Instantiation count: 50,000
Symbol count: 50,000

=== callsOnComplexSignatures.tsx ===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
=== Performance Stats ===
Assignability cache: 2,500
Type Count: 10,000
Instantiation count: 100,000
Instantiation count: 50,000
Symbol count: 50,000

=== checkJsxChildrenCanBeTupleType.tsx ===
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/checkJsxChildrenProperty16.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
=== Performance Stats ===
Assignability cache: 2,500
Type Count: 10,000
Instantiation count: 100,000
Instantiation count: 50,000
Symbol count: 50,000

=== checkJsxChildrenProperty16.tsx ===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
=== Performance Stats ===
Assignability cache: 2,500
Type Count: 10,000
Instantiation count: 100,000
Instantiation count: 50,000
Symbol count: 50,000

=== checkJsxUnionSFXContextualTypeInferredCorrectly.tsx ===
Expand Down
3 changes: 0 additions & 3 deletions tests/baselines/reference/circularBaseConstraint.types
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//// [tests/cases/compiler/circularBaseConstraint.ts] ////

=== Performance Stats ===
Instantiation count: 2,500

=== circularBaseConstraint.ts ===
// Repro from #54610

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//// [tests/cases/compiler/circularlySimplifyingConditionalTypesNoCrash.ts] ////

=== Performance Stats ===
Instantiation count: 2,500

=== circularlySimplifyingConditionalTypesNoCrash.ts ===
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
>Omit : Omit<T, K>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Strict subtype cache: 2,500
Assignability cache: 10,000
Type Count: 10,000
Instantiation count: 100,000
Instantiation count: 50,000

=== conditionalTypeDiscriminatingLargeUnionRegularTypeFetchingSpeedReasonable.ts ===
type BigUnion =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

=== conditionalTypeDoesntSpinForever.ts ===
// A *self-contained* demonstration of the problem follows...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
=== Performance Stats ===
Assignability cache: 1,000
Type Count: 5,000
Instantiation count: 100,000
Instantiation count: 50,000
Symbol count: 50,000

=== conditionalTypeVarianceBigArrayConstraintsPerformance.ts ===
Expand Down
3 changes: 0 additions & 3 deletions tests/baselines/reference/conditionalTypes1.types
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//// [tests/cases/conformance/types/conditional/conditionalTypes1.ts] ////

=== Performance Stats ===
Instantiation count: 1,000

=== conditionalTypes1.ts ===
type T00 = Exclude<"a" | "b" | "c" | "d", "a" | "c" | "f">; // "b" | "d"
>T00 : T00
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//// [tests/cases/compiler/contextualParameterAndSelfReferentialConstraint1.ts] ////

=== contextualParameterAndSelfReferentialConstraint1.ts ===
type NoExcessProperties<T, U> = T & {
>NoExcessProperties : Symbol(NoExcessProperties, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 0, 0))
>T : Symbol(T, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 0, 24))
>U : Symbol(U, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 0, 26))
>T : Symbol(T, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 0, 24))

readonly [K in Exclude<keyof U, keyof T>]: never;
>K : Symbol(K, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 1, 12))
>Exclude : Symbol(Exclude, Decl(lib.es5.d.ts, --, --))
>U : Symbol(U, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 0, 26))
>T : Symbol(T, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 0, 24))

};

interface Effect<out A> {
>Effect : Symbol(Effect, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 2, 2))
>A : Symbol(A, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 4, 17))

readonly EffectTypeId: {
>EffectTypeId : Symbol(Effect.EffectTypeId, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 4, 25))

readonly _A: (_: never) => A;
>_A : Symbol(_A, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 5, 26))
>_ : Symbol(_, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 6, 18))
>A : Symbol(A, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 4, 17))

};
}

declare function pipe<A, B>(a: A, ab: (a: A) => B): B;
>pipe : Symbol(pipe, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 8, 1))
>A : Symbol(A, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 10, 22))
>B : Symbol(B, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 10, 24))
>a : Symbol(a, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 10, 28))
>A : Symbol(A, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 10, 22))
>ab : Symbol(ab, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 10, 33))
>a : Symbol(a, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 10, 39))
>A : Symbol(A, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 10, 22))
>B : Symbol(B, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 10, 24))
>B : Symbol(B, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 10, 24))

interface RepeatOptions<A> {
>RepeatOptions : Symbol(RepeatOptions, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 10, 54))
>A : Symbol(A, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 12, 24))

until?: (_: A) => boolean;
>until : Symbol(RepeatOptions.until, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 12, 28))
>_ : Symbol(_, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 13, 11))
>A : Symbol(A, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 12, 24))
}

declare const repeat: {
>repeat : Symbol(repeat, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 16, 13))

<O extends NoExcessProperties<RepeatOptions<A>, O>, A>(
>O : Symbol(O, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 17, 3))
>NoExcessProperties : Symbol(NoExcessProperties, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 0, 0))
>RepeatOptions : Symbol(RepeatOptions, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 10, 54))
>A : Symbol(A, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 17, 53))
>O : Symbol(O, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 17, 3))
>A : Symbol(A, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 17, 53))

options: O,
>options : Symbol(options, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 17, 57))
>O : Symbol(O, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 17, 3))

): (self: Effect<A>) => Effect<A>;
>self : Symbol(self, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 19, 6))
>Effect : Symbol(Effect, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 2, 2))
>A : Symbol(A, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 17, 53))
>Effect : Symbol(Effect, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 2, 2))
>A : Symbol(A, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 17, 53))

};

pipe(
>pipe : Symbol(pipe, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 8, 1))

{} as Effect<boolean>,
>Effect : Symbol(Effect, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 2, 2))

repeat({
>repeat : Symbol(repeat, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 16, 13))

until: (x) => {
>until : Symbol(until, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 24, 10))
>x : Symbol(x, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 25, 12))

return x; // boolean
>x : Symbol(x, Decl(contextualParameterAndSelfReferentialConstraint1.ts, 25, 12))

},
}),
);

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//// [tests/cases/compiler/contextualParameterAndSelfReferentialConstraint1.ts] ////

=== contextualParameterAndSelfReferentialConstraint1.ts ===
type NoExcessProperties<T, U> = T & {
>NoExcessProperties : NoExcessProperties<T, U>
> : ^^^^^^^^^^^^^^^^^^^^^^^^

readonly [K in Exclude<keyof U, keyof T>]: never;
};

interface Effect<out A> {
readonly EffectTypeId: {
>EffectTypeId : { readonly _A: (_: never) => A; }
> : ^^^^^^^^^^^^^^^ ^^^

readonly _A: (_: never) => A;
>_A : (_: never) => A
> : ^ ^^ ^^^^^
>_ : never
> : ^^^^^

};
}

declare function pipe<A, B>(a: A, ab: (a: A) => B): B;
>pipe : <A, B>(a: A, ab: (a: A) => B) => B
> : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^
>a : A
> : ^
>ab : (a: A) => B
> : ^ ^^ ^^^^^
>a : A
> : ^

interface RepeatOptions<A> {
until?: (_: A) => boolean;
>until : ((_: A) => boolean) | undefined
> : ^^ ^^ ^^^^^ ^^^^^^^^^^^^^
>_ : A
> : ^
}

declare const repeat: {
>repeat : <O extends NoExcessProperties<RepeatOptions<A>, O>, A>(options: O) => (self: Effect<A>) => Effect<A>
> : ^ ^^^^^^^^^ ^^ ^^ ^^ ^^^^^

<O extends NoExcessProperties<RepeatOptions<A>, O>, A>(
options: O,
>options : O
> : ^

): (self: Effect<A>) => Effect<A>;
>self : Effect<A>
> : ^^^^^^^^^

};

pipe(
>pipe( {} as Effect<boolean>, repeat({ until: (x) => { return x; // boolean }, }),) : Effect<boolean>
> : ^^^^^^^^^^^^^^^
>pipe : <A, B>(a: A, ab: (a: A) => B) => B
> : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^

{} as Effect<boolean>,
>{} as Effect<boolean> : Effect<boolean>
> : ^^^^^^^^^^^^^^^
>{} : {}
> : ^^

repeat({
>repeat({ until: (x) => { return x; // boolean }, }) : (self: Effect<boolean>) => Effect<boolean>
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>repeat : <O extends NoExcessProperties<RepeatOptions<A>, O>, A>(options: O) => (self: Effect<A>) => Effect<A>
> : ^ ^^^^^^^^^ ^^ ^^ ^^ ^^^^^
>{ until: (x) => { return x; // boolean }, } : { until: (x: boolean) => boolean; }
> : ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^

until: (x) => {
>until : (x: boolean) => boolean
> : ^ ^^^^^^^^^^^^^^^^^^^^^
>(x) => { return x; // boolean } : (x: boolean) => boolean
> : ^ ^^^^^^^^^^^^^^^^^^^^^
>x : boolean
> : ^^^^^^^

return x; // boolean
>x : boolean
> : ^^^^^^^

},
}),
);

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
=== Performance Stats ===
Assignability cache: 2,500
Type Count: 10,000
Instantiation count: 100,000
Instantiation count: 50,000
Symbol count: 50,000

=== contextuallyTypedJsxChildren.tsx ===
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/controlFlowOptionalChain3.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
=== Performance Stats ===
Assignability cache: 2,500
Type Count: 10,000
Instantiation count: 100,000
Instantiation count: 50,000
Symbol count: 50,000

=== controlFlowOptionalChain3.tsx ===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

=== declarationEmitHigherOrderRetainedGenerics.ts ===
export interface TypeLambda {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//// [tests/cases/compiler/declarationsWithRecursiveInternalTypesProduceUniqueTypeParams.ts] ////

=== Performance Stats ===
Instantiation count: 500 -> 1,000

=== declarationsWithRecursiveInternalTypesProduceUniqueTypeParams.ts ===
// Note that both of the following have an `any` in their return type from where we bottom out the type printout
// for having too many instances of the same symbol nesting.
Expand Down
Loading