Skip to content

Commit 005ab58

Browse files
committed
RangeNodes#LenOfRange: eliminate boxing, add documentation
1 parent 973fcd5 commit 005ab58

File tree

1 file changed

+16
-14
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/range

1 file changed

+16
-14
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/range/RangeNodes.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747

4848
import com.oracle.graal.python.builtins.objects.ints.PInt;
4949
import com.oracle.graal.python.builtins.objects.range.RangeNodesFactory.LenOfRangeNodeFactory;
50-
import com.oracle.graal.python.builtins.objects.slice.PObjectSlice.SliceObjectInfo;
5150
import com.oracle.graal.python.builtins.objects.slice.PSlice.SliceInfo;
5251
import com.oracle.graal.python.nodes.PNodeWithContext;
5352
import com.oracle.graal.python.nodes.PRaiseNode;
@@ -94,52 +93,55 @@ PBigRange createBigRange(Object start, Object stop, Object step, PythonObjectFac
9493

9594
}
9695

96+
/**
97+
* Attempts to produce result of the same type as the arguments, otherwise throws
98+
* {@link ArithmeticException}. It is responsibility of the caller to widen the arguments' types
99+
* if necessary.
100+
*/
97101
@GenerateNodeFactory
98102
@GenerateUncached
99103
@ImportStatic(SpecialMethodNames.class)
100104
public abstract static class LenOfRangeNode extends Node {
101105
public abstract Object execute(Object start, Object stop, Object step);
102106

103-
public int len(Object start, Object stop, Object step) throws ArithmeticException {
104-
return (int) execute(start, stop, step);
105-
}
107+
public abstract int executeInt(int start, int stop, int step);
106108

107-
public int len(SliceInfo slice) throws ArithmeticException {
108-
return (int) execute(slice.start, slice.stop, slice.step);
109+
public int len(int start, int stop, int step) throws ArithmeticException {
110+
return executeInt(start, stop, step);
109111
}
110112

111-
public Object len(SliceObjectInfo slice) throws ArithmeticException {
112-
return execute(slice.start, slice.stop, slice.step);
113+
public int len(SliceInfo slice) throws ArithmeticException {
114+
return executeInt(slice.start, slice.stop, slice.step);
113115
}
114116

115117
@Specialization(guards = {"step > 0", "lo > 0", "lo < hi"})
116-
Object simple(int lo, int hi, int step) {
118+
int simple(int lo, int hi, int step) {
117119
return 1 + ((hi - 1 - lo) / step);
118120
}
119121

120122
@Specialization(guards = {"step > 0", "lo >= hi"})
121-
Object zero1(@SuppressWarnings("unused") int lo, @SuppressWarnings("unused") int hi, @SuppressWarnings("unused") int step) {
123+
int zero1(@SuppressWarnings("unused") int lo, @SuppressWarnings("unused") int hi, @SuppressWarnings("unused") int step) {
122124
return 0;
123125
}
124126

125127
@Specialization(guards = {"step > 0", "lo < hi"})
126-
Object mightBeBig1(int lo, int hi, int step) throws ArithmeticException {
128+
int mightBeBig1(int lo, int hi, int step) throws ArithmeticException {
127129
long diff = Math.subtractExact(Math.subtractExact(hi, (long) lo), 1);
128130
return Math.toIntExact(Math.addExact(diff / step, 1));
129131
}
130132

131133
@Specialization(guards = {"step < 0", "lo < 0", "lo > hi"})
132-
Object simpleNegative(int lo, int hi, int step) {
134+
int simpleNegative(int lo, int hi, int step) {
133135
return 1 + ((lo - 1 - hi) / -step);
134136
}
135137

136138
@Specialization(guards = {"step < 0", "lo <= hi"})
137-
Object zero2(@SuppressWarnings("unused") int lo, @SuppressWarnings("unused") int hi, @SuppressWarnings("unused") int step) {
139+
int zero2(@SuppressWarnings("unused") int lo, @SuppressWarnings("unused") int hi, @SuppressWarnings("unused") int step) {
138140
return 0;
139141
}
140142

141143
@Specialization(guards = {"step < 0", "lo > hi"})
142-
Object mightBeBig2(int lo, int hi, int step) throws ArithmeticException {
144+
int mightBeBig2(int lo, int hi, int step) throws ArithmeticException {
143145
long diff = Math.subtractExact(Math.subtractExact(lo, (long) hi), 1);
144146
return Math.toIntExact(Math.addExact(diff / -(long) step, 1));
145147
}

0 commit comments

Comments
 (0)