Skip to content
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

Improved type evaluation performance in certain cases involving proto… #9115

Merged
merged 1 commit into from
Sep 30, 2024
Merged
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
8 changes: 0 additions & 8 deletions packages/pyright-internal/src/analyzer/constraintTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,6 @@ export class ConstraintTracker {
}
}

isSame(other: ConstraintTracker) {
if (other._constraintSets.length !== this._constraintSets.length) {
return false;
}

return this._constraintSets.every((set, index) => set.isSame(other._constraintSets[index]));
}

isEmpty() {
return this._constraintSets.every((set) => set.isEmpty());
}
Expand Down
34 changes: 9 additions & 25 deletions packages/pyright-internal/src/analyzer/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ interface ProtocolCompatibility {
srcType: Type;
destType: Type;
flags: AssignTypeFlags;
constraints: ConstraintTracker | undefined;
isCompatible: boolean;
}

Expand All @@ -80,6 +79,12 @@ export function assignClassToProtocol(
// srcType can be an instantiable class or a class instance.
assert(isInstantiableClass(destType) && ClassType.isProtocolClass(destType));

// A literal source type should never affect protocol matching, so strip
// the literal type if it's present. This helps conserve on cache entries.
if (srcType.priv.literalValue !== undefined) {
srcType = evaluator.stripLiteralValue(srcType) as ClassType;
}

const enforceInvariance = (flags & AssignTypeFlags.Invariant) !== 0;

// Use a stack of pending protocol class evaluations to detect recursion.
Expand Down Expand Up @@ -120,7 +125,6 @@ export function assignClassToProtocol(

protocolAssignmentStack.push({ srcType, destType });
let isCompatible = true;
const clonedConstraints = constraints?.clone();

try {
isCompatible = assignToProtocolInternal(evaluator, destType, srcType, diag, constraints, flags, recursionCount);
Expand All @@ -134,7 +138,7 @@ export function assignClassToProtocol(
protocolAssignmentStack.pop();

// Cache the results for next time.
setProtocolCompatibility(destType, srcType, flags, clonedConstraints, isCompatible);
setProtocolCompatibility(destType, srcType, flags, isCompatible);

return isCompatible;
}
Expand Down Expand Up @@ -225,12 +229,7 @@ function getProtocolCompatibility(
}

const entry = entries.find((entry) => {
return (
isTypeSame(entry.destType, destType) &&
isTypeSame(entry.srcType, srcType) &&
entry.flags === flags &&
isConstraintTrackerSame(constraints, entry.constraints)
);
return isTypeSame(entry.destType, destType) && isTypeSame(entry.srcType, srcType) && entry.flags === flags;
});

return entry?.isCompatible;
Expand All @@ -240,7 +239,6 @@ function setProtocolCompatibility(
destType: ClassType,
srcType: ClassType,
flags: AssignTypeFlags,
constraints: ConstraintTracker | undefined,
isCompatible: boolean
) {
let map = srcType.shared.protocolCompatibility as Map<string, ProtocolCompatibility[]> | undefined;
Expand All @@ -255,27 +253,13 @@ function setProtocolCompatibility(
map.set(destType.shared.fullName, entries);
}

entries.push({
destType,
srcType,
flags,
constraints: constraints,
isCompatible,
});
entries.push({ destType, srcType, flags, isCompatible });

if (entries.length > maxProtocolCompatibilityCacheEntries) {
entries.shift();
}
}

function isConstraintTrackerSame(context1: ConstraintTracker | undefined, context2: ConstraintTracker | undefined) {
if (!context1 || !context2) {
return context1 === context2;
}

return context1.isSame(context2);
}

function assignToProtocolInternal(
evaluator: TypeEvaluator,
destType: ClassType,
Expand Down
Loading