Skip to content

Commit f13f5da

Browse files
author
Nicolas Laurent
committed
[GR-26735] Stop tracking depth & balance of ropes.
PullRequest: truffleruby/2074
2 parents 7b5cb93 + aa8f330 commit f13f5da

File tree

11 files changed

+30
-85
lines changed

11 files changed

+30
-85
lines changed

src/main/java/org/truffleruby/collections/BoundedIntStack.java renamed to src/main/java/org/truffleruby/collections/IntStack.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@
99
*/
1010
package org.truffleruby.collections;
1111

12-
public final class BoundedIntStack {
12+
import org.truffleruby.core.array.ArrayUtils;
1313

14-
final int[] storage;
14+
public final class IntStack {
15+
16+
int[] storage;
1517
int index = -1;
1618

17-
public BoundedIntStack(int length) {
19+
public IntStack() {
20+
this(16);
21+
}
22+
23+
public IntStack(int length) {
1824
this.storage = new int[length];
1925
}
2026

@@ -23,7 +29,10 @@ public boolean isEmpty() {
2329
}
2430

2531
public void push(int value) {
26-
storage[++index] = value;
32+
if (++index == storage.length) {
33+
storage = ArrayUtils.grow(storage, storage.length * 2);
34+
}
35+
storage[index] = value;
2736
}
2837

2938
public int peek() {
@@ -33,5 +42,4 @@ public int peek() {
3342
public int pop() {
3443
return storage[index--];
3544
}
36-
3745
}

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

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@ public class ConcatRope extends ManagedRope {
1616

1717
private final ManagedRope left;
1818
private final ManagedRope right;
19-
private final boolean balanced;
2019

2120
public ConcatRope(
2221
ManagedRope left,
2322
ManagedRope right,
2423
Encoding encoding,
25-
CodeRange codeRange,
26-
int depth,
27-
boolean balanced) {
28-
this(left, right, encoding, codeRange, left.characterLength() + right.characterLength(), depth, null, balanced);
24+
CodeRange codeRange) {
25+
this(left, right, encoding, codeRange, left.characterLength() + right.characterLength(), null);
2926
}
3027

3128
private ConcatRope(
@@ -34,19 +31,15 @@ private ConcatRope(
3431
Encoding encoding,
3532
CodeRange codeRange,
3633
int characterLength,
37-
int depth,
38-
byte[] bytes,
39-
boolean balanced) {
34+
byte[] bytes) {
4035
super(
4136
encoding,
4237
codeRange,
4338
left.byteLength() + right.byteLength(),
4439
characterLength,
45-
depth,
4640
bytes);
4741
this.left = left;
4842
this.right = right;
49-
this.balanced = balanced;
5043
}
5144

5245
@Override
@@ -58,9 +51,7 @@ Rope withEncoding7bit(Encoding newEncoding) {
5851
newEncoding,
5952
CodeRange.CR_7BIT,
6053
characterLength(),
61-
depth(),
62-
getRawBytes(),
63-
balanced);
54+
getRawBytes());
6455
}
6556

6657
@Override
@@ -72,9 +63,7 @@ Rope withBinaryEncoding() {
7263
ASCIIEncoding.INSTANCE,
7364
CodeRange.CR_VALID,
7465
byteLength(),
75-
depth(),
76-
getRawBytes(),
77-
balanced);
66+
getRawBytes());
7867
}
7968

8069
public ManagedRope getLeft() {
@@ -84,9 +73,4 @@ public ManagedRope getLeft() {
8473
public ManagedRope getRight() {
8574
return right;
8675
}
87-
88-
public boolean isBalanced() {
89-
return balanced;
90-
}
91-
9276
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public LazyIntRope(int value) {
2424
}
2525

2626
protected LazyIntRope(int value, Encoding encoding, int length) {
27-
super(encoding, CodeRange.CR_7BIT, length, length, 1, null);
27+
super(encoding, CodeRange.CR_7BIT, length, length, null);
2828
this.value = value;
2929
assert Integer.toString(value).length() == length;
3030
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
public abstract class LeafRope extends ManagedRope {
1515

1616
public LeafRope(byte[] bytes, Encoding encoding, CodeRange codeRange, int characterLength) {
17-
super(encoding, codeRange, bytes.length, characterLength, 1, bytes);
17+
super(encoding, codeRange, bytes.length, characterLength, bytes);
1818
}
1919

2020
@Override

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ protected ManagedRope(
2121
CodeRange codeRange,
2222
int byteLength,
2323
int characterLength,
24-
int ropeDepth,
2524
byte[] bytes) {
26-
super(encoding, byteLength, ropeDepth, bytes);
25+
super(encoding, byteLength, bytes);
2726

2827
this.codeRange = codeRange;
2928
this.characterLength = characterLength;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public NativeRope(
3636
}
3737

3838
private NativeRope(Pointer pointer, int byteLength, Encoding encoding, int characterLength, CodeRange codeRange) {
39-
super(encoding, byteLength, 1, null);
39+
super(encoding, byteLength, null);
4040

4141
assert (codeRange == CodeRange.CR_UNKNOWN) == (characterLength == UNKNOWN_CHARACTER_LENGTH);
4242
this.codeRange = codeRange;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public RepeatingRope(ManagedRope child, int times, int byteLength) {
2525
child.getCodeRange(),
2626
byteLength,
2727
child.characterLength() * times,
28-
child.depth() + 1,
2928
null);
3029
this.child = child;
3130
this.times = times;

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,14 @@ public abstract class Rope implements Comparable<Rope> {
2525

2626
public final Encoding encoding;
2727
private final int byteLength;
28-
private final int ropeDepth;
2928
private int hashCode = 0;
3029
protected byte[] bytes;
3130

32-
protected Rope(Encoding encoding, int byteLength, int ropeDepth, byte[] bytes) {
31+
protected Rope(Encoding encoding, int byteLength, byte[] bytes) {
3332
assert encoding != null;
3433

3534
this.encoding = encoding;
3635
this.byteLength = byteLength;
37-
this.ropeDepth = ropeDepth;
3836
this.bytes = bytes;
3937
}
4038

@@ -87,10 +85,6 @@ public final boolean isAsciiOnly() {
8785
return getCodeRange() == CodeRange.CR_7BIT;
8886
}
8987

90-
public final int depth() {
91-
return ropeDepth;
92-
}
93-
9488
@Override
9589
@TruffleBoundary
9690
public int hashCode() {

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

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,6 @@ protected Rope concat(ManagedRope left, ManagedRope right, Encoding encoding,
472472
getContext().getCoreExceptions().argumentErrorTooLargeString(this));
473473
}
474474

475-
int depth = depth(left, right);
476-
477475
return new ConcatRope(
478476
left,
479477
right,
@@ -482,33 +480,9 @@ protected Rope concat(ManagedRope left, ManagedRope right, Encoding encoding,
482480
left.getCodeRange(),
483481
right.getCodeRange(),
484482
sameCodeRangeProfile,
485-
brokenCodeRangeProfile),
486-
depth,
487-
isBalanced(left, right));
488-
}
489-
490-
private boolean isBalanced(Rope left, Rope right) {
491-
// Our definition of balanced is centered around the notion of rebalancing. We could have a simple structure
492-
// such as ConcatRope(ConcatRope(LeafRope, LeafRope), LeafRope) that is balanced on its own but may contribute
493-
// to an unbalanced rope when combined with another rope of similar structure. To keep things simple, we only
494-
// consider ConcatRopes that consist of two non-ConcatRopes balanced as the base case and ConcatRopes that
495-
// have balanced ConcatRopes for both children are balanced by induction.
496-
if (left instanceof ConcatRope) {
497-
if (right instanceof ConcatRope) {
498-
return ((ConcatRope) left).isBalanced() && ((ConcatRope) right).isBalanced();
499-
}
500-
501-
return false;
502-
} else {
503-
// We treat the concatenation of two non-ConcatRopes as balanced, even if their children not balanced.
504-
// E.g., a SubstringRope whose child is an unbalanced ConcatRope arguable isn't balanced. However,
505-
// the code is much simpler by handling it this way. Balanced ConcatRopes will never rebalance, but
506-
// if they become part of a larger subtree that exceeds the depth threshold, they may be flattened.
507-
return !(right instanceof ConcatRope);
508-
}
483+
brokenCodeRangeProfile));
509484
}
510485

511-
512486
@SuppressFBWarnings("RV")
513487
@Specialization(guards = { "!left.isEmpty()", "!right.isEmpty()", "isCodeRangeBroken(left, right)" })
514488
protected Rope concatCrBroken(ManagedRope left, ManagedRope right, Encoding encoding,
@@ -561,10 +535,6 @@ public static CodeRange commonCodeRange(CodeRange first, CodeRange second) {
561535
return CR_VALID;
562536
}
563537

564-
private int depth(Rope left, Rope right) {
565-
return Math.max(left.depth(), right.depth()) + 1;
566-
}
567-
568538
protected static boolean isCodeRangeBroken(ManagedRope first, ManagedRope second) {
569539
return first.getCodeRange() == CR_BROKEN || second.getCodeRange() == CR_BROKEN;
570540
}
@@ -836,14 +806,13 @@ protected Object debugPrintLeafRope(LeafRope rope, int currentLevel, boolean pri
836806
final boolean bytesAreNull = rope.getRawBytes() == null;
837807

838808
System.err.println(StringUtils.format(
839-
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; D: %d; E: %s)",
809+
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; E: %s)",
840810
printString ? rope.toString() : "<skipped>",
841811
rope.getClass().getSimpleName(),
842812
bytesAreNull,
843813
rope.byteLength(),
844814
rope.characterLength(),
845815
rope.getCodeRange(),
846-
rope.depth(),
847816
rope.getEncoding()));
848817

849818
return nil;
@@ -858,15 +827,14 @@ protected Object debugPrintSubstringRope(SubstringRope rope, int currentLevel, b
858827
final boolean bytesAreNull = rope.getRawBytes() == null;
859828

860829
System.err.println(StringUtils.format(
861-
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; O: %d; D: %d; E: %s)",
830+
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; O: %d; E: %s)",
862831
printString ? rope.toString() : "<skipped>",
863832
rope.getClass().getSimpleName(),
864833
bytesAreNull,
865834
rope.byteLength(),
866835
rope.characterLength(),
867836
rope.getCodeRange(),
868837
rope.getByteOffset(),
869-
rope.depth(),
870838
rope.getEncoding()));
871839

872840
executeDebugPrint(rope.getChild(), currentLevel + 1, printString);
@@ -884,16 +852,13 @@ protected Object debugPrintConcatRope(ConcatRope rope, int currentLevel, boolean
884852

885853
System.err
886854
.println(StringUtils.format(
887-
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; D: %d; LD: %d; RD: %d; E: %s)",
855+
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; E: %s)",
888856
printString ? rope.toString() : "<skipped>",
889857
rope.getClass().getSimpleName(),
890858
bytesAreNull,
891859
rope.byteLength(),
892860
rope.characterLength(),
893861
rope.getCodeRange(),
894-
rope.depth(),
895-
rope.getLeft().depth(),
896-
rope.getRight().depth(),
897862
rope.getEncoding()));
898863

899864
executeDebugPrint(rope.getLeft(), currentLevel + 1, printString);
@@ -919,7 +884,6 @@ protected Object debugPrintRepeatingRope(RepeatingRope rope, int currentLevel, b
919884
rope.characterLength(),
920885
rope.getCodeRange(),
921886
rope.getTimes(),
922-
rope.depth(),
923887
rope.getEncoding()));
924888

925889
executeDebugPrint(rope.getChild(), currentLevel + 1, printString);
@@ -944,7 +908,6 @@ protected Object debugPrintLazyInt(LazyIntRope rope, int currentLevel, boolean p
944908
rope.characterLength(),
945909
rope.getCodeRange(),
946910
rope.getValue(),
947-
rope.depth(),
948911
rope.getEncoding()));
949912

950913
return nil;

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import org.jcodings.specific.ASCIIEncoding;
3737
import org.jcodings.specific.USASCIIEncoding;
3838
import org.jcodings.specific.UTF8Encoding;
39-
import org.truffleruby.collections.BoundedIntStack;
39+
import org.truffleruby.collections.IntStack;
4040
import org.truffleruby.core.Hashing;
4141
import org.truffleruby.core.encoding.EncodingManager;
4242
import org.truffleruby.core.rope.RopeNodesFactory.WithEncodingNodeGen;
@@ -320,9 +320,7 @@ public static byte[] flattenBytes(Rope rope) {
320320
// need to track each SubstringRope's bounded length and how much that bounded length contributes to the total
321321
// byte[] for any ancestor (e.g., a SubstringRope of a ConcatRope with SubstringRopes for each of its children).
322322
// Because we need to track multiple levels, we can't use a single updated int.
323-
// SubstringRope.child is never a SubstringRope, so there are most depth / 2 SubstringRope.
324-
// TODO (eregon, 9 July 2020): maybe we should resize dynamically to avoid a too big array if the depth option is large?
325-
final BoundedIntStack substringLengths = new BoundedIntStack(rope.depth() / 2);
323+
final IntStack substringLengths = new IntStack();
326324

327325
final Deque<Rope> workStack = new ArrayDeque<>();
328326
workStack.push(rope);

0 commit comments

Comments
 (0)