Skip to content

Eliminate redundant work in type inference #15863

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

Merged
merged 2 commits into from
May 16, 2017
Merged
Changes from 1 commit
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
37 changes: 20 additions & 17 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10096,21 +10096,29 @@ namespace ts {
inferTypes(context.signature.typeParameters, context.inferences, originalSource, originalTarget);
}

function getSymbolForInference(type: Type) {
// Exclude the static side of classes since it shares its symbol with the instance side which leads
// to false positives.
return type.flags & TypeFlags.Object && !(getObjectFlags(type) & ObjectFlags.Anonymous && type.symbol && type.symbol.flags & SymbolFlags.Class) ? type.symbol : undefined;
}

function inferTypes(typeVariables: TypeVariable[], typeInferences: TypeInferences[], originalSource: Type, originalTarget: Type) {
let sourceStack: Type[];
let targetStack: Type[];
let stack: Type[];
let depth = 0;
let inferiority = 0;
const visited = createMap<boolean>();
inferFromTypes(originalSource, originalTarget);

function isInProcess(source: Type, target: Type) {
for (let i = 0; i < depth; i++) {
if (source === sourceStack[i] && target === targetStack[i]) {
return true;
function isInstantiationInProcess(type: Type) {
const symbol = getSymbolForInference(type);
if (symbol) {
for (let i = 0; i < depth; i++) {
const t = stack[i];
if (getSymbolForInference(t) === symbol) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not storing getSymbolForInference(target) on the stack directly instead of calling it on every entry when we check

return true;
}
}
}
return false;
}

function inferFromTypes(source: Type, target: Type) {
Expand Down Expand Up @@ -10240,23 +10248,18 @@ namespace ts {
else {
source = getApparentType(source);
if (source.flags & TypeFlags.Object) {
if (isInProcess(source, target)) {
return;
}
if (isDeeplyNestedType(source, sourceStack, depth) && isDeeplyNestedType(target, targetStack, depth)) {
// If we are already processing another target type with the same associated symbol (such as
// an instantiation of the same generic type), we do not explore this target as it would yield
// no further inferences.
if (isInstantiationInProcess(target)) {
return;
}
const key = source.id + "," + target.id;
if (visited.get(key)) {
return;
}
visited.set(key, true);
if (depth === 0) {
sourceStack = [];
targetStack = [];
}
sourceStack[depth] = source;
targetStack[depth] = target;
(stack || (stack = []))[depth] = target;
depth++;
inferFromObjectTypes(source, target);
depth--;
Expand Down