Skip to content

Commit

Permalink
Added some performance optimizations to handle cases where there are …
Browse files Browse the repository at this point in the history
…many overloads for a function (>100). Previous code hit n^2 analysis times where n is number of overloads.
  • Loading branch information
msfterictraut committed Feb 3, 2021
1 parent 77410d8 commit d9f621e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/pyright-internal/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,18 @@ export class Checker extends ParseTreeWalker {
if (functionTypeResult && isOverloadedFunction(functionTypeResult.decoratedType)) {
const overloads = functionTypeResult.decoratedType.overloads;
if (overloads.length > 1) {
this._validateOverloadConsistency(
node,
overloads[overloads.length - 1],
overloads.slice(0, overloads.length - 1)
);
const maxOverloadConsistencyCheckLength = 100;

// The check is n^2 in time, so if the number of overloads
// is very large (which can happen for some generated code),
// skip this check to avoid quadratic analysis time.
if (overloads.length < maxOverloadConsistencyCheckLength) {
this._validateOverloadConsistency(
node,
overloads[overloads.length - 1],
overloads.slice(0, overloads.length - 1)
);
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12144,6 +12144,16 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
// Find this function's declaration.
const declIndex = decls.findIndex((decl) => decl === functionDecl);
if (declIndex > 0) {
// Evaluate all of the previous function declarations. They will
// be cached. We do it in this order to avoid a stack overflow due
// to recursion if there is a large number (1000's) of overloads.
for (let i = 0; i < declIndex; i++) {
const decl = decls[i];
if (decl.type === DeclarationType.Function) {
getTypeOfFunction(decl.node);
}
}

const overloadedTypes: FunctionType[] = [];

// Look at the previous declaration's type.
Expand Down

0 comments on commit d9f621e

Please sign in to comment.