Skip to content

Commit 5fa7d8b

Browse files
committed
TruffleString: use static strings on single-byte string in FromByteArrayNode and SubstringNode
1 parent a41f94e commit 5fa7d8b

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

truffle/src/com.oracle.truffle.api.strings/src/com/oracle/truffle/api/strings/TStringInternalNodes.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ static TruffleString fromBufferWithStringCompaction(Node node, Object dataA, int
317317
@Cached InlinedConditionProfile utf32Profile,
318318
@Cached InlinedConditionProfile utf32Compact0Profile,
319319
@Cached InlinedConditionProfile utf32Compact1Profile,
320+
@Cached InlinedConditionProfile singleByteProfile,
320321
@Cached InlinedByteValueProfile unlikelyEncodingProfile) {
321322
assert dataA instanceof byte[] || dataA instanceof NativePointer;
322323
try {
@@ -343,6 +344,9 @@ static TruffleString fromBufferWithStringCompaction(Node node, Object dataA, int
343344
codeRange = StringAttributes.getCodeRange(attrs);
344345
stride = Stride.fromCodeRangeUTF16(codeRange);
345346
if (copy || stride == 0) {
347+
if (singleByteProfile.profile(node, isCacheHead && length == 1 && stride == 0)) {
348+
return TStringConstants.getSingleByte(Encoding.UTF_16, TStringOps.readS1(arrayA, offsetA, 1, 0));
349+
}
346350
offset = 0;
347351
array = new byte[length << stride];
348352
if (utf16CompactProfile.profile(node, stride == 0)) {
@@ -361,6 +365,9 @@ static TruffleString fromBufferWithStringCompaction(Node node, Object dataA, int
361365
codePointLength = length;
362366
stride = Stride.fromCodeRangeUTF32(codeRange);
363367
if (copy || stride < 2) {
368+
if (singleByteProfile.profile(node, isCacheHead && length == 1 && stride == 0)) {
369+
return TStringConstants.getSingleByte(Encoding.UTF_32, TStringOps.readS2(arrayA, offsetA, 1, 0));
370+
}
364371
offset = 0;
365372
array = new byte[length << stride];
366373
if (utf32Compact0Profile.profile(node, stride == 0)) {
@@ -379,6 +386,9 @@ static TruffleString fromBufferWithStringCompaction(Node node, Object dataA, int
379386
stride = 0;
380387
final long attrs;
381388
if (utf8Profile.profile(node, isUTF8(encoding))) {
389+
if (singleByteProfile.profile(node, isCacheHead && length == 1)) {
390+
return TStringConstants.getSingleByte(Encoding.UTF_8, TStringOps.readS0(arrayA, offsetA, 1, 0));
391+
}
382392
attrs = TStringOps.calcStringAttributesUTF8(node, arrayA, offsetA, length, false, false, utf8BrokenProfile);
383393
} else {
384394
attrs = unlikelyCases(node, arrayA, offsetA, byteLength, encoding, unlikelyEncodingProfile.profile(node, encoding.id));
@@ -1410,7 +1420,8 @@ static TruffleString materializeSubstring(Node node, AbstractTruffleString a, by
14101420
@SuppressWarnings("unused") boolean lazy,
14111421
@Shared("attributes") @Cached CalcStringAttributesNode calcAttributesNode,
14121422
@Exclusive @Cached InlinedConditionProfile utf16Profile,
1413-
@Exclusive @Cached InlinedConditionProfile utf32Profile) {
1423+
@Exclusive @Cached InlinedConditionProfile utf32Profile,
1424+
@Exclusive @Cached InlinedConditionProfile singleByteProfile) {
14141425
final long attrs;
14151426
final int codeRange;
14161427
final int stride;
@@ -1431,6 +1442,9 @@ static TruffleString materializeSubstring(Node node, AbstractTruffleString a, by
14311442
codeRange = StringAttributes.getCodeRange(attrs);
14321443
newStride = 0;
14331444
}
1445+
if (singleByteProfile.profile(node, length == 1 && newStride == 0 && encoding.isSupported())) {
1446+
return TStringConstants.getSingleByte(encoding, TStringOps.readValue(arrayA, offsetA, a.length(), stride, fromIndex));
1447+
}
14341448
byte[] newBytes = TStringOps.arraycopyOfWithStride(node, arrayA, offsetA + (fromIndex << stride), length, stride, length, newStride);
14351449
return TruffleString.createFromByteArray(newBytes, length, newStride, encoding, StringAttributes.getCodePointLength(attrs), codeRange);
14361450
}
@@ -1440,11 +1454,15 @@ static TruffleString createLazySubstring(Node node, TruffleString a, byte[] arra
14401454
@SuppressWarnings("unused") boolean lazy,
14411455
@Shared("attributes") @Cached CalcStringAttributesNode calcAttributesNode,
14421456
@Exclusive @Cached InlinedConditionProfile stride1MustMaterializeProfile,
1443-
@Exclusive @Cached InlinedConditionProfile stride2MustMaterializeProfile) {
1457+
@Exclusive @Cached InlinedConditionProfile stride2MustMaterializeProfile,
1458+
@Exclusive @Cached InlinedConditionProfile singleByteProfile) {
14441459
long lazyOffset = offsetA + (fromIndex << a.stride());
14451460
long attrs = calcAttributesNode.execute(node, a, arrayA, offsetA, length, a.stride(), encoding, fromIndex, codeRangeA);
14461461
int codeRange = StringAttributes.getCodeRange(attrs);
14471462
int codePointLength = StringAttributes.getCodePointLength(attrs);
1463+
if (singleByteProfile.profile(node, length == 1 && Stride.fromCodeRange(codeRange, encoding) == 0 && encoding.isSupported())) {
1464+
return TStringConstants.getSingleByte(encoding, TStringOps.readValue(arrayA, offsetA, a.length(), a.stride(), fromIndex));
1465+
}
14481466
final Object data;
14491467
final int offset;
14501468
final int stride;

0 commit comments

Comments
 (0)