71
71
import static org .truffleruby .core .string .StringSupport .MBCLEN_INVALID_P ;
72
72
import static org .truffleruby .core .string .StringSupport .MBCLEN_NEEDMORE_P ;
73
73
74
+ import com .oracle .truffle .api .TruffleSafepoint ;
74
75
import com .oracle .truffle .api .dsl .Bind ;
75
76
import com .oracle .truffle .api .dsl .Cached .Exclusive ;
76
77
import com .oracle .truffle .api .dsl .Cached .Shared ;
77
78
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
79
+ import com .oracle .truffle .api .profiles .LoopConditionProfile ;
78
80
import com .oracle .truffle .api .strings .AbstractTruffleString ;
79
81
import com .oracle .truffle .api .strings .InternalByteArray ;
80
82
import com .oracle .truffle .api .strings .MutableTruffleString ;
@@ -3149,6 +3151,7 @@ protected Object stringAwkSplitAsciiOnly(Object string, int limit, Object block,
3149
3151
@ Cached TruffleString .MaterializeNode materializeNode ,
3150
3152
@ Cached TruffleString .ReadByteNode readByteNode ,
3151
3153
@ Cached TruffleString .SubstringByteIndexNode substringNode ,
3154
+ @ Cached LoopConditionProfile loopProfile ,
3152
3155
@ Bind ("strings.getTString(string)" ) AbstractTruffleString tstring ,
3153
3156
@ Bind ("strings.getEncoding(string)" ) RubyEncoding encoding ) {
3154
3157
Object [] ret = new Object [10 ];
@@ -3160,32 +3163,40 @@ protected Object stringAwkSplitAsciiOnly(Object string, int limit, Object block,
3160
3163
int substringStart = 0 ;
3161
3164
boolean findingSubstringEnd = false ;
3162
3165
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 ;
3183
3189
3184
- if (storeIndex == limit - 1 ) {
3185
- break ;
3190
+ if (storeIndex == limit - 1 ) {
3191
+ break ;
3192
+ }
3186
3193
}
3187
3194
}
3195
+
3196
+ TruffleSafepoint .poll (this );
3188
3197
}
3198
+ } finally {
3199
+ profileAndReportLoopCount (loopProfile , i );
3189
3200
}
3190
3201
3191
3202
if (trailingSubstringProfile .profile (findingSubstringEnd )) {
@@ -3216,6 +3227,7 @@ protected Object stringAwkSplit(Object string, int limit, Object block,
3216
3227
@ Cached CreateCodePointIteratorNode createCodePointIteratorNode ,
3217
3228
@ Cached TruffleStringIterator .NextNode nextNode ,
3218
3229
@ Cached TruffleString .SubstringByteIndexNode substringNode ,
3230
+ @ Cached LoopConditionProfile loopProfile ,
3219
3231
@ Bind ("strings.getTString(string)" ) AbstractTruffleString tstring ,
3220
3232
@ Bind ("strings.getEncoding(string)" ) RubyEncoding encoding ) {
3221
3233
Object [] ret = new Object [10 ];
@@ -3230,40 +3242,45 @@ protected Object stringAwkSplit(Object string, int limit, Object block,
3230
3242
var iterator = createCodePointIteratorNode .execute (tstring , tencoding , ErrorHandling .RETURN_NEGATIVE );
3231
3243
3232
3244
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
+ }
3262
3261
}
3263
3262
} 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
+ }
3265
3280
}
3266
3281
}
3282
+ } finally {
3283
+ profileAndReportLoopCount (loopProfile , n );
3267
3284
}
3268
3285
3269
3286
if (trailingSubstringProfile .profile (len > 0 && (limitPositive || len > b || limit < 0 ))) {
0 commit comments