Skip to content

Commit e6f5af3

Browse files
committed
Remove method 'SequenceStorage.index'.
1 parent ff26d3f commit e6f5af3

15 files changed

+213
-179
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java

Lines changed: 168 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@
8181
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.DeleteSliceNodeGen;
8282
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.EnsureCapacityNodeGen;
8383
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.ExtendNodeGen;
84+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.GetElementTypeNodeGen;
8485
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.GetItemNodeGen;
8586
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.GetItemScalarNodeGen;
8687
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.GetItemSliceNodeGen;
88+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.ItemIndexNodeGen;
8789
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.LenNodeGen;
8890
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.ListGeneralizationNodeGen;
8991
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.NoGeneralizationNodeGen;
@@ -104,6 +106,7 @@
104106
import com.oracle.graal.python.builtins.objects.str.PString;
105107
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
106108
import com.oracle.graal.python.nodes.PBaseNode;
109+
import com.oracle.graal.python.nodes.SpecialMethodNames;
107110
import com.oracle.graal.python.nodes.builtins.ListNodes;
108111
import com.oracle.graal.python.nodes.control.GetIteratorNode;
109112
import com.oracle.graal.python.nodes.control.GetNextNode;
@@ -152,6 +155,8 @@ public abstract class SequenceStorageNodes {
152155

153156
abstract static class SequenceStorageBaseNode extends PBaseNode {
154157

158+
@Child private GetElementType getElementTypeNode;
159+
155160
protected static final int DEFAULT_CAPACITY = 8;
156161

157162
protected static final int MAX_SEQUENCE_STORAGES = 12;
@@ -163,7 +168,7 @@ protected static boolean isByteStorage(NativeSequenceStorage store) {
163168
/**
164169
* Tests if {@code left} has the same element type as {@code right}.
165170
*/
166-
protected static boolean compatible(SequenceStorage left, NativeSequenceStorage right) {
171+
protected boolean compatible(SequenceStorage left, NativeSequenceStorage right) {
167172
switch (right.getElementType()) {
168173
case Boolean:
169174
return left instanceof BoolSequenceStorage;
@@ -187,9 +192,9 @@ protected static boolean compatible(SequenceStorage left, NativeSequenceStorage
187192
/**
188193
* Tests if each element of {@code rhs} can be assign to {@code lhs} without casting.
189194
*/
190-
protected static boolean compatibleAssign(SequenceStorage lhs, SequenceStorage rhs) {
191-
ListStorageType rhsType = rhs.getElementType();
192-
switch (lhs.getElementType()) {
195+
protected boolean compatibleAssign(SequenceStorage lhs, SequenceStorage rhs) {
196+
ListStorageType rhsType = getElementType(rhs);
197+
switch (getElementType(lhs)) {
193198
case Boolean:
194199
return rhsType == Boolean || rhsType == Uninitialized;
195200
case Byte:
@@ -218,9 +223,9 @@ protected static boolean compatibleAssign(SequenceStorage lhs, SequenceStorage r
218223
/**
219224
* Tests if elements of {@code rhs} can be assign to {@code lhs} with casting.
220225
*/
221-
protected static boolean compatibleDataType(SequenceStorage lhs, SequenceStorage rhs) {
222-
ListStorageType rhsType = rhs.getElementType();
223-
switch (lhs.getElementType()) {
226+
protected boolean compatibleDataType(SequenceStorage lhs, SequenceStorage rhs) {
227+
ListStorageType rhsType = getElementType(rhs);
228+
switch (getElementType(lhs)) {
224229
case Boolean:
225230
case Byte:
226231
case Int:
@@ -252,44 +257,52 @@ protected boolean isEmpty(SequenceStorage left) {
252257
return left instanceof EmptySequenceStorage || left.length() == 0;
253258
}
254259

255-
protected static boolean isBoolean(SequenceStorage s) {
256-
return s.getElementType() == ListStorageType.Boolean;
260+
private ListStorageType getElementType(SequenceStorage s) {
261+
if (getElementTypeNode == null) {
262+
CompilerDirectives.transferToInterpreterAndInvalidate();
263+
getElementTypeNode = insert(GetElementTypeNodeGen.create());
264+
}
265+
return getElementTypeNode.execute(s);
266+
}
267+
268+
protected boolean isBoolean(SequenceStorage s) {
269+
return getElementType(s) == ListStorageType.Boolean;
257270
}
258271

259-
protected static boolean isByte(SequenceStorage s) {
260-
return s.getElementType() == ListStorageType.Byte;
272+
protected boolean isByte(SequenceStorage s) {
273+
return getElementType(s) == ListStorageType.Byte;
261274
}
262275

263-
protected static boolean isByteLike(SequenceStorage s) {
276+
protected boolean isByteLike(SequenceStorage s) {
264277
return isByte(s) || isInt(s) || isLong(s);
265278
}
266279

267-
protected static boolean isChar(SequenceStorage s) {
268-
return s.getElementType() == ListStorageType.Char;
280+
protected boolean isChar(SequenceStorage s) {
281+
return getElementType(s) == ListStorageType.Char;
269282
}
270283

271-
protected static boolean isInt(SequenceStorage s) {
272-
return s.getElementType() == ListStorageType.Int;
284+
protected boolean isInt(SequenceStorage s) {
285+
return getElementType(s) == ListStorageType.Int;
273286
}
274287

275-
protected static boolean isLong(SequenceStorage s) {
276-
return s.getElementType() == ListStorageType.Long;
288+
protected boolean isLong(SequenceStorage s) {
289+
return getElementType(s) == ListStorageType.Long;
277290
}
278291

279-
protected static boolean isDouble(SequenceStorage s) {
280-
return s.getElementType() == ListStorageType.Double;
292+
protected boolean isDouble(SequenceStorage s) {
293+
return getElementType(s) == ListStorageType.Double;
281294
}
282295

283-
protected static boolean isObject(SequenceStorage s) {
284-
return s.getElementType() == ListStorageType.Generic;
296+
protected boolean isObject(SequenceStorage s) {
297+
return getElementType(s) == ListStorageType.Generic;
285298
}
286299

287-
protected static boolean isTuple(SequenceStorage s) {
288-
return s.getElementType() == ListStorageType.Tuple;
300+
protected boolean isTuple(SequenceStorage s) {
301+
return getElementType(s) == ListStorageType.Tuple;
289302
}
290303

291-
protected static boolean isList(SequenceStorage s) {
292-
return s.getElementType() == ListStorageType.List;
304+
protected boolean isList(SequenceStorage s) {
305+
return getElementType(s) == ListStorageType.List;
293306
}
294307

295308
protected static boolean hasStorage(Object source) {
@@ -478,6 +491,8 @@ public static GetItemScalarNode create() {
478491

479492
public abstract byte executeByte(SequenceStorage s, int idx);
480493

494+
public abstract char executeChar(SequenceStorage s, int idx);
495+
481496
public abstract int executeInt(SequenceStorage s, int idx);
482497

483498
public abstract long executeLong(SequenceStorage s, int idx);
@@ -2362,10 +2377,6 @@ SequenceStorage doGeneric(@SuppressWarnings("unused") SequenceStorage s, @Suppre
23622377
throw raise(TypeError, errorMessage);
23632378
}
23642379

2365-
protected static boolean compatibleAssign(SequenceStorage lhs, SequenceStorage rhs) {
2366-
return SequenceStorageBaseNode.compatibleAssign(lhs, rhs);
2367-
}
2368-
23692380
public static NoGeneralizationNode create(String invalidItemErrorMessage) {
23702381
return NoGeneralizationNodeGen.create(invalidItemErrorMessage);
23712382
}
@@ -2503,10 +2514,6 @@ private ValueProfile getSelfProfile() {
25032514
return selfProfile;
25042515
}
25052516

2506-
protected static boolean compatibleAssign(SequenceStorage lhs, SequenceStorage rhs) {
2507-
return SequenceStorageBaseNode.compatibleAssign(lhs, rhs);
2508-
}
2509-
25102517
public static ListGeneralizationNode create() {
25112518
return ListGeneralizationNodeGen.create();
25122519
}
@@ -2885,4 +2892,131 @@ protected static DeleteSliceNode create() {
28852892
return DeleteSliceNodeGen.create();
28862893
}
28872894
}
2895+
2896+
abstract static class GetElementType extends PBaseNode {
2897+
2898+
public abstract ListStorageType execute(SequenceStorage s);
2899+
2900+
@Specialization(limit = "cacheLimit()", guards = {"s.getClass() == cachedClass"})
2901+
ListStorageType doCached(SequenceStorage s,
2902+
@Cached("s.getClass()") Class<? extends SequenceStorage> cachedClass) {
2903+
return cachedClass.cast(s).getElementType();
2904+
}
2905+
2906+
protected static int cacheLimit() {
2907+
return SequenceStorageBaseNode.MAX_SEQUENCE_STORAGES;
2908+
}
2909+
}
2910+
2911+
@ImportStatic(SpecialMethodNames.class)
2912+
public abstract static class ItemIndexNode extends SequenceStorageBaseNode {
2913+
2914+
@Child private GetItemScalarNode getItemNode;
2915+
@Child private LenNode lenNode;
2916+
2917+
public abstract int execute(SequenceStorage s, Object item, int start, int end);
2918+
2919+
public abstract int execute(SequenceStorage s, boolean item, int start, int end);
2920+
2921+
public abstract int execute(SequenceStorage s, char item, int start, int end);
2922+
2923+
public abstract int execute(SequenceStorage s, int item, int start, int end);
2924+
2925+
public abstract int execute(SequenceStorage s, long item, int start, int end);
2926+
2927+
public abstract int execute(SequenceStorage s, double item, int start, int end);
2928+
2929+
@Specialization(guards = "isBoolean(s)")
2930+
int doBoolean(SequenceStorage s, boolean item, int start, int end) {
2931+
for (int i = start; i < getLength(s, end); i++) {
2932+
if (getItemNode().executeBoolean(s, i) == item) {
2933+
return i;
2934+
}
2935+
}
2936+
return -1;
2937+
}
2938+
2939+
@Specialization(guards = "isByte(s)")
2940+
int doByte(SequenceStorage s, byte item, int start, int end) {
2941+
for (int i = start; i < getLength(s, end); i++) {
2942+
if (getItemNode().executeByte(s, i) == item) {
2943+
return i;
2944+
}
2945+
}
2946+
return -1;
2947+
}
2948+
2949+
@Specialization(guards = "isChar(s)")
2950+
int doChar(SequenceStorage s, char item, int start, int end) {
2951+
for (int i = start; i < getLength(s, end); i++) {
2952+
if (getItemNode().executeChar(s, i) == item) {
2953+
return i;
2954+
}
2955+
}
2956+
return -1;
2957+
}
2958+
2959+
@Specialization(guards = "isInt(s)")
2960+
int doInt(SequenceStorage s, int item, int start, int end) {
2961+
for (int i = start; i < getLength(s, end); i++) {
2962+
if (getItemNode().executeInt(s, i) == item) {
2963+
return i;
2964+
}
2965+
}
2966+
return -1;
2967+
}
2968+
2969+
@Specialization(guards = "isLong(s)")
2970+
int doLong(SequenceStorage s, long item, int start, int end) {
2971+
for (int i = start; i < getLength(s, end); i++) {
2972+
if (getItemNode().executeLong(s, i) == item) {
2973+
return i;
2974+
}
2975+
}
2976+
return -1;
2977+
}
2978+
2979+
@Specialization(guards = "isDouble(s)")
2980+
int doDouble(SequenceStorage s, double item, int start, int end) {
2981+
for (int i = start; i < getLength(s, end); i++) {
2982+
if (getItemNode().executeDouble(s, i) == item) {
2983+
return i;
2984+
}
2985+
}
2986+
return -1;
2987+
}
2988+
2989+
@Specialization
2990+
int doGeneric(SequenceStorage s, Object item, int start, int end,
2991+
@Cached("createIfTrueNode()") CastToBooleanNode castToBooleanNode,
2992+
@Cached("create(__EQ__, __EQ__, __EQ__)") BinaryComparisonNode eqNode) {
2993+
for (int i = start; i < getLength(s, end); i++) {
2994+
Object object = getItemNode().execute(s, i);
2995+
if (castToBooleanNode.executeWith(eqNode.executeWith(object, item))) {
2996+
return i;
2997+
}
2998+
}
2999+
return -1;
3000+
}
3001+
3002+
private GetItemScalarNode getItemNode() {
3003+
if (getItemNode == null) {
3004+
CompilerDirectives.transferToInterpreterAndInvalidate();
3005+
getItemNode = insert(GetItemScalarNode.create());
3006+
}
3007+
return getItemNode;
3008+
}
3009+
3010+
private int getLength(SequenceStorage s, int end) {
3011+
if (lenNode == null) {
3012+
CompilerDirectives.transferToInterpreterAndInvalidate();
3013+
lenNode = insert(LenNode.create());
3014+
}
3015+
return Math.min(lenNode.execute(s), end);
3016+
}
3017+
3018+
public static ItemIndexNode create() {
3019+
return ItemIndexNodeGen.create();
3020+
}
3021+
}
28883022
}

0 commit comments

Comments
 (0)