Skip to content

Commit 635e686

Browse files
committed
Add loop profiling to the string_awk_split primitive specializations.
1 parent 4a73f05 commit 635e686

File tree

1 file changed

+69
-52
lines changed

1 file changed

+69
-52
lines changed

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

Lines changed: 69 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@
7171
import static org.truffleruby.core.string.StringSupport.MBCLEN_INVALID_P;
7272
import static org.truffleruby.core.string.StringSupport.MBCLEN_NEEDMORE_P;
7373

74+
import com.oracle.truffle.api.TruffleSafepoint;
7475
import com.oracle.truffle.api.dsl.Bind;
7576
import com.oracle.truffle.api.dsl.Cached.Exclusive;
7677
import com.oracle.truffle.api.dsl.Cached.Shared;
7778
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
79+
import com.oracle.truffle.api.profiles.LoopConditionProfile;
7880
import com.oracle.truffle.api.strings.AbstractTruffleString;
7981
import com.oracle.truffle.api.strings.InternalByteArray;
8082
import com.oracle.truffle.api.strings.MutableTruffleString;
@@ -3149,6 +3151,7 @@ protected Object stringAwkSplitAsciiOnly(Object string, int limit, Object block,
31493151
@Cached TruffleString.MaterializeNode materializeNode,
31503152
@Cached TruffleString.ReadByteNode readByteNode,
31513153
@Cached TruffleString.SubstringByteIndexNode substringNode,
3154+
@Cached LoopConditionProfile loopProfile,
31523155
@Bind("strings.getTString(string)") AbstractTruffleString tstring,
31533156
@Bind("strings.getEncoding(string)") RubyEncoding encoding) {
31543157
Object[] ret = new Object[10];
@@ -3160,32 +3163,40 @@ protected Object stringAwkSplitAsciiOnly(Object string, int limit, Object block,
31603163
int substringStart = 0;
31613164
boolean findingSubstringEnd = false;
31623165

3163-
for (int i = 0; i < byteLength; i++) {
3164-
if (StringSupport.isAsciiSpace(readByteNode.execute(tstring, i, encoding.tencoding))) {
3165-
if (findingSubstringEnd) {
3166-
findingSubstringEnd = false;
3167-
3168-
final RubyString substring = createSubString(substringNode, tstring, encoding, substringStart,
3169-
i - substringStart);
3170-
ret = addSubstring(
3171-
ret,
3172-
storeIndex++,
3173-
substring,
3174-
block,
3175-
executeBlockProfile,
3176-
growArrayProfile);
3177-
substringStart = SUBSTRING_CREATED;
3178-
}
3179-
} else {
3180-
if (!findingSubstringEnd) {
3181-
substringStart = i;
3182-
findingSubstringEnd = true;
3166+
int i = 0;
3167+
try {
3168+
for (; loopProfile.inject(i < byteLength); i++) {
3169+
if (StringSupport.isAsciiSpace(readByteNode.execute(tstring, i, encoding.tencoding))) {
3170+
if (findingSubstringEnd) {
3171+
findingSubstringEnd = false;
3172+
3173+
final RubyString substring = createSubString(substringNode, tstring, encoding,
3174+
substringStart,
3175+
i - substringStart);
3176+
ret = addSubstring(
3177+
ret,
3178+
storeIndex++,
3179+
substring,
3180+
block,
3181+
executeBlockProfile,
3182+
growArrayProfile);
3183+
substringStart = SUBSTRING_CREATED;
3184+
}
3185+
} else {
3186+
if (!findingSubstringEnd) {
3187+
substringStart = i;
3188+
findingSubstringEnd = true;
31833189

3184-
if (storeIndex == limit - 1) {
3185-
break;
3190+
if (storeIndex == limit - 1) {
3191+
break;
3192+
}
31863193
}
31873194
}
3195+
3196+
TruffleSafepoint.poll(this);
31883197
}
3198+
} finally {
3199+
profileAndReportLoopCount(loopProfile, i);
31893200
}
31903201

31913202
if (trailingSubstringProfile.profile(findingSubstringEnd)) {
@@ -3216,6 +3227,7 @@ protected Object stringAwkSplit(Object string, int limit, Object block,
32163227
@Cached CreateCodePointIteratorNode createCodePointIteratorNode,
32173228
@Cached TruffleStringIterator.NextNode nextNode,
32183229
@Cached TruffleString.SubstringByteIndexNode substringNode,
3230+
@Cached LoopConditionProfile loopProfile,
32193231
@Bind("strings.getTString(string)") AbstractTruffleString tstring,
32203232
@Bind("strings.getEncoding(string)") RubyEncoding encoding) {
32213233
Object[] ret = new Object[10];
@@ -3230,40 +3242,45 @@ protected Object stringAwkSplit(Object string, int limit, Object block,
32303242
var iterator = createCodePointIteratorNode.execute(tstring, tencoding, ErrorHandling.RETURN_NEGATIVE);
32313243

32323244
boolean skip = true;
3233-
int e = 0, b = 0;
3234-
while (iterator.hasNext()) {
3235-
int c = nextNode.execute(iterator);
3236-
int p = iterator.getByteIndex();
3237-
3238-
if (skip) {
3239-
if (StringSupport.isAsciiSpace(c)) {
3240-
b = p;
3241-
} else {
3242-
e = p;
3243-
skip = false;
3244-
if (limitPositive && limit <= i) {
3245-
break;
3246-
}
3247-
}
3248-
} else {
3249-
if (StringSupport.isAsciiSpace(c)) {
3250-
var substring = createSubString(substringNode, tstring, encoding, b, e - b);
3251-
ret = addSubstring(
3252-
ret,
3253-
storeIndex++,
3254-
substring,
3255-
block,
3256-
executeBlockProfile,
3257-
growArrayProfile);
3258-
skip = true;
3259-
b = p;
3260-
if (limitPositive) {
3261-
i++;
3245+
int e = 0, b = 0, n = 0;
3246+
try {
3247+
while (loopProfile.inject(iterator.hasNext())) {
3248+
int c = nextNode.execute(iterator);
3249+
int p = iterator.getByteIndex();
3250+
n++;
3251+
3252+
if (skip) {
3253+
if (StringSupport.isAsciiSpace(c)) {
3254+
b = p;
3255+
} else {
3256+
e = p;
3257+
skip = false;
3258+
if (limitPositive && limit <= i) {
3259+
break;
3260+
}
32623261
}
32633262
} else {
3264-
e = p;
3263+
if (StringSupport.isAsciiSpace(c)) {
3264+
var substring = createSubString(substringNode, tstring, encoding, b, e - b);
3265+
ret = addSubstring(
3266+
ret,
3267+
storeIndex++,
3268+
substring,
3269+
block,
3270+
executeBlockProfile,
3271+
growArrayProfile);
3272+
skip = true;
3273+
b = p;
3274+
if (limitPositive) {
3275+
i++;
3276+
}
3277+
} else {
3278+
e = p;
3279+
}
32653280
}
32663281
}
3282+
} finally {
3283+
profileAndReportLoopCount(loopProfile, n);
32673284
}
32683285

32693286
if (trailingSubstringProfile.profile(len > 0 && (limitPositive || len > b || limit < 0))) {

0 commit comments

Comments
 (0)