Skip to content

Commit

Permalink
Fixed a performance regression caused by a recent bug fix that affect…
Browse files Browse the repository at this point in the history
…s variance inference for recursive type aliases. This addresses #9118. (#9119)
  • Loading branch information
erictraut authored Oct 1, 2024
1 parent 9326096 commit 67a3d24
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7615,6 +7615,7 @@ export function createTypeEvaluator(
typeAliasTypeParams: TypeVarType[],
usageVariances: Variance[],
varianceContext: Variance,
pendingTypes: Type[] = [],
recursionCount = 0
) {
if (recursionCount > maxTypeRecursionCount) {
Expand All @@ -7623,9 +7624,12 @@ export function createTypeEvaluator(

const transformedType = transformPossibleRecursiveTypeAlias(type);

// If this is a recursive type alias, use a lower recursion limit.
// If this is a recursive type alias, see if we've already recursed
// seen it once before in the recursion stack. If so, don't recurse
// further.
if (transformedType !== type) {
if (recursionCount > maxRecursiveTypeAliasRecursionCount) {
const pendingOverlaps = pendingTypes.filter((pendingType) => isTypeSame(pendingType, type));
if (pendingOverlaps.length > 1) {
return;
}
}
Expand All @@ -7639,13 +7643,18 @@ export function createTypeEvaluator(
if (typeParamIndex >= 0) {
usageVariances[typeParamIndex] = combineVariances(usageVariances[typeParamIndex], variance);
} else {
pendingTypes.push(type);

updateUsageVariancesRecursive(
subtype,
typeAliasTypeParams,
usageVariances,
variance,
pendingTypes,
recursionCount
);

pendingTypes.pop();
}
});
}
Expand Down

0 comments on commit 67a3d24

Please sign in to comment.