Skip to content

Commit d9f621e

Browse files
committed
Added some performance optimizations to handle cases where there are many overloads for a function (>100). Previous code hit n^2 analysis times where n is number of overloads.
1 parent 77410d8 commit d9f621e

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

‎packages/pyright-internal/src/analyzer/checker.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,18 @@ export class Checker extends ParseTreeWalker {
418418
if (functionTypeResult && isOverloadedFunction(functionTypeResult.decoratedType)) {
419419
const overloads = functionTypeResult.decoratedType.overloads;
420420
if (overloads.length > 1) {
421-
this._validateOverloadConsistency(
422-
node,
423-
overloads[overloads.length - 1],
424-
overloads.slice(0, overloads.length - 1)
425-
);
421+
const maxOverloadConsistencyCheckLength = 100;
422+
423+
// The check is n^2 in time, so if the number of overloads
424+
// is very large (which can happen for some generated code),
425+
// skip this check to avoid quadratic analysis time.
426+
if (overloads.length < maxOverloadConsistencyCheckLength) {
427+
this._validateOverloadConsistency(
428+
node,
429+
overloads[overloads.length - 1],
430+
overloads.slice(0, overloads.length - 1)
431+
);
432+
}
426433
}
427434
}
428435

‎packages/pyright-internal/src/analyzer/typeEvaluator.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12144,6 +12144,16 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
1214412144
// Find this function's declaration.
1214512145
const declIndex = decls.findIndex((decl) => decl === functionDecl);
1214612146
if (declIndex > 0) {
12147+
// Evaluate all of the previous function declarations. They will
12148+
// be cached. We do it in this order to avoid a stack overflow due
12149+
// to recursion if there is a large number (1000's) of overloads.
12150+
for (let i = 0; i < declIndex; i++) {
12151+
const decl = decls[i];
12152+
if (decl.type === DeclarationType.Function) {
12153+
getTypeOfFunction(decl.node);
12154+
}
12155+
}
12156+
1214712157
const overloadedTypes: FunctionType[] = [];
1214812158

1214912159
// Look at the previous declaration's type.

0 commit comments

Comments
 (0)