Skip to content

Commit 0079332

Browse files
committed
Turn ALLOW_TO_STRING into a debug flag as in most cases it's OK to store the byte[]
* And preventing {RubyString,Rope}#toString() calls is really challenging and does not seem worth it. (cherry picked from commit b4fcc2c)
1 parent 98997de commit 0079332

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ public final byte[] getBytes() {
5454

5555
@Override
5656
public final String toString() {
57-
assert ALLOW_TO_STRING;
58-
59-
final byte[] bytesBefore = bytes;
60-
final String string = RopeOperations.decodeOrEscapeBinaryRope(this, RopeOperations.flattenBytes(this));
61-
assert bytes == bytesBefore : "bytes should not be modified by Rope#toString() as otherwise inspecting a Rope would have a side effect";
62-
return string;
57+
if (DEBUG_ROPE_BYTES) {
58+
final byte[] bytesBefore = bytes;
59+
final String string = RopeOperations.decodeOrEscapeBinaryRope(this, RopeOperations.flattenBytes(this));
60+
assert bytes == bytesBefore : "bytes should not be modified by Rope#toString() as otherwise inspecting a Rope would have a side effect";
61+
return string;
62+
} else {
63+
return RopeOperations.decodeRope(this);
64+
}
6365
}
6466

6567
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ public int hashCode() {
184184

185185
@Override
186186
public String toString() {
187-
assert ALLOW_TO_STRING;
188187
return toLeafRope().toString();
189188
}
190189

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public abstract class Rope implements Comparable<Rope> {
2020
// NativeRope, RepeatingRope, 3 LeafRope, ConcatRope, SubstringRope, 1 LazyRope
2121
public static final int NUMBER_OF_CONCRETE_CLASSES = 8;
2222

23-
// Useful for debugging. Setting to false allow to catch wrong usages.
24-
protected static final boolean ALLOW_TO_STRING = false;
23+
// Useful for debugging. Setting to true avoids ManagedRope#toString to populate bytes as a side-effect of the debugger calling toString().
24+
protected static final boolean DEBUG_ROPE_BYTES = false;
2525

2626
public final Encoding encoding;
2727
private final int byteLength;

0 commit comments

Comments
 (0)