Skip to content

Commit 2ad79e0

Browse files
author
Nicolas Laurent
committed
stop rebalancing trees of ConcatRopes on append
1 parent 845cfe3 commit 2ad79e0

File tree

1 file changed

+1
-100
lines changed

1 file changed

+1
-100
lines changed

src/main/java/org/truffleruby/core/rope/RopeNodes.java

Lines changed: 1 addition & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
import static org.truffleruby.core.rope.CodeRange.CR_UNKNOWN;
2020
import static org.truffleruby.core.rope.CodeRange.CR_VALID;
2121

22-
import java.util.ArrayDeque;
2322
import java.util.Arrays;
24-
import java.util.Deque;
2523

2624
import org.jcodings.Encoding;
2725
import org.jcodings.specific.ASCIIEncoding;
@@ -425,8 +423,6 @@ public static ConcatNode create() {
425423
return RopeNodesFactory.ConcatNodeGen.create();
426424
}
427425

428-
@Child private FlattenNode flattenNode;
429-
430426
public abstract Rope executeConcat(Rope left, Rope right, Encoding encoding);
431427

432428
@Specialization
@@ -467,8 +463,7 @@ protected Rope concatRightEmpty(ManagedRope left, Rope right, Encoding encoding,
467463
@Specialization(guards = { "!left.isEmpty()", "!right.isEmpty()", "!isCodeRangeBroken(left, right)" })
468464
protected Rope concat(ManagedRope left, ManagedRope right, Encoding encoding,
469465
@Cached ConditionProfile sameCodeRangeProfile,
470-
@Cached ConditionProfile brokenCodeRangeProfile,
471-
@Cached ConditionProfile shouldRebalanceProfile) {
466+
@Cached ConditionProfile brokenCodeRangeProfile) {
472467
try {
473468
Math.addExact(left.byteLength(), right.byteLength());
474469
} catch (ArithmeticException e) {
@@ -477,18 +472,7 @@ protected Rope concat(ManagedRope left, ManagedRope right, Encoding encoding,
477472
getContext().getCoreExceptions().argumentErrorTooLargeString(this));
478473
}
479474

480-
if (shouldRebalanceProfile.profile(
481-
left.depth() >= getContext().getOptions().ROPE_DEPTH_THRESHOLD && left instanceof ConcatRope)) {
482-
left = rebalance((ConcatRope) left, getContext().getOptions().ROPE_DEPTH_THRESHOLD, getFlattenNode());
483-
}
484-
485-
if (shouldRebalanceProfile.profile(
486-
right.depth() >= getContext().getOptions().ROPE_DEPTH_THRESHOLD && right instanceof ConcatRope)) {
487-
right = rebalance((ConcatRope) right, getContext().getOptions().ROPE_DEPTH_THRESHOLD, getFlattenNode());
488-
}
489-
490475
int depth = depth(left, right);
491-
/* if (depth >= 10) { System.out.println("ConcatRope depth: " + depth); } */
492476

493477
return new ConcatRope(
494478
left,
@@ -503,14 +487,6 @@ protected Rope concat(ManagedRope left, ManagedRope right, Encoding encoding,
503487
isBalanced(left, right));
504488
}
505489

506-
private FlattenNode getFlattenNode() {
507-
if (flattenNode == null) {
508-
CompilerDirectives.transferToInterpreterAndInvalidate();
509-
flattenNode = insert(FlattenNode.create());
510-
}
511-
return flattenNode;
512-
}
513-
514490
private boolean isBalanced(Rope left, Rope right) {
515491
// Our definition of balanced is centered around the notion of rebalancing. We could have a simple structure
516492
// such as ConcatRope(ConcatRope(LeafRope, LeafRope), LeafRope) that is balanced on its own but may contribute
@@ -532,81 +508,6 @@ private boolean isBalanced(Rope left, Rope right) {
532508
}
533509
}
534510

535-
@TruffleBoundary
536-
private ManagedRope rebalance(ConcatRope rope, int depthThreshold, FlattenNode flattenNode) {
537-
Deque<ManagedRope> currentRopeQueue = new ArrayDeque<>();
538-
Deque<ManagedRope> nextLevelQueue = new ArrayDeque<>();
539-
540-
linearizeTree(rope.getLeft(), currentRopeQueue);
541-
linearizeTree(rope.getRight(), currentRopeQueue);
542-
543-
final int flattenThreshold = depthThreshold / 2;
544-
545-
ManagedRope root = null;
546-
while (!currentRopeQueue.isEmpty()) {
547-
ManagedRope left = currentRopeQueue.pop();
548-
549-
if (left.depth() >= flattenThreshold) {
550-
// TODO (eregon, 15 June 2020): should this cache the resulting bytes by using getBytes()?
551-
left = flattenNode.executeFlatten(left);
552-
}
553-
554-
if (currentRopeQueue.isEmpty()) {
555-
if (nextLevelQueue.isEmpty()) {
556-
root = left;
557-
} else {
558-
// If a rope can't be paired with another rope at the current level (i.e., odd numbers of ropes),
559-
// it needs to be promoted to the next level where it will be tried again. Since by definition
560-
// every rope already present in the next level must have occurred before this rope in the current
561-
// level, this rope must be added to the end of the list in the next level to maintain proper
562-
// position.
563-
nextLevelQueue.add(left);
564-
}
565-
} else {
566-
ManagedRope right = currentRopeQueue.pop();
567-
568-
if (right.depth() >= flattenThreshold) {
569-
// TODO (eregon, 15 June 2020): should this cache the resulting bytes by using getBytes()?
570-
right = flattenNode.executeFlatten(right);
571-
}
572-
573-
final ManagedRope child = new ConcatRope(
574-
left,
575-
right,
576-
rope.getEncoding(),
577-
commonCodeRange(left.getCodeRange(), right.getCodeRange()),
578-
depth(left, right),
579-
isBalanced(left, right));
580-
581-
nextLevelQueue.add(child);
582-
}
583-
584-
if (currentRopeQueue.isEmpty() && !nextLevelQueue.isEmpty()) {
585-
currentRopeQueue = nextLevelQueue;
586-
nextLevelQueue = new ArrayDeque<>();
587-
}
588-
}
589-
590-
return root;
591-
}
592-
593-
@TruffleBoundary
594-
private void linearizeTree(ManagedRope rope, Deque<ManagedRope> ropeQueue) {
595-
if (rope instanceof ConcatRope) {
596-
final ConcatRope concatRope = (ConcatRope) rope;
597-
598-
// If a rope is known to be balanced, there's no need to rebalance it.
599-
if (concatRope.isBalanced()) {
600-
ropeQueue.add(concatRope);
601-
} else {
602-
linearizeTree(concatRope.getLeft(), ropeQueue);
603-
linearizeTree(concatRope.getRight(), ropeQueue);
604-
}
605-
} else {
606-
// We never rebalance non-ConcatRopes since that requires per-rope type logic with likely minimal benefit.
607-
ropeQueue.add(rope);
608-
}
609-
}
610511

611512
@SuppressFBWarnings("RV")
612513
@Specialization(guards = { "!left.isEmpty()", "!right.isEmpty()", "isCodeRangeBroken(left, right)" })

0 commit comments

Comments
 (0)