Skip to content

Commit 0d4374d

Browse files
committed
Revert part of 9ad3837
It breaks document tree rebalancing. Closes codemirror#4032
1 parent ac3530e commit 0d4374d

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

lib/codemirror.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7342,31 +7342,25 @@
73427342
},
73437343
// When a node has grown, check whether it should be split.
73447344
maybeSpill: function() {
7345+
if (this.children.length <= 10) return;
73457346
var me = this;
7346-
var children = me.children;
7347-
var numChildren = children.length;
7348-
if (numChildren <= 10) return;
7349-
// To avoid memory thrashing when the children array is huge (e.g. first view of a large file), it's never spliced.
7350-
// Instead, small slices are taken. They're taken in order because sequential memory accesses are fastest.
7351-
var parent = me.parent || me;
7352-
var numMoreBranches = Math.ceil((numChildren - 10) / 5);
7353-
var firstBranchNumChildren = numChildren - numMoreBranches * 5;
7354-
var firstBranch = new BranchChunk(children.slice(0, firstBranchNumChildren));
7355-
var branches = [firstBranch];
7356-
firstBranch.parent = parent;
7357-
for (var i = 0; i < numMoreBranches; i++) {
7358-
var branchStart = firstBranchNumChildren + i * 5;
7359-
var branch = new BranchChunk(children.slice(branchStart, branchStart + 5));
7360-
branches.push(branch);
7361-
branch.parent = parent;
7362-
}
7363-
if (parent === me) {
7364-
parent.children = branches;
7365-
} else {
7366-
var myIndex = indexOf(parent.children, me);
7367-
parent.children.splice(myIndex, 1, branches);
7368-
}
7369-
parent.maybeSpill();
7347+
do {
7348+
var spilled = me.children.splice(me.children.length - 5, 5);
7349+
var sibling = new BranchChunk(spilled);
7350+
if (!me.parent) { // Become the parent node
7351+
var copy = new BranchChunk(me.children);
7352+
copy.parent = me;
7353+
me.children = [copy, sibling];
7354+
me = copy;
7355+
} else {
7356+
me.size -= sibling.size;
7357+
me.height -= sibling.height;
7358+
var myIndex = indexOf(me.parent.children, me);
7359+
me.parent.children.splice(myIndex + 1, 0, sibling);
7360+
}
7361+
sibling.parent = me.parent;
7362+
} while (me.children.length > 10);
7363+
me.parent.maybeSpill();
73707364
},
73717365
iterN: function(at, n, op) {
73727366
for (var i = 0; i < this.children.length; ++i) {

0 commit comments

Comments
 (0)