Improve the performance of requesting completions within a massive array literal

This commit is contained in:
Wesley Wigham
2020-10-05 14:42:10 -07:00
parent 1191e2e731
commit da57f9876e
2 changed files with 50 additions and 1 deletions

View File

@@ -1158,6 +1158,35 @@ namespace ts {
}
}
/**
* Psuedo-binary searches the list of children for the first element whose end position exceeds the input position
* Returns `undefined` if no element satisfies the condition, or the index of the satisfying element otherwise.
* This could probably be written using our `binarySeach` helper, but the `keyComparator` would be hairy enough
* that it's probably worth just writing out the algorithm in full.
* @param {number} start - Start search index, inclusive
* @param {number} end - End search index, inclusive
*/
function searchChildrenSlice(position: number, children: Node[], start = 0, end = children.length - 1): number | undefined {
const pivot = Math.floor((end - start)/2) + start;
if (!children[pivot]) {
return undefined;
}
if (position < children[pivot].end) {
// first element whose end position is greater than the input position
if (!children[pivot - 1] || position >= children[pivot - 1].end) {
return pivot;
}
if (pivot === start) {
return undefined;
}
return searchChildrenSlice(position, children, start, pivot - 1);
}
if (pivot === end) {
return undefined;
}
return searchChildrenSlice(position, children, pivot + 1, end);
}
/**
* Finds the rightmost token satisfying `token.end <= position`,
* excluding `JsxText` tokens containing only whitespace.
@@ -1173,7 +1202,8 @@ namespace ts {
}
const children = n.getChildren(sourceFile);
for (let i = 0; i < children.length; i++) {
const i = searchChildrenSlice(position, children);
if (typeof i !== "undefined") {
const child = children[i];
// Note that the span of a node's tokens is [node.getStart(...), node.end).
// Given that `position < child.end` and child has constituent tokens, we distinguish these cases: