Skip to content

Commit 7b3fe84

Browse files
author
Nicolas Laurent
committed
refactor Rope#withEncoding methods to take a profile for the sake of ConcatRope#getState
1 parent cec0b0c commit 7b3fe84

File tree

10 files changed

+41
-43
lines changed

10 files changed

+41
-43
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.truffleruby.core.rope;
1212

13+
import com.oracle.truffle.api.profiles.ConditionProfile;
1314
import org.jcodings.Encoding;
1415

1516
import com.oracle.truffle.api.CompilerDirectives;
@@ -23,12 +24,12 @@ public AsciiOnlyLeafRope(byte[] bytes, Encoding encoding) {
2324
}
2425

2526
@Override
26-
Rope withEncoding7bit(Encoding newEncoding) {
27+
Rope withEncoding7bit(Encoding newEncoding, ConditionProfile bytesNotNull) {
2728
return new AsciiOnlyLeafRope(getRawBytes(), newEncoding);
2829
}
2930

3031
@Override
31-
Rope withBinaryEncoding() {
32+
Rope withBinaryEncoding(ConditionProfile bytesNotNull) {
3233
CompilerDirectives.transferToInterpreterAndInvalidate();
3334
throw new UnsupportedOperationException("Must only be called for CR_VALID Strings");
3435
}

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

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,20 @@ private ConcatRope(
7373
}
7474

7575
@Override
76-
Rope withEncoding7bit(Encoding newEncoding) {
76+
Rope withEncoding7bit(Encoding newEncoding, ConditionProfile bytesNotNull) {
7777
assert getCodeRange() == CodeRange.CR_7BIT;
78-
return withEncoding(newEncoding, CodeRange.CR_7BIT, characterLength());
78+
return withEncoding(newEncoding, CodeRange.CR_7BIT, characterLength(), bytesNotNull);
7979
}
8080

8181
@Override
82-
Rope withBinaryEncoding() {
82+
Rope withBinaryEncoding(ConditionProfile bytesNotNull) {
8383
assert getCodeRange() == CodeRange.CR_VALID;
84-
return withEncoding(ASCIIEncoding.INSTANCE, CodeRange.CR_VALID, byteLength());
84+
return withEncoding(ASCIIEncoding.INSTANCE, CodeRange.CR_VALID, byteLength(), bytesNotNull);
8585
}
8686

87-
private ConcatRope withEncoding(Encoding encoding, CodeRange codeRange, int characterLength) {
88-
final ConcatState state = getState();
87+
private ConcatRope withEncoding(Encoding encoding, CodeRange codeRange, int characterLength,
88+
ConditionProfile bytesNotNull) {
89+
final ConcatState state = getState(bytesNotNull);
8990
return new ConcatRope(state.left, state.right, encoding, codeRange, byteLength(), characterLength, state.bytes);
9091
}
9192

@@ -102,24 +103,8 @@ protected byte[] getBytesSlow() {
102103
* <p>
103104
* This version is not allowed in compiled code, use {@link #getState(ConditionProfile)} there instead. */
104105
public ConcatState getState() {
105-
CompilerAsserts.neverPartOfCompilation("use getState(ConditionProfile) instead!");
106-
107-
if (this.bytes != null) {
108-
return new ConcatState(null, null, this.bytes);
109-
}
110-
111-
final ManagedRope left = this.left;
112-
final ManagedRope right = this.right;
113-
if (left != null && right != null) {
114-
return new ConcatState(left, right, null);
115-
}
116-
117-
CompilerDirectives.transferToInterpreterAndInvalidate();
118-
if (this.bytes != null) {
119-
throw CompilerDirectives
120-
.shouldNotReachHere("our assumptions about reordering and memory barriers seem incorrect");
121-
}
122-
return new ConcatState(null, null, this.bytes);
106+
CompilerAsserts.neverPartOfCompilation("Use #getState(ConditionProfile) instead.");
107+
return getState(ConditionProfile.getUncached());
123108
}
124109

125110
/** Access the state in a way that prevents race conditions.
@@ -138,7 +123,11 @@ public ConcatState getState(ConditionProfile bytesNotNull) {
138123
}
139124

140125
CompilerDirectives.transferToInterpreterAndInvalidate();
141-
assert this.bytes != null;
126+
if (this.bytes != null) {
127+
throw CompilerDirectives
128+
.shouldNotReachHere("our assumptions about reordering and memory barriers seem incorrect");
129+
}
130+
142131
return new ConcatState(null, null, this.bytes);
143132
}
144133
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.truffleruby.core.rope;
1212

13+
import com.oracle.truffle.api.profiles.ConditionProfile;
1314
import org.jcodings.Encoding;
1415

1516
import com.oracle.truffle.api.CompilerDirectives;
@@ -23,13 +24,13 @@ public InvalidLeafRope(byte[] bytes, Encoding encoding, int characterLength) {
2324
}
2425

2526
@Override
26-
Rope withEncoding7bit(Encoding newEncoding) {
27+
Rope withEncoding7bit(Encoding newEncoding, ConditionProfile bytesNotNull) {
2728
CompilerDirectives.transferToInterpreterAndInvalidate();
2829
throw new UnsupportedOperationException("Must only be called for ASCII-only Strings");
2930
}
3031

3132
@Override
32-
Rope withBinaryEncoding() {
33+
Rope withBinaryEncoding(ConditionProfile bytesNotNull) {
3334
CompilerDirectives.transferToInterpreterAndInvalidate();
3435
throw new UnsupportedOperationException("Must only be called for CR_VALID Strings");
3536
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.truffleruby.core.rope;
1111

1212
import com.oracle.truffle.api.CompilerDirectives;
13+
import com.oracle.truffle.api.profiles.ConditionProfile;
1314
import org.jcodings.Encoding;
1415
import org.jcodings.specific.USASCIIEncoding;
1516

@@ -52,13 +53,13 @@ private static int length(int value) {
5253
}
5354

5455
@Override
55-
Rope withEncoding7bit(Encoding newEncoding) {
56+
Rope withEncoding7bit(Encoding newEncoding, ConditionProfile bytesNotNull) {
5657
assert getCodeRange() == CodeRange.CR_7BIT;
5758
return new LazyIntRope(value, newEncoding, length(value));
5859
}
5960

6061
@Override
61-
Rope withBinaryEncoding() {
62+
Rope withBinaryEncoding(ConditionProfile bytesNotNull) {
6263
CompilerDirectives.transferToInterpreterAndInvalidate();
6364
throw new UnsupportedOperationException("Must only be called for CR_VALID Strings");
6465
}

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

Lines changed: 3 additions & 2 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.profiles.ConditionProfile;
1213
import org.jcodings.Encoding;
1314
import org.jcodings.specific.ASCIIEncoding;
1415
import org.truffleruby.core.FinalizationService;
@@ -188,12 +189,12 @@ public String toString() {
188189
}
189190

190191
@Override
191-
Rope withEncoding7bit(Encoding newEncoding) {
192+
Rope withEncoding7bit(Encoding newEncoding, ConditionProfile bytesNotNull) {
192193
return withEncoding(newEncoding);
193194
}
194195

195196
@Override
196-
Rope withBinaryEncoding() {
197+
Rope withBinaryEncoding(ConditionProfile bytesNotNull) {
197198
return withEncoding(ASCIIEncoding.INSTANCE);
198199
}
199200

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.truffleruby.core.rope;
1212

13+
import com.oracle.truffle.api.profiles.ConditionProfile;
1314
import org.jcodings.Encoding;
1415
import org.jcodings.specific.ASCIIEncoding;
1516

@@ -31,13 +32,13 @@ public RepeatingRope(ManagedRope child, int times, int byteLength) {
3132
}
3233

3334
@Override
34-
Rope withEncoding7bit(Encoding newEncoding) {
35+
Rope withEncoding7bit(Encoding newEncoding, ConditionProfile bytesNotNull) {
3536
assert getCodeRange() == CodeRange.CR_7BIT;
3637
return new RepeatingRope((ManagedRope) RopeOperations.withEncoding(child, newEncoding), times, byteLength());
3738
}
3839

3940
@Override
40-
Rope withBinaryEncoding() {
41+
Rope withBinaryEncoding(ConditionProfile bytesNotNull) {
4142
assert getCodeRange() == CodeRange.CR_VALID;
4243
return new RepeatingRope(
4344
(ManagedRope) RopeOperations.withEncoding(child, ASCIIEncoding.INSTANCE),

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.util.Arrays;
1313

14+
import com.oracle.truffle.api.profiles.ConditionProfile;
1415
import org.jcodings.Encoding;
1516

1617
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -38,11 +39,11 @@ protected Rope(Encoding encoding, int byteLength, byte[] bytes) {
3839

3940
/** Only used internally by WithEncodingNode. Returns a Rope with the given Encoding. Both the original and new
4041
* Encodings must be ASCII-compatible and the rope must be {@link #isAsciiOnly()}. */
41-
abstract Rope withEncoding7bit(Encoding newEncoding);
42+
abstract Rope withEncoding7bit(Encoding newEncoding, ConditionProfile bytesNotNull);
4243

4344
/** Only used internally by WithEncodingNode. Returns a Rope with the BINARY Encoding. The original Encoding must be
4445
* ASCII-compatible and {@link #getCodeRange()} must be {@link CodeRange#CR_VALID} to call this. */
45-
abstract Rope withBinaryEncoding();
46+
abstract Rope withBinaryEncoding(ConditionProfile bytesNotNull);
4647

4748
public abstract int characterLength();
4849

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,18 +923,19 @@ protected Rope withEncodingAsciiCompatible(ManagedRope rope, Encoding encoding,
923923
@Cached ConditionProfile asciiCompatibleProfile,
924924
@Cached ConditionProfile asciiOnlyProfile,
925925
@Cached ConditionProfile binaryEncodingProfile,
926+
@Cached ConditionProfile bytesNotNull,
926927
@Cached BytesNode bytesNode,
927928
@Cached MakeLeafRopeNode makeLeafRopeNode) {
928929

929930
if (asciiCompatibleProfile.profile(encoding.isAsciiCompatible())) {
930931
if (asciiOnlyProfile.profile(rope.isAsciiOnly())) {
931932
// ASCII-only strings can trivially convert to other ASCII-compatible encodings.
932-
return cachedRopeClass.cast(rope).withEncoding7bit(encoding);
933+
return cachedRopeClass.cast(rope).withEncoding7bit(encoding, bytesNotNull);
933934
} else if (binaryEncodingProfile.profile(encoding == ASCIIEncoding.INSTANCE &&
934935
rope.getCodeRange() == CR_VALID &&
935936
rope.getEncoding().isAsciiCompatible())) {
936937
// ASCII-compatible CR_VALID strings are also CR_VALID in binary, but they might change character length.
937-
final Rope binary = cachedRopeClass.cast(rope).withBinaryEncoding();
938+
final Rope binary = cachedRopeClass.cast(rope).withBinaryEncoding(bytesNotNull);
938939
assert binary.getCodeRange() == CR_VALID;
939940
return binary;
940941
} else {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.truffleruby.core.rope;
1212

13+
import com.oracle.truffle.api.profiles.ConditionProfile;
1314
import org.jcodings.Encoding;
1415
import org.jcodings.specific.ASCIIEncoding;
1516

@@ -46,7 +47,7 @@ private SubstringRope(
4647
}
4748

4849
@Override
49-
Rope withEncoding7bit(Encoding newEncoding) {
50+
Rope withEncoding7bit(Encoding newEncoding, ConditionProfile bytesNotNull) {
5051
assert getCodeRange() == CodeRange.CR_7BIT;
5152
return new SubstringRope(
5253
getChild(),
@@ -58,7 +59,7 @@ Rope withEncoding7bit(Encoding newEncoding) {
5859
}
5960

6061
@Override
61-
Rope withBinaryEncoding() {
62+
Rope withBinaryEncoding(ConditionProfile bytesNotNull) {
6263
assert getCodeRange() == CodeRange.CR_VALID;
6364
return new SubstringRope(
6465
getChild(),

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.truffleruby.core.rope;
1212

13+
import com.oracle.truffle.api.profiles.ConditionProfile;
1314
import org.jcodings.Encoding;
1415
import org.jcodings.specific.ASCIIEncoding;
1516

@@ -25,13 +26,13 @@ public ValidLeafRope(byte[] bytes, Encoding encoding, int characterLength) {
2526
}
2627

2728
@Override
28-
Rope withEncoding7bit(Encoding newEncoding) {
29+
Rope withEncoding7bit(Encoding newEncoding, ConditionProfile bytesNotNull) {
2930
CompilerDirectives.transferToInterpreterAndInvalidate();
3031
throw new UnsupportedOperationException("Must only be called for ASCII-only Strings");
3132
}
3233

3334
@Override
34-
Rope withBinaryEncoding() {
35+
Rope withBinaryEncoding(ConditionProfile bytesNotNull) {
3536
return new ValidLeafRope(getRawBytes(), ASCIIEncoding.INSTANCE, byteLength());
3637
}
3738
}

0 commit comments

Comments
 (0)