Skip to content

Commit 004041b

Browse files
committed
Convert NormalizeIndexNode to DSL inlinable node
1 parent c887f9e commit 004041b

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,19 +443,16 @@ static int checkIndex(Node node, int index, int length,
443443

444444
}
445445

446+
@GenerateInline
447+
@GenerateCached(false)
446448
public abstract static class NormalizeIndexNode extends RubyBaseNode {
447449

448-
@NeverDefault
449-
public static NormalizeIndexNode create() {
450-
return StringHelperNodesFactory.NormalizeIndexNodeGen.create();
451-
}
452-
453-
public abstract int executeNormalize(int index, int length);
450+
public abstract int executeNormalize(Node node, int index, int length);
454451

455452
@Specialization
456-
int normalizeIndex(int index, int length,
453+
static int normalizeIndex(Node node, int index, int length,
457454
@Cached InlinedConditionProfile negativeIndexProfile) {
458-
if (negativeIndexProfile.profile(this, index < 0)) {
455+
if (negativeIndexProfile.profile(node, index < 0)) {
459456
return index + length;
460457
}
461458

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,19 +1406,18 @@ static RubyString forceEncoding(RubyString string, Object newEncoding,
14061406
@CoreMethod(names = "getbyte", required = 1, lowerFixnum = 1)
14071407
public abstract static class StringGetByteNode extends CoreMethodArrayArgumentsNode {
14081408

1409-
@Child private StringHelperNodes.NormalizeIndexNode normalizeIndexNode = StringHelperNodes.NormalizeIndexNode
1410-
.create();
14111409
@Child private TruffleString.ReadByteNode readByteNode = TruffleString.ReadByteNode.create();
14121410

14131411
@Specialization
14141412
Object getByte(Object string, int index,
14151413
@Cached InlinedConditionProfile indexOutOfBoundsProfile,
1416-
@Cached RubyStringLibrary libString) {
1414+
@Cached RubyStringLibrary libString,
1415+
@Cached StringHelperNodes.NormalizeIndexNode normalizeIndexNode) {
14171416
var tstring = libString.getTString(string);
14181417
var encoding = libString.getEncoding(string).tencoding;
14191418
int byteLength = tstring.byteLength(encoding);
14201419

1421-
final int normalizedIndex = normalizeIndexNode.executeNormalize(index, byteLength);
1420+
final int normalizedIndex = normalizeIndexNode.executeNormalize(this, index, byteLength);
14221421

14231422
if (indexOutOfBoundsProfile.profile(this, (normalizedIndex < 0) || (normalizedIndex >= byteLength))) {
14241423
return nil;
@@ -3329,18 +3328,16 @@ private static Object[] addSubstring(Node node, CallBlockNode yieldNode, Object[
33293328
@Primitive(name = "string_byte_substring", lowerFixnum = { 1, 2 })
33303329
public abstract static class StringByteSubstringPrimitiveNode extends PrimitiveArrayArgumentsNode {
33313330

3332-
@Child private StringHelperNodes.NormalizeIndexNode normalizeIndexNode = StringHelperNodes.NormalizeIndexNode
3333-
.create();
3334-
33353331
@Specialization
33363332
Object stringByteSubstring(Object string, int index, NotProvided length,
33373333
@Cached @Exclusive InlinedConditionProfile indexOutOfBoundsProfile,
33383334
@Cached @Shared RubyStringLibrary libString,
3339-
@Cached @Shared TruffleString.SubstringByteIndexNode substringNode) {
3335+
@Cached @Shared TruffleString.SubstringByteIndexNode substringNode,
3336+
@Cached @Shared StringHelperNodes.NormalizeIndexNode normalizeIndexNode) {
33403337
var tString = libString.getTString(string);
33413338
var encoding = libString.getEncoding(string);
33423339
final int stringByteLength = tString.byteLength(encoding.tencoding);
3343-
final int normalizedIndex = normalizeIndexNode.executeNormalize(index, stringByteLength);
3340+
final int normalizedIndex = normalizeIndexNode.executeNormalize(this, index, stringByteLength);
33443341

33453342
if (indexOutOfBoundsProfile.profile(this, normalizedIndex < 0 || normalizedIndex >= stringByteLength)) {
33463343
return nil;
@@ -3355,15 +3352,16 @@ Object stringByteSubstring(Object string, int index, int length,
33553352
@Cached @Exclusive InlinedConditionProfile indexOutOfBoundsProfile,
33563353
@Cached @Exclusive InlinedConditionProfile lengthTooLongProfile,
33573354
@Cached @Shared RubyStringLibrary libString,
3358-
@Cached @Shared TruffleString.SubstringByteIndexNode substringNode) {
3355+
@Cached @Shared TruffleString.SubstringByteIndexNode substringNode,
3356+
@Cached @Shared StringHelperNodes.NormalizeIndexNode normalizeIndexNode) {
33593357
if (negativeLengthProfile.profile(this, length < 0)) {
33603358
return nil;
33613359
}
33623360

33633361
var tString = libString.getTString(string);
33643362
var encoding = libString.getEncoding(string);
33653363
final int stringByteLength = tString.byteLength(encoding.tencoding);
3366-
final int normalizedIndex = normalizeIndexNode.executeNormalize(index, stringByteLength);
3364+
final int normalizedIndex = normalizeIndexNode.executeNormalize(this, index, stringByteLength);
33673365

33683366
if (indexOutOfBoundsProfile.profile(this, normalizedIndex < 0 || normalizedIndex > stringByteLength)) {
33693367
return nil;
@@ -4280,7 +4278,8 @@ Object stringSubstringGeneric(Object string, int codePointOffset, int codePointL
42804278
return nil;
42814279
}
42824280

4283-
int normalizedCodePointOffset = normalizeIndexNode.executeNormalize(codePointOffset, stringCodePointLength);
4281+
int normalizedCodePointOffset = normalizeIndexNode.executeNormalize(this, codePointOffset,
4282+
stringCodePointLength);
42844283
if (negativeIndexProfile.profile(this, normalizedCodePointOffset < 0)) {
42854284
return nil;
42864285
}

0 commit comments

Comments
 (0)