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

Use Set<T> for faster lookup of "maybe related" type pairs #43623

Closed
wants to merge 1 commit into from
Closed
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
50 changes: 28 additions & 22 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17175,7 +17175,8 @@ namespace ts {

let errorInfo: DiagnosticMessageChain | undefined;
let relatedInfo: [DiagnosticRelatedInformation, ...DiagnosticRelatedInformation[]] | undefined;
let maybeKeys: string[];
let maybeKeyList: string[];
let maybeKeySet: Set<string>;
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion. You can just just Map<string, number> where number is index mayBeList index and then you dont need array.

Copy link
Member

@amcasey amcasey Apr 12, 2021

Choose a reason for hiding this comment

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

I don't think that's true because of the way the set has to be "popped" down to maybeStart.

Edit: I guess you could traverse the whole thing, dropping keys with values above maybeStart?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, we'd have to traverse the whole thing every time--which we really don't want to do.

let sourceStack: Type[];
let targetStack: Type[];
let maybeCount = 0;
Expand Down Expand Up @@ -18024,8 +18025,9 @@ namespace ts {
return entry & RelationComparisonResult.Succeeded ? Ternary.True : Ternary.False;
}
}
if (!maybeKeys) {
maybeKeys = [];
if (!maybeKeyList) {
maybeKeyList = [];
maybeKeySet = new Set<string>();
sourceStack = [];
targetStack = [];
}
Expand All @@ -18037,20 +18039,19 @@ namespace ts {
const index = length(id.slice(0, offset).match(/[-=]/g) || undefined);
return `=${index}`;
})).join(",");
for (let i = 0; i < maybeCount; i++) {
if (maybeKeySet.has(id) || maybeKeySet.has(broadestEquivalentId)) {
// If source and target are already being compared, consider them related with assumptions
if (id === maybeKeys[i] || broadestEquivalentId === maybeKeys[i]) {
return Ternary.Maybe;
}
return Ternary.Maybe;
}
if (depth === 100) {
overflow = true;
return Ternary.False;
}
}
const maybeStart = maybeCount;
maybeKeys[maybeCount] = id;
maybeKeyList[maybeCount] = id;
maybeCount++;
maybeKeySet.add(id);
sourceStack[depth] = source;
targetStack[depth] = target;
depth++;
Expand Down Expand Up @@ -18083,22 +18084,27 @@ namespace ts {
}
expandingFlags = saveExpandingFlags;
depth--;
if (result) {
if (result === Ternary.True || depth === 0) {
if (result === Ternary.True || result === Ternary.Maybe) {
// If result is definitely true, record all maybe keys as having succeeded. Also, record Ternary.Maybe
// results as having succeeded once we reach depth 0, but never record Ternary.Unknown results.
for (let i = maybeStart; i < maybeCount; i++) {
relation.set(maybeKeys[i], RelationComparisonResult.Succeeded | propagatingVarianceFlags);
}
if (result === Ternary.True || result === Ternary.False || depth === 0) {
if (result === Ternary.True || result === Ternary.Maybe) {
// If result is definitely true, record all maybe keys as having succeeded. Also, record Ternary.Maybe
// results as having succeeded once we reach depth 0, but never record Ternary.Unknown results.
for (let i = maybeStart; i < maybeCount; i++) {
relation.set(maybeKeyList[i], RelationComparisonResult.Succeeded | propagatingVarianceFlags);
}
}
else if (result === Ternary.False) {
// A false result goes straight into global cache (when something is false under
// assumptions it will also be false without assumptions)
relation.set(id, (reportErrors ? RelationComparisonResult.Reported : 0) | RelationComparisonResult.Failed | propagatingVarianceFlags);
}
if (maybeStart === 0) {
maybeKeySet.clear();
}
else {
for (let i = maybeStart; i < maybeCount; i++) {
maybeKeySet.delete(maybeKeyList[i]);
amcasey marked this conversation as resolved.
Show resolved Hide resolved
}
maybeCount = maybeStart;
}
}
else {
// A false result goes straight into global cache (when something is false under
// assumptions it will also be false without assumptions)
relation.set(id, (reportErrors ? RelationComparisonResult.Reported : 0) | RelationComparisonResult.Failed | propagatingVarianceFlags);
maybeCount = maybeStart;
}
return result;
Expand Down