Skip to content

Commit

Permalink
Automatic merge of master into galahad
Browse files Browse the repository at this point in the history
  • Loading branch information
OracleLabsAutomation committed Jun 1, 2024
2 parents 93923e4 + 98b72db commit a29b4da
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,28 @@ public static Iterable<TruffleStringBuilder.AppendSubstringByteIndexNode> data()
public void testAll() throws Exception {
forAllStrings(true, (a, arrayA, codeRangeA, isValidA, encodingA, codepointsA, byteIndicesA) -> {
forAllStrings(new TruffleString.Encoding[]{encodingA}, true, (b, arrayB, codeRangeB, isValidB, encodingB, codepointsB, byteIndicesB) -> {
TruffleStringBuilder sb = TruffleStringBuilder.create(encodingA);
node.execute(sb, a, 0, arrayA.length);
int fromByteIndexB = codepointsB.length > 1 ? byteIndicesB[1] : 0;
int byteLengthB = (codepointsB.length > 2 ? byteIndicesB[codepointsB.length - 1] : arrayB.length) - fromByteIndexB;
assert byteLengthB > 0;
node.execute(sb, b, fromByteIndexB, byteLengthB);
byte[] expected = Arrays.copyOf(arrayA, arrayA.length + byteLengthB);
System.arraycopy(arrayB, fromByteIndexB, expected, arrayA.length, byteLengthB);
assertBytesEqual(sb.toStringUncached(), encodingA, expected);
forAllStrings(new TruffleString.Encoding[]{encodingA}, true, (c, arrayC, codeRangeC, isValidC, encodingC, codepointsC, byteIndicesC) -> {

TruffleStringBuilder sb = TruffleStringBuilder.create(encodingA);
node.execute(sb, a, 0, arrayA.length);
int fromByteIndexB = codepointsB.length > 1 ? byteIndicesB[1] : 0;
int byteLengthB = (codepointsB.length > 2 ? byteIndicesB[codepointsB.length - 1] : arrayB.length) - fromByteIndexB;
assert byteLengthB > 0;
node.execute(sb, b, fromByteIndexB, byteLengthB);
byte[] expected = Arrays.copyOf(arrayA, arrayA.length + byteLengthB);
System.arraycopy(arrayB, fromByteIndexB, expected, arrayA.length, byteLengthB);
assertBytesEqual(sb.toStringUncached(), encodingA, expected);

sb = TruffleStringBuilder.create(encodingA);
node.execute(sb, a, 0, arrayA.length);
TruffleString concat = b.concatUncached(c, encodingA, true);
int fromByteIndex = b.byteLength(encodingA);
int byteLength = c.byteLength(encodingA);
node.execute(sb, concat, fromByteIndex, byteLength);
expected = Arrays.copyOf(arrayA, arrayA.length + byteLength);
System.arraycopy(arrayC, 0, expected, arrayA.length, byteLength);
assertBytesEqual(sb.toStringUncached(), encodingA, expected);
});
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ private static boolean attrsAreCorrect(Object bytes, Encoding encoding, int offs
}
if (bytes instanceof NativePointer) {
((NativePointer) bytes).materializeByteArray(null, offset, length << stride, InlinedConditionProfile.getUncached());
} else {
assert stride == Stride.fromCodeRangeAllowImprecise(codeRange, encoding);
}
long attrs = CalcStringAttributesNodeGen.getUncached().execute(CalcStringAttributesNodeGen.getUncached(), null, bytes, offset, length, stride, encoding, 0, knownCodeRange);
int cpLengthCalc = StringAttributes.getCodePointLength(attrs);
Expand Down Expand Up @@ -5031,7 +5033,8 @@ final TruffleString repeat(AbstractTruffleString a, int n, Encoding expectedEnco
@Cached TStringInternalNodes.GetCodePointLengthNode getCodePointLengthNode,
@Cached TStringInternalNodes.CalcStringAttributesNode calcStringAttributesNode,
@Cached InlinedConditionProfile brokenProfile,
@Cached InlinedBranchProfile outOfMemoryProfile) {
@Cached InlinedBranchProfile outOfMemoryProfile,
@Cached InlinedBranchProfile compactProfile) {
a.checkEncoding(expectedEncoding);
if (n < 0) {
throw InternalErrors.illegalArgument("n must be positive");
Expand All @@ -5046,27 +5049,38 @@ final TruffleString repeat(AbstractTruffleString a, int n, Encoding expectedEnco
int codeRangeA = getPreciseCodeRangeNode.execute(this, a, expectedEncoding);
int codePointLengthA = getCodePointLengthNode.execute(this, a, expectedEncoding);
int byteLengthA = (a.length()) << a.stride();
long byteLength = ((long) byteLengthA) * n;
int stride = Stride.fromCodeRange(codeRangeA, expectedEncoding);
long byteLength = (((long) a.length()) << stride) * n;
if (Long.compareUnsigned(byteLength, TStringConstants.MAX_ARRAY_SIZE) > 0) {
outOfMemoryProfile.enter(this);
throw InternalErrors.outOfMemory();
}
byte[] array = new byte[(int) byteLength];
int offsetB = 0;
for (int i = 0; i < n; i++) {
TStringOps.arraycopyWithStride(this, arrayA, a.offset(), 0, 0, array, offsetB, 0, 0, byteLengthA);
offsetB += byteLengthA;
TStringConstants.truffleSafePointPoll(this, i + 1);
if (stride == a.stride()) {
for (int i = 0; i < n; i++) {
TStringOps.arraycopyWithStride(this, arrayA, a.offset(), 0, 0, array, offsetB, 0, 0, byteLengthA);
offsetB += byteLengthA;
TStringConstants.truffleSafePointPoll(this, i + 1);
}
} else {
compactProfile.enter(this);
int byteLengthCompact = a.length() << stride;
for (int i = 0; i < n; i++) {
TStringOps.arraycopyWithStride(this, arrayA, a.offset(), a.stride(), 0, array, offsetB, stride, 0, a.length());
offsetB += byteLengthCompact;
TStringConstants.truffleSafePointPoll(this, i + 1);
}
}
int length = (int) (byteLength >> a.stride());
int length = (int) (byteLength >> stride);
if (brokenProfile.profile(this, isBroken(codeRangeA))) {
long attrs = calcStringAttributesNode.execute(this, null, array, 0, length, a.stride(), expectedEncoding, 0, codeRangeA);
long attrs = calcStringAttributesNode.execute(this, null, array, 0, length, stride, expectedEncoding, 0, codeRangeA);
codeRangeA = StringAttributes.getCodeRange(attrs);
codePointLengthA = StringAttributes.getCodePointLength(attrs);
} else {
codePointLengthA *= n;
}
return createFromByteArray(array, length, a.stride(), expectedEncoding, codePointLengthA, codeRangeA);
return createFromByteArray(array, length, stride, expectedEncoding, codePointLengthA, codeRangeA);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,7 @@ final void append(TruffleStringBuilderUTF8 sb, AbstractTruffleString a, int from
@Specialization
final void append(TruffleStringBuilderUTF16 sb, AbstractTruffleString a, int fromByteIndex, int byteLength,
@Cached @Shared TruffleString.ToIndexableNode toIndexableNode,
@Cached @Shared TStringInternalNodes.GetPreciseCodeRangeNode getPreciseCodeRangeNode,
@Cached @Shared InlinedBranchProfile slowPathProfile,
@Cached @Shared InlinedBranchProfile inflateProfile,
@Cached @Shared InlinedBranchProfile bufferGrowProfile,
Expand All @@ -1118,30 +1119,32 @@ final void append(TruffleStringBuilderUTF16 sb, AbstractTruffleString a, int fro
sb.codePointLength += length;
} else {
slowPathProfile.enter(this);
final int codeRangeA = a.codeRange();
final int codePointLength;
final int codeRange;
if (a.stride() == 0) {
codeRange = TSCodeRange.markImprecise(codeRangeA);
codeRange = TSCodeRange.markImprecise(a.codeRange());
codePointLength = length;
} else if (fromIndex == 0 && length == a.length()) {
codeRange = codeRangeA;
codePointLength = a.codePointLength();
} else if (TSCodeRange.is16Bit(codeRangeA)) {
assert a.stride() == 1;
codeRange = TStringOps.calcStringAttributesBMP(this, arrayA, a.offset() + fromByteIndex, length);
codePointLength = length;
} else if (TSCodeRange.isValidMultiByte(codeRangeA)) {
long attrs = TStringOps.calcStringAttributesUTF16(this, arrayA, a.offset() + fromByteIndex, length, true);
codeRange = StringAttributes.getCodeRange(attrs);
codePointLength = StringAttributes.getCodePointLength(attrs);
} else {
long attrs = TStringOps.calcStringAttributesUTF16(this, arrayA, a.offset() + fromByteIndex, length, false);
codeRange = StringAttributes.getCodeRange(attrs);
codePointLength = StringAttributes.getCodePointLength(attrs);
final int codeRangeA = getPreciseCodeRangeNode.execute(this, a, Encoding.UTF_16);
if (fromIndex == 0 && length == a.length()) {
codeRange = codeRangeA;
codePointLength = a.codePointLength();
} else if (TSCodeRange.is16Bit(codeRangeA)) {
assert a.stride() == 1;
codeRange = TStringOps.calcStringAttributesBMP(this, arrayA, a.offset() + fromByteIndex, length);
codePointLength = length;
} else if (TSCodeRange.isValidMultiByte(codeRangeA)) {
long attrs = TStringOps.calcStringAttributesUTF16(this, arrayA, a.offset() + fromByteIndex, length, true);
codeRange = StringAttributes.getCodeRange(attrs);
codePointLength = StringAttributes.getCodePointLength(attrs);
} else {
long attrs = TStringOps.calcStringAttributesUTF16(this, arrayA, a.offset() + fromByteIndex, length, false);
codeRange = StringAttributes.getCodeRange(attrs);
codePointLength = StringAttributes.getCodePointLength(attrs);
}
}
sb.updateCodeRange(codeRange);
sb.ensureCapacityAndInflate(this, a.length(), Stride.fromCodeRangeUTF16AllowImprecise(codeRangeA), inflateProfile, bufferGrowProfile, errorProfile);
sb.ensureCapacityAndInflate(this, a.length(), Stride.fromCodeRangeUTF16AllowImprecise(codeRange), inflateProfile, bufferGrowProfile, errorProfile);
TStringOps.arraycopyWithStride(this,
arrayA, a.offset(), a.stride(), fromIndex,
sb.buf, 0, sb.stride, sb.length, length);
Expand All @@ -1153,6 +1156,7 @@ final void append(TruffleStringBuilderUTF16 sb, AbstractTruffleString a, int fro
@Specialization
final void append(TruffleStringBuilderUTF32 sb, AbstractTruffleString a, int fromByteIndex, int byteLength,
@Cached @Shared TruffleString.ToIndexableNode toIndexableNode,
@Cached @Shared TStringInternalNodes.GetPreciseCodeRangeNode getPreciseCodeRangeNode,
@Cached @Shared InlinedBranchProfile slowPathProfile,
@Cached @Shared InlinedBranchProfile inflateProfile,
@Cached @Shared InlinedBranchProfile bufferGrowProfile,
Expand All @@ -1173,18 +1177,17 @@ final void append(TruffleStringBuilderUTF32 sb, AbstractTruffleString a, int fro
sb.buf, 0, 0, sb.length, length);
} else {
slowPathProfile.enter(this);
final int codeRangeA = a.codeRange();
final int codeRange;
if (a.stride() == 0 || fromIndex == 0 && length == a.length() || !TSCodeRange.isMoreGeneralThan(codeRangeA, sb.codeRange)) {
codeRange = TSCodeRange.markImprecise(codeRangeA);
if (a.stride() == 0 || fromIndex == 0 && length == a.length() || !TSCodeRange.isMoreGeneralThan(getPreciseCodeRangeNode.execute(this, a, Encoding.UTF_32), sb.codeRange)) {
codeRange = TSCodeRange.markImprecise(a.codeRange());
} else if (a.stride() == 1) {
codeRange = TStringOps.calcStringAttributesBMP(this, arrayA, a.offset() + (fromIndex << 1), length);
} else {
assert a.stride() == 2;
codeRange = TStringOps.calcStringAttributesUTF32(this, arrayA, a.offset() + fromByteIndex, length);
}
sb.updateCodeRange(codeRange);
sb.ensureCapacityAndInflate(this, a.length(), Stride.fromCodeRangeUTF32AllowImprecise(codeRangeA), inflateProfile, bufferGrowProfile, errorProfile);
sb.ensureCapacityAndInflate(this, a.length(), Stride.fromCodeRangeUTF32AllowImprecise(codeRange), inflateProfile, bufferGrowProfile, errorProfile);
TStringOps.arraycopyWithStride(this,
arrayA, a.offset(), a.stride(), fromIndex,
sb.buf, 0, sb.stride, sb.length, length);
Expand All @@ -1197,7 +1200,7 @@ static void append(TruffleStringBuilderGeneric sb, AbstractTruffleString a, int
@Bind("this") Node node,
@Cached @Exclusive TruffleString.ToIndexableNode toIndexableNode,
@Cached TStringInternalNodes.GetCodePointLengthNode getCodePointLengthNode,
@Cached TStringInternalNodes.GetPreciseCodeRangeNode getPreciseCodeRangeNode,
@Cached @Exclusive TStringInternalNodes.GetPreciseCodeRangeNode getPreciseCodeRangeNode,
@Cached TStringInternalNodes.CalcStringAttributesNode calcAttributesNode,
@Cached InlinedConditionProfile calcAttrsProfile,
@Cached @Exclusive InlinedBranchProfile bufferGrowProfile,
Expand Down

0 comments on commit a29b4da

Please sign in to comment.