|
7342 | 7342 | }, |
7343 | 7343 | // When a node has grown, check whether it should be split. |
7344 | 7344 | maybeSpill: function() { |
| 7345 | + if (this.children.length <= 10) return; |
7345 | 7346 | 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(); |
7370 | 7364 | }, |
7371 | 7365 | iterN: function(at, n, op) { |
7372 | 7366 | for (var i = 0; i < this.children.length; ++i) { |
|
0 commit comments