Skip to content

Commit fbc32b6

Browse files
committed
Inline ArrayOperations.normalizeIndex()
* The logic is simpler in the caller where variables can be re-assigned.
1 parent 21b95e6 commit fbc32b6

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

src/main/java/org/truffleruby/core/array/ArrayNodes.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,10 @@ public abstract static class IndexNode extends ArrayCoreMethodNode {
264264
protected Object index(RubyArray array, int index, NotProvided length,
265265
@Cached ConditionProfile negativeIndexProfile,
266266
@Cached ReadNormalizedNode readNode) {
267-
final int normalizedIndex = ArrayOperations
268-
.normalizeIndex(array.size, index, negativeIndexProfile);
269-
return readNode.executeRead(array, normalizedIndex);
267+
if (negativeIndexProfile.profile(index < 0)) {
268+
index += array.size;
269+
}
270+
return readNode.executeRead(array, index);
270271
}
271272

272273
@Specialization
@@ -291,9 +292,10 @@ protected Object slice(RubyArray array, int start, int length,
291292
if (length < 0) {
292293
return nil;
293294
}
294-
final int normalizedStart = ArrayOperations
295-
.normalizeIndex(array.size, start, negativeIndexProfile);
296-
return readSliceNode.executeReadSlice(array, normalizedStart, length);
295+
if (negativeIndexProfile.profile(start < 0)) {
296+
start += array.size;
297+
}
298+
return readSliceNode.executeReadSlice(array, start, length);
297299
}
298300

299301
@Specialization(guards = { "wasProvided(length)", "!isInteger(start) || !isInteger(length)" })
@@ -728,7 +730,10 @@ protected Object deleteAt(RubyArray array, int index,
728730
@Cached ConditionProfile negativeIndexProfile,
729731
@Cached ConditionProfile notInBoundsProfile) {
730732
final int size = array.size;
731-
final int i = ArrayOperations.normalizeIndex(size, index, negativeIndexProfile);
733+
int i = index;
734+
if (negativeIndexProfile.profile(index < 0)) {
735+
i += size;
736+
}
732737

733738
if (notInBoundsProfile.profile(i < 0 || i >= size)) {
734739
return nil;
@@ -751,7 +756,10 @@ protected Object deleteAtCopying(RubyArray array, int index,
751756
@Cached ConditionProfile negativeIndexProfile,
752757
@Cached ConditionProfile notInBoundsProfile) {
753758
final int size = array.size;
754-
final int i = ArrayOperations.normalizeIndex(size, index, negativeIndexProfile);
759+
int i = index;
760+
if (negativeIndexProfile.profile(index < 0)) {
761+
i += size;
762+
}
755763

756764
if (notInBoundsProfile.profile(i < 0 || i >= size)) {
757765
return nil;

src/main/java/org/truffleruby/core/array/ArrayOperations.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import com.oracle.truffle.api.CompilerDirectives;
2121
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
22-
import com.oracle.truffle.api.profiles.ConditionProfile;
2322

2423
public abstract class ArrayOperations {
2524

@@ -69,22 +68,6 @@ public static boolean verifyStore(RubyArray array) {
6968
return true;
7069
}
7170

72-
public static int normalizeIndex(int length, int index, ConditionProfile negativeIndexProfile) {
73-
if (negativeIndexProfile.profile(index < 0)) {
74-
return length + index;
75-
} else {
76-
return index;
77-
}
78-
}
79-
80-
public static int normalizeIndex(int length, int index) {
81-
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.SLOWPATH_PROBABILITY, index < 0)) {
82-
return length + index;
83-
} else {
84-
return index;
85-
}
86-
}
87-
8871
public static int clampExclusiveIndex(int length, int index) {
8972
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.UNLIKELY_PROBABILITY, index < 0)) {
9073
return 0;

src/main/java/org/truffleruby/core/regexp/MatchDataNodes.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,15 @@ protected Object getIndex(RubyMatchData matchData, int index, NotProvided length
241241
final RubyString source = matchData.source;
242242
final Rope sourceRope = source.rope;
243243
final Region region = matchData.region;
244-
final int normalizedIndex = ArrayOperations
245-
.normalizeIndex(region.beg.length, index, normalizedIndexProfile);
244+
if (normalizedIndexProfile.profile(index < 0)) {
245+
index += region.beg.length;
246+
}
246247

247-
if (indexOutOfBoundsProfile.profile((normalizedIndex < 0) || (normalizedIndex >= region.beg.length))) {
248+
if (indexOutOfBoundsProfile.profile((index < 0) || (index >= region.beg.length))) {
248249
return nil;
249250
} else {
250-
final int start = region.beg[normalizedIndex];
251-
final int end = region.end[normalizedIndex];
251+
final int start = region.beg[index];
252+
final int end = region.end[index];
252253
if (hasValueProfile.profile(start > -1 && end > -1)) {
253254
Rope rope = substringNode.executeSubstring(sourceRope, start, end - start);
254255
final Shape shape = allocateHelperNode.getCachedShape(source.getLogicalClass());
@@ -262,11 +263,14 @@ protected Object getIndex(RubyMatchData matchData, int index, NotProvided length
262263
}
263264

264265
@Specialization
265-
protected RubyArray getIndex(RubyMatchData matchData, int index, int length) {
266+
protected RubyArray getIndex(RubyMatchData matchData, int index, int length,
267+
@Cached ConditionProfile normalizedIndexProfile) {
266268
// TODO BJF 15-May-2015 Need to handle negative indexes and lengths and out of bounds
267269
final Object[] values = getValuesNode.execute(matchData);
268-
final int normalizedIndex = ArrayOperations.normalizeIndex(values.length, index);
269-
final Object[] store = Arrays.copyOfRange(values, normalizedIndex, normalizedIndex + length);
270+
if (normalizedIndexProfile.profile(index < 0)) {
271+
index += values.length;
272+
}
273+
final Object[] store = Arrays.copyOfRange(values, index, index + length);
270274
return createArray(store);
271275
}
272276

@@ -310,13 +314,19 @@ protected Object getIndex(RubyMatchData matchData, Object index, NotProvided len
310314
@Specialization
311315
protected RubyArray getIndex(RubyMatchData matchData, RubyIntRange range, NotProvided len) {
312316
final Object[] values = getValuesNode.execute(matchData);
313-
final int normalizedIndex = ArrayOperations.normalizeIndex(values.length, range.begin);
314-
final int end = ArrayOperations.normalizeIndex(values.length, range.end);
317+
int index = range.begin;
318+
if (range.begin < 0) {
319+
index += values.length;
320+
}
321+
int end = range.end;
322+
if (end < 0) {
323+
end += values.length;
324+
}
315325
final int exclusiveEnd = ArrayOperations
316326
.clampExclusiveIndex(values.length, range.excludedEnd ? end : end + 1);
317-
final int length = exclusiveEnd - normalizedIndex;
327+
final int length = exclusiveEnd - index;
318328

319-
return createArray(Arrays.copyOfRange(values, normalizedIndex, normalizedIndex + length));
329+
return createArray(Arrays.copyOfRange(values, index, index + length));
320330
}
321331

322332
@TruffleBoundary

0 commit comments

Comments
 (0)