Skip to content

Commit 35f1444

Browse files
author
Nicolas Laurent
committed
implement getByteSlow non-recursively
1 parent a38e6af commit 35f1444

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import org.jcodings.Encoding;
1313
import org.jcodings.specific.ASCIIEncoding;
1414

15-
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
16-
1715
public class ConcatRope extends ManagedRope {
1816

1917
private final ManagedRope left;
@@ -80,13 +78,8 @@ Rope withBinaryEncoding() {
8078
}
8179

8280
@Override
83-
@TruffleBoundary
8481
public byte getByteSlow(int index) {
85-
if (index < left.byteLength()) {
86-
return left.getByteSlow(index);
87-
}
88-
89-
return right.getByteSlow(index - left.byteLength());
82+
return RopeOperations.getByteSlow(this, index);
9083
}
9184

9285
public ManagedRope getLeft() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected byte[] getBytesSlow() {
7070

7171
@Override
7272
protected byte getByteSlow(int index) {
73-
return child.getByteSlow(index % child.byteLength());
73+
return RopeOperations.getByteSlow(this, index);
7474
}
7575

7676
public ManagedRope getChild() {

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,36 @@ public static byte[] flattenBytes(Rope rope) {
497497
return buffer;
498498
}
499499

500+
/** Used to implement {@link Rope#getByteSlow(int)} in a non-recursive fashion for some Rope subclasses. Do not call
501+
* directly, call {@link Rope#getByteSlow(int) instead.} */
502+
@TruffleBoundary
503+
static byte getByteSlow(Rope rope, int index) {
504+
while (true) {
505+
final byte[] rawBytes = rope.getRawBytes();
506+
if (rawBytes != null) {
507+
return rawBytes[index];
508+
} else if (rope instanceof ConcatRope) {
509+
final ConcatRope concatRope = (ConcatRope) rope;
510+
final Rope left = concatRope.getLeft();
511+
if (index < left.byteLength()) {
512+
rope = left;
513+
} else {
514+
rope = concatRope.getRight();
515+
index -= left.byteLength();
516+
}
517+
} else if (rope instanceof SubstringRope) {
518+
final SubstringRope substringRope = (SubstringRope) rope;
519+
rope = substringRope.getChild();
520+
index += substringRope.getByteOffset();
521+
} else if (rope instanceof RepeatingRope) {
522+
rope = ((RepeatingRope) rope).getChild();
523+
index %= rope.byteLength();
524+
} else {
525+
return rope.getByteSlow(index);
526+
}
527+
}
528+
}
529+
500530
private static int computeLoopCount(int offset, int times, int length, int patternLength) {
501531
// The loopCount has to be precisely determined so every repetition has at least some parts used.
502532
// It has to account for the beginning we don't need (offset), has to reach the end but, and must not

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected byte[] getBytesSlow() {
8282

8383
@Override
8484
public byte getByteSlow(int index) {
85-
return child.getByteSlow(index + byteOffset);
85+
return RopeOperations.getByteSlow(this, index);
8686
}
8787

8888
public ManagedRope getChild() {

0 commit comments

Comments
 (0)