Skip to content

Commit 38db79d

Browse files
author
Andy
authored
buildTreeFromBottom: Really simplify loop (microsoft#17105)
1 parent 815af7d commit 38db79d

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

src/server/scriptVersionCache.ts

+13-21
Original file line numberDiff line numberDiff line change
@@ -524,29 +524,18 @@ namespace ts.server {
524524
}
525525

526526
private static buildTreeFromBottom(nodes: LineCollection[]): LineNode {
527-
const interiorNodeCount = Math.ceil(nodes.length / lineCollectionCapacity);
528-
const interiorNodes: LineNode[] = new Array(interiorNodeCount);
527+
if (nodes.length < lineCollectionCapacity) {
528+
return new LineNode(nodes);
529+
}
530+
531+
const interiorNodes = new Array<LineNode>(Math.ceil(nodes.length / lineCollectionCapacity));
529532
let nodeIndex = 0;
530-
for (let i = 0; i < interiorNodeCount; i++) {
531-
const interiorNode = interiorNodes[i] = new LineNode();
532-
let charCount = 0;
533-
let lineCount = 0;
533+
for (let i = 0; i < interiorNodes.length; i++) {
534534
const end = Math.min(nodeIndex + lineCollectionCapacity, nodes.length);
535-
for (; nodeIndex < end; nodeIndex++) {
536-
const node = nodes[nodeIndex];
537-
interiorNode.add(node);
538-
charCount += node.charCount();
539-
lineCount += node.lineCount();
540-
}
541-
interiorNode.totalChars = charCount;
542-
interiorNode.totalLines = lineCount;
543-
}
544-
if (interiorNodes.length === 1) {
545-
return interiorNodes[0];
546-
}
547-
else {
548-
return this.buildTreeFromBottom(interiorNodes);
535+
interiorNodes[i] = new LineNode(nodes.slice(nodeIndex, end));
536+
nodeIndex = end;
549537
}
538+
return this.buildTreeFromBottom(interiorNodes);
550539
}
551540

552541
static linesFromText(text: string) {
@@ -575,7 +564,10 @@ namespace ts.server {
575564
export class LineNode implements LineCollection {
576565
totalChars = 0;
577566
totalLines = 0;
578-
private children: LineCollection[] = [];
567+
568+
constructor(private readonly children: LineCollection[] = []) {
569+
if (children.length) this.updateCounts();
570+
}
579571

580572
isLeaf() {
581573
return false;

0 commit comments

Comments
 (0)