Skip to content

Commit fda761a

Browse files
author
Nicolas Laurent
committed
make ManagedRope#getBytes NeverPartOfCompilation, and fix violations
1 parent b97777e commit fda761a

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

src/main/java/org/truffleruby/cext/CExtNodes.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -942,12 +942,14 @@ public abstract static class StringPointerSizeNode extends PrimitiveArrayArgumen
942942

943943
@Specialization(guards = "strings.isRubyString(string)")
944944
protected int size(Object string,
945-
@CachedLibrary(limit = "2") RubyStringLibrary strings) {
945+
@CachedLibrary(limit = "2") RubyStringLibrary strings,
946+
@Cached RopeNodes.BytesNode getBytes) {
946947
final Rope rope = strings.getRope(string);
948+
final byte[] bytes = getBytes.execute(rope);
947949
final int byteLength = rope.byteLength();
948950
int i = 0;
949951
for (; i < byteLength; i++) {
950-
if (rope.get(i) == 0) {
952+
if (bytes[i] == 0) {
951953
return i;
952954
}
953955
}
@@ -1209,8 +1211,9 @@ public abstract static class RbTrMbcCaseFoldNode extends CoreMethodArrayArgument
12091211
protected Object rbTrEncMbcCaseFold(RubyEncoding enc, int flags, Object string, Object write_p, Object p,
12101212
@CachedLibrary(limit = "2") RubyStringLibrary strings,
12111213
@CachedLibrary("write_p") InteropLibrary receivers,
1214+
@Cached RopeNodes.BytesNode getBytes,
12121215
@Cached TranslateInteropExceptionNode translateInteropExceptionNode) {
1213-
final byte[] bytes = strings.getRope(string).getBytes();
1216+
final byte[] bytes = getBytes.execute(strings.getRope(string));
12141217
final byte[] to = new byte[bytes.length];
12151218
final IntHolder intHolder = new IntHolder();
12161219
intHolder.value = 0;
@@ -1277,6 +1280,7 @@ public abstract static class RbEncMbLenNode extends CoreMethodArrayArgumentsNode
12771280
@Specialization(guards = "strings.isRubyString(string)")
12781281
protected Object rbEncMbLen(RubyEncoding enc, Object string, int p, int e,
12791282
@CachedLibrary(limit = "2") RubyStringLibrary strings,
1283+
@Cached RopeNodes.BytesNode getBytes,
12801284
@Cached RopeNodes.CodeRangeNode codeRangeNode,
12811285
@Cached ConditionProfile sameEncodingProfile) {
12821286
final Encoding encoding = enc.encoding;
@@ -1288,7 +1292,7 @@ protected Object rbEncMbLen(RubyEncoding enc, Object string, int p, int e,
12881292
sameEncodingProfile.profile(encoding == ropeEncoding)
12891293
? codeRangeNode.execute(rope)
12901294
: CodeRange.CR_UNKNOWN,
1291-
strings.getRope(string).getBytes(),
1295+
getBytes.execute(strings.getRope(string)),
12921296
p,
12931297
e,
12941298
true);
@@ -1331,6 +1335,7 @@ public abstract static class RbEncPreciseMbclenNode extends CoreMethodArrayArgum
13311335
protected int rbEncPreciseMbclen(RubyEncoding enc, Object string, int p, int end,
13321336
@CachedLibrary(limit = "2") RubyStringLibrary strings,
13331337
@Cached RopeNodes.CalculateCharacterLengthNode calculateCharacterLengthNode,
1338+
@Cached RopeNodes.BytesNode getBytes,
13341339
@Cached ConditionProfile sameEncodingProfile) {
13351340
final Encoding encoding = enc.encoding;
13361341
final Rope rope = strings.getRope(string);
@@ -1341,7 +1346,8 @@ protected int rbEncPreciseMbclen(RubyEncoding enc, Object string, int p, int end
13411346
cr = CodeRange.CR_UNKNOWN;
13421347
}
13431348

1344-
final int length = calculateCharacterLengthNode.characterLength(encoding, cr, rope.getBytes(), p, end);
1349+
final int length = calculateCharacterLengthNode
1350+
.characterLength(encoding, cr, getBytes.execute(rope), p, end);
13451351
assert end - p >= length; // assert this condition not reached: https://github.com/ruby/ruby/blob/46a5d1b4a63f624f2c5c5b6f710cc1a176c88b02/encoding.c#L1046
13461352
return length;
13471353
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package org.truffleruby.core.rope;
1111

12+
import com.oracle.truffle.api.CompilerAsserts;
1213
import org.jcodings.Encoding;
1314

1415
public abstract class ManagedRope extends Rope {
@@ -45,6 +46,7 @@ protected byte getByteSlow(int index) {
4546

4647
@Override
4748
public final byte[] getBytes() {
49+
CompilerAsserts.neverPartOfCompilation("Use RopeNodes.ByteNodes instead, or add a @TruffleBoundary.");
4850
if (bytes == null) {
4951
bytes = getBytesSlow();
5052
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,7 @@ protected int getByte(NativeRope rope, int index) {
958958
return rope.get(index) & 0xff;
959959
}
960960

961+
@TruffleBoundary
961962
@Specialization(guards = "rope.getRawBytes() == null")
962963
protected int getByte(LazyIntRope rope, int index) {
963964
return rope.getBytes()[index] & 0xff;
@@ -1384,11 +1385,12 @@ protected CodeRange getCodeRangeManaged(ManagedRope rope) {
13841385

13851386
@Specialization
13861387
protected CodeRange getCodeRangeNative(NativeRope rope,
1388+
@Cached BytesNode getBytes,
13871389
@Cached CalculateAttributesNode calculateAttributesNode,
13881390
@Cached ConditionProfile unknownCodeRangeProfile) {
13891391
if (unknownCodeRangeProfile.profile(rope.getRawCodeRange() == CR_UNKNOWN)) {
13901392
final StringAttributes attributes = calculateAttributesNode
1391-
.executeCalculateAttributes(rope.getEncoding(), rope.getBytes());
1393+
.executeCalculateAttributes(rope.getEncoding(), getBytes.execute(rope));
13921394
rope.updateAttributes(attributes);
13931395
return attributes.getCodeRange();
13941396
} else {
@@ -1433,12 +1435,13 @@ protected int getCharacterLengthManaged(ManagedRope rope) {
14331435

14341436
@Specialization
14351437
protected int getCharacterLengthNative(NativeRope rope,
1438+
@Cached BytesNode getBytes,
14361439
@Cached CalculateAttributesNode calculateAttributesNode,
14371440
@Cached ConditionProfile unknownCharacterLengthProfile) {
14381441
if (unknownCharacterLengthProfile
14391442
.profile(rope.rawCharacterLength() == NativeRope.UNKNOWN_CHARACTER_LENGTH)) {
14401443
final StringAttributes attributes = calculateAttributesNode
1441-
.executeCalculateAttributes(rope.getEncoding(), rope.getBytes());
1444+
.executeCalculateAttributes(rope.getEncoding(), getBytes.execute(rope));
14421445
rope.updateAttributes(attributes);
14431446
return attributes.getCharacterLength();
14441447
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public static byte[] extractRange(Rope rope, int offset, int length) {
290290
}
291291

292292
/** This method should not be used directly, because it does not cache the result in the Rope. Use
293-
* {@link Rope#getBytes()} instead.
293+
* {@link RopeNodes.BytesNode} or {@link Rope#getBytes()} instead.
294294
*
295295
* Performs an iterative depth first search of the Rope tree to calculate its byte[] without needing to populate the
296296
* byte[] for each level beneath. Every LeafRope has its byte[] populated by definition. The goal is to determine

src/main/java/org/truffleruby/core/string/StringNodes.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2108,6 +2108,7 @@ public abstract static class DumpNode extends CoreMethodArrayArgumentsNode {
21082108

21092109
@Child private RopeNodes.MakeLeafRopeNode makeLeafRopeNode = RopeNodes.MakeLeafRopeNode.create();
21102110

2111+
@TruffleBoundary
21112112
@Specialization(guards = "isAsciiCompatible(libString.getRope(string))")
21122113
protected RubyString dumpAsciiCompatible(Object string,
21132114
@Cached LogicalClassNode logicalClassNode,
@@ -3932,6 +3933,7 @@ protected Object stringFindCharacterSingleByte(Object string, int offset,
39323933
"!isSingleByteOptimizable(strings.getRope(string), singleByteOptimizableNode)" })
39333934
protected Object stringFindCharacter(Object string, int offset,
39343935
@CachedLibrary(limit = "2") RubyStringLibrary strings,
3936+
@Cached RopeNodes.BytesNode getBytes,
39353937
@Cached RopeNodes.CalculateCharacterLengthNode calculateCharacterLengthNode,
39363938
@Cached RopeNodes.CodeRangeNode codeRangeNode,
39373939
@Cached RopeNodes.SingleByteOptimizableNode singleByteOptimizableNode) {
@@ -3942,7 +3944,7 @@ protected Object stringFindCharacter(Object string, int offset,
39423944
final CodeRange cr = codeRangeNode.execute(rope);
39433945

39443946
final int clen = calculateCharacterLengthNode
3945-
.characterLength(enc, cr, rope.getBytes(), offset, offset + enc.maxLength());
3947+
.characterLength(enc, cr, getBytes.execute(rope), offset, offset + enc.maxLength());
39463948

39473949
return substringNode.executeSubstring(string, offset, clen);
39483950
}

0 commit comments

Comments
 (0)