Skip to content

Commit 8442c6f

Browse files
committed
Push SequenceStorage.getItemNormalized down
1 parent ee8400a commit 8442c6f

File tree

15 files changed

+52
-51
lines changed

15 files changed

+52
-51
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/builtin/modules/PathConversionNodeTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.oracle.graal.python.builtins.modules.PosixModuleBuiltins.PosixPath;
6363
import com.oracle.graal.python.builtins.modules.PosixModuleBuiltinsFactory;
6464
import com.oracle.graal.python.builtins.objects.PNone;
65+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
6566
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
6667
import com.oracle.graal.python.runtime.PosixSupportLibrary.Buffer;
6768
import com.oracle.graal.python.runtime.PythonContext;
@@ -341,8 +342,8 @@ private String callAndExpectPath(boolean nullable, boolean allowFd, Object arg,
341342

342343
private String callAndExpectPathEx(String script, boolean wasBufferLike) {
343344
PTuple o = (PTuple) evalValue(script);
344-
Object arg = o.getSequenceStorage().getItemNormalized(0);
345-
Object orig = o.getSequenceStorage().getItemNormalized(1);
345+
Object arg = SequenceStorageNodes.GetItemScalarNode.executeUncached(o.getSequenceStorage(), 0);
346+
Object orig = SequenceStorageNodes.GetItemScalarNode.executeUncached(o.getSequenceStorage(), 1);
346347
return callAndExpectPath(true, true, arg, orig, wasBufferLike);
347348
}
348349

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,7 +2426,7 @@ static PTuple update(PTuple bases, Object[] arguments, int nargs,
24262426
}
24272427
SequenceStorage storage = newBaseTuple.getSequenceStorage();
24282428
for (int j = 0; j < storage.length(); j++) {
2429-
newBases.add(storage.getItemNormalized(j));
2429+
newBases.add(SequenceStorageNodes.GetItemScalarNode.executeUncached(storage, j));
24302430
}
24312431
}
24322432
if (newBases == null) {
@@ -2460,7 +2460,7 @@ static Object calculate(Object metatype, PTuple bases,
24602460
int nbases = storage.length();
24612461
Object winner = metatype;
24622462
for (int i = 0; i < nbases; i++) {
2463-
Object tmp = storage.getItemNormalized(i);
2463+
Object tmp = SequenceStorageNodes.GetItemScalarNode.executeUncached(storage, i);
24642464
Object tmpType = getClass.execute(inliningTarget, tmp);
24652465
if (isSubType.execute(winner, tmpType)) {
24662466
// nothing to do
@@ -2564,7 +2564,7 @@ class InitializeBuildClass {
25642564
meta = PythonContext.get(update).lookupType(PythonBuiltinClassType.PythonClass);
25652565
} else {
25662566
// else get the type of the first base
2567-
meta = getClass.execute(inliningTarget, bases.getSequenceStorage().getItemNormalized(0));
2567+
meta = getClass.execute(inliningTarget, SequenceStorageNodes.GetItemScalarNode.executeUncached(bases.getSequenceStorage(), 0));
25682568
}
25692569
isClass = true; // meta is really a class
25702570
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ast/Obj2SstBase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAccessLibrary;
6161
import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAcquireLibrary;
6262
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
63+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
6364
import com.oracle.graal.python.builtins.objects.complex.PComplex;
6465
import com.oracle.graal.python.builtins.objects.ellipsis.PEllipsis;
6566
import com.oracle.graal.python.builtins.objects.floats.PFloat;
@@ -168,7 +169,7 @@ <T> T[] lookupAndConvertSequence(Object obj, TruffleString attrName, TruffleStri
168169
SequenceStorage seq = ((PList) tmp).getSequenceStorage();
169170
T[] result = arrayFactory.apply(seq.length());
170171
for (int i = 0; i < result.length; ++i) {
171-
tmp = seq.getItemNormalized(i);
172+
tmp = SequenceStorageNodes.GetItemScalarNode.executeUncached(seq, i);
172173
// Py_EnterRecursiveCall(" while traversing '%s' node")
173174
result[i] = conversion.convert(tmp);
174175
if (result.length != seq.length()) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/json/JSONEncoderBuiltins.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.HashingStorageIteratorNext;
3535
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.HashingStorageIteratorValue;
3636
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.HashingStorageLen;
37+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
3738
import com.oracle.graal.python.builtins.objects.dict.PDict;
3839
import com.oracle.graal.python.builtins.objects.floats.FloatBuiltins;
3940
import com.oracle.graal.python.builtins.objects.floats.PFloat;
@@ -273,12 +274,12 @@ private void appendDictSlowPath(PJSONEncoder encoder, TruffleStringBuilderUTF32
273274
e.expectStopIteration(null, IsBuiltinObjectProfile.getUncached());
274275
break;
275276
}
276-
if (!(item instanceof PTuple) || ((PTuple) item).getSequenceStorage().length() != 2) {
277+
if (!(item instanceof PTuple itemTuple) || itemTuple.getSequenceStorage().length() != 2) {
277278
throw PRaiseNode.raiseUncached(this, ValueError, ErrorMessages.ITEMS_MUST_RETURN_2_TUPLES);
278279
}
279-
SequenceStorage sequenceStorage = ((PTuple) item).getSequenceStorage();
280-
Object key = sequenceStorage.getItemNormalized(0);
281-
Object value = sequenceStorage.getItemNormalized(1);
280+
SequenceStorage sequenceStorage = itemTuple.getSequenceStorage();
281+
Object key = SequenceStorageNodes.GetItemScalarNode.executeUncached(sequenceStorage, 0);
282+
Object value = SequenceStorageNodes.GetItemScalarNode.executeUncached(sequenceStorage, 1);
282283
first = appendDictEntry(encoder, builder, first, key, value);
283284
}
284285
}
@@ -319,7 +320,7 @@ private void appendList(PJSONEncoder encoder, TruffleStringBuilderUTF32 builder,
319320
if (i > 0) {
320321
builder.appendStringUncached(encoder.itemSeparator);
321322
}
322-
appendListObj(encoder, builder, storage.getItemNormalized(i));
323+
appendListObj(encoder, builder, SequenceStorageNodes.GetItemScalarNode.executeUncached(storage, i));
323324
}
324325
} else {
325326
appendListSlowPath(encoder, builder, list);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/PBytes.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ public PBytes(Object cls, Shape instanceShape, SequenceStorage store) {
5555
super(cls, instanceShape, store);
5656
}
5757

58-
public Object getItemNormalized(int index) {
59-
return store.getItemNormalized(index);
60-
}
61-
6258
@Override
6359
public String toString() {
6460
// TODO(fa) really required ?

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/BaseExceptionGroupBuiltins.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.oracle.graal.python.builtins.PythonBuiltins;
6363
import com.oracle.graal.python.builtins.objects.PNone;
6464
import com.oracle.graal.python.builtins.objects.common.EconomicMapStorage;
65+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
6566
import com.oracle.graal.python.builtins.objects.dict.PDict;
6667
import com.oracle.graal.python.builtins.objects.function.PFunction;
6768
import com.oracle.graal.python.builtins.objects.list.PList;
@@ -229,7 +230,7 @@ private static MatcherType getMatcherType(Node inliningTarget, Object value) {
229230
if (value instanceof PTuple tuple && PyTupleCheckExactNode.executeUncached(tuple)) {
230231
SequenceStorage storage = tuple.getSequenceStorage();
231232
for (int i = 0; i < storage.length(); i++) {
232-
Object elem = storage.getItemNormalized(i);
233+
Object elem = SequenceStorageNodes.GetItemScalarNode.executeUncached(storage, i);
233234
if (!isExceptionTypeUncached(elem)) {
234235
throw PRaiseNode.raiseUncached(inliningTarget, TypeError, ErrorMessages.EXPECTED_A_FUNCTION_EXCEPTION_TYPE_OR_TUPLE_OF_EXCEPTION_TYPES);
235236
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/PBaseException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public String getFormattedMessage() {
251251
return typeName;
252252
} else if (args.getSequenceStorage().length() == 1) {
253253
SequenceStorage store = args.getSequenceStorage();
254-
Object item = store instanceof BasicSequenceStorage ? store.getItemNormalized(0) : "<unknown>";
254+
Object item = store instanceof BasicSequenceStorage basicStorage ? basicStorage.getItemNormalized(0) : "<unknown>";
255255
return typeName + ": " + item.toString();
256256
} else {
257257
return typeName + ": " + args.toString();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/posix/DirEntryBuiltins.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import com.oracle.graal.python.builtins.modules.PosixModuleBuiltins;
6666
import com.oracle.graal.python.builtins.modules.PosixModuleBuiltins.PosixFd;
6767
import com.oracle.graal.python.builtins.modules.PosixModuleBuiltins.PosixPath;
68+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
6869
import com.oracle.graal.python.builtins.objects.exception.OSErrorEnum;
6970
import com.oracle.graal.python.builtins.objects.str.StringUtils.SimpleTruffleStringFormatNode;
7071
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
@@ -324,25 +325,29 @@ protected TestModeNode(long expectedMode, int expectedDirEntryType) {
324325
abstract boolean execute(VirtualFrame frame, PDirEntry self, boolean followSymlinks);
325326

326327
@Specialization(guards = "followSymlinks")
327-
boolean testModeUsingStat(VirtualFrame frame, PDirEntry self, boolean followSymlinks) {
328+
boolean testModeUsingStat(VirtualFrame frame, PDirEntry self, boolean followSymlinks,
329+
@Bind("this") Node inliningTarget,
330+
@Shared @Cached SequenceStorageNodes.GetItemScalarNode getItemScalarNode) {
328331
PTuple statResult = getStatHelperNode().execute(frame, self, followSymlinks, true);
329332
if (statResult == null) {
330333
// file not found
331334
return false;
332335
}
333336
// TODO constants for stat_result indices
334-
long mode = (long) statResult.getSequenceStorage().getItemNormalized(0) & S_IFMT.value;
337+
long mode = (long) getItemScalarNode.execute(inliningTarget, statResult.getSequenceStorage(), 0) & S_IFMT.value;
335338
return mode == expectedMode;
336339
}
337340

338341
@Specialization(guards = "!followSymlinks")
339342
boolean useTypeIfKnown(VirtualFrame frame, PDirEntry self, @SuppressWarnings("unused") boolean followSymlinks,
343+
@Bind("this") Node inliningTarget,
344+
@Shared @Cached SequenceStorageNodes.GetItemScalarNode getItemScalarNode,
340345
@CachedLibrary(limit = "1") PosixSupportLibrary posixLib) {
341346
int entryType = posixLib.dirEntryGetType(PosixSupport.get(this), self.dirEntryData);
342347
if (entryType != DT_UNKNOWN.value) {
343348
return entryType == expectedDirEntryType;
344349
}
345-
return testModeUsingStat(frame, self, false);
350+
return testModeUsingStat(frame, self, false, inliningTarget, getItemScalarNode);
346351
}
347352

348353
private StatHelperNode getStatHelperNode() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/types/GenericAliasBuiltins.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import com.oracle.graal.python.builtins.PythonBuiltins;
8686
import com.oracle.graal.python.builtins.objects.PNone;
8787
import com.oracle.graal.python.builtins.objects.PNotImplemented;
88+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
8889
import com.oracle.graal.python.builtins.objects.ellipsis.PEllipsis;
8990
import com.oracle.graal.python.builtins.objects.function.PKeyword;
9091
import com.oracle.graal.python.builtins.objects.list.PList;
@@ -225,7 +226,7 @@ static Object repr(PGenericAlias self) {
225226
if (i > 0) {
226227
sb.appendStringUncached(SEPARATOR);
227228
}
228-
reprItem(sb, argsStorage.getItemNormalized(i));
229+
reprItem(sb, SequenceStorageNodes.GetItemScalarNode.executeUncached(argsStorage, i));
229230
}
230231
if (argsStorage.length() == 0) {
231232
// for something like tuple[()] we should print a "()"

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/types/GenericTypeNodes.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5656
import com.oracle.graal.python.builtins.objects.PNone;
5757
import com.oracle.graal.python.builtins.objects.PNotImplemented;
58+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
5859
import com.oracle.graal.python.builtins.objects.ellipsis.PEllipsis;
5960
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
6061
import com.oracle.graal.python.builtins.objects.type.SpecialMethodSlot;
@@ -101,6 +102,11 @@ public abstract class GenericTypeNodes {
101102
public static final String J___TYPING_PREPARE_SUBST__ = "__typing_prepare_subst__";
102103
public static final TruffleString T___TYPING_PREPARE_SUBST__ = tsLiteral(J___TYPING_PREPARE_SUBST__);
103104

105+
@TruffleBoundary
106+
private static Object getItemUncached(SequenceStorage storage, int i) {
107+
return SequenceStorageNodes.GetItemScalarNode.executeUncached(storage, i);
108+
}
109+
104110
static void reprItem(TruffleStringBuilder sb, Object obj) {
105111
PyObjectLookupAttr lookup = PyObjectLookupAttr.getUncached();
106112
PyObjectStrAsTruffleStringNode str = PyObjectStrAsTruffleStringNode.getUncached();
@@ -137,11 +143,11 @@ static void reprItem(TruffleStringBuilder sb, Object obj) {
137143
@TruffleBoundary
138144
static Object[] makeParameters(PTuple args) {
139145
PyObjectLookupAttr lookup = PyObjectLookupAttr.getUncached();
140-
SequenceStorage storage = args.getSequenceStorage();
141-
int nargs = storage.length();
146+
SequenceStorage argsStorage = args.getSequenceStorage();
147+
int nargs = argsStorage.length();
142148
List<Object> parameters = new ArrayList<>(nargs);
143149
for (int iarg = 0; iarg < nargs; iarg++) {
144-
Object t = storage.getItemNormalized(iarg);
150+
Object t = getItemUncached(argsStorage, iarg);
145151
// We don't want __parameters__ descriptor of a bare Python class
146152
if (TypeNodes.IsTypeNode.executeUncached(t)) {
147153
continue;
@@ -152,10 +158,10 @@ static Object[] makeParameters(PTuple args) {
152158
listAdd(parameters, t);
153159
} else {
154160
Object subparams = lookup.execute(null, null, t, T___PARAMETERS__);
155-
if (subparams instanceof PTuple) {
156-
SequenceStorage storage2 = ((PTuple) subparams).getSequenceStorage();
157-
for (int j = 0; j < storage2.length(); j++) {
158-
listAdd(parameters, storage2.getItemNormalized(j));
161+
if (subparams instanceof PTuple subparamsTuple) {
162+
SequenceStorage subparamsStorage = subparamsTuple.getSequenceStorage();
163+
for (int j = 0; j < subparamsStorage.length(); j++) {
164+
listAdd(parameters, getItemUncached(subparamsStorage, j));
159165
}
160166
}
161167
}
@@ -186,7 +192,7 @@ private static int listIndex(List<Object> list, Object obj) {
186192
private static int tupleIndex(PTuple tuple, Object obj) {
187193
SequenceStorage storage = tuple.getSequenceStorage();
188194
for (int i = 0; i < storage.length(); i++) {
189-
if (storage.getItemNormalized(i) == obj) {
195+
if (getItemUncached(storage, i) == obj) {
190196
return i;
191197
}
192198
}
@@ -198,7 +204,7 @@ private static int tupleIndex(PTuple tuple, Object obj) {
198204
private static void listExtend(List<Object> list, PTuple tuple) {
199205
SequenceStorage storage = tuple.getSequenceStorage();
200206
for (int i = 0; i < storage.length(); i++) {
201-
list.add(storage.getItemNormalized(i));
207+
list.add(SequenceStorageNodes.GetItemScalarNode.executeUncached(storage, i));
202208
}
203209
}
204210

@@ -231,7 +237,7 @@ private static Object[] unpackArgs(Object item) {
231237
if (item instanceof PTuple tuple) {
232238
SequenceStorage storage = tuple.getSequenceStorage();
233239
for (int i = 0; i < storage.length(); i++) {
234-
unpackArgsInner(newargs, storage.getItemNormalized(i));
240+
unpackArgsInner(newargs, getItemUncached(storage, i));
235241
}
236242
} else {
237243
unpackArgsInner(newargs, item);
@@ -244,9 +250,9 @@ private static void unpackArgsInner(List<Object> newargs, Object item) {
244250
Object subargs = unpackedTupleArgs(item);
245251
if (subargs instanceof PTuple tuple) {
246252
SequenceStorage storage = tuple.getSequenceStorage();
247-
if (!(storage.length() > 0 && storage.getItemNormalized(storage.length() - 1) == PEllipsis.INSTANCE)) {
253+
if (!(storage.length() > 0 && getItemUncached(storage, storage.length() - 1) == PEllipsis.INSTANCE)) {
248254
for (int i = 0; i < storage.length(); i++) {
249-
newargs.add(storage.getItemNormalized(i));
255+
newargs.add(getItemUncached(storage, i));
250256
}
251257
return;
252258
}
@@ -265,7 +271,7 @@ static Object[] subsParameters(Node node, Object self, PTuple args, PTuple param
265271
}
266272
Object[] argitems = unpackArgs(item);
267273
for (int i = 0; i < nparams; i++) {
268-
Object param = paramsStorage.getItemNormalized(i);
274+
Object param = getItemUncached(paramsStorage, i);
269275
Object prepare = PyObjectLookupAttr.executeUncached(param, T___TYPING_PREPARE_SUBST__);
270276
if (!(prepare instanceof PNone)) {
271277
Object itemarg = item instanceof PTuple ? item : PythonContext.get(node).factory().createTuple(new Object[]{item});
@@ -280,7 +286,7 @@ static Object[] subsParameters(Node node, Object self, PTuple args, PTuple param
280286
SequenceStorage argsStorage = args.getSequenceStorage();
281287
List<Object> newargs = new ArrayList<>(argsStorage.length());
282288
for (int iarg = 0; iarg < argsStorage.length(); iarg++) {
283-
Object arg = argsStorage.getItemNormalized(iarg);
289+
Object arg = getItemUncached(argsStorage, iarg);
284290
if (TypeNodes.IsTypeNode.executeUncached(arg)) {
285291
newargs.add(arg);
286292
continue;
@@ -310,7 +316,7 @@ private static Object subsTvars(Node node, Object obj, PTuple parameters, Object
310316
SequenceStorage subparamsStorage = tuple.getSequenceStorage();
311317
List<Object> subargs = new ArrayList<>(subparamsStorage.length());
312318
for (int i = 0; i < subparamsStorage.length(); i++) {
313-
Object arg = subparamsStorage.getItemNormalized(i);
319+
Object arg = getItemUncached(subparamsStorage, i);
314320
int foundIndex = tupleIndex(parameters, arg);
315321
if (foundIndex >= 0) {
316322
Object param = arg;
@@ -405,7 +411,7 @@ private static Object[] flattenArgs(Object[] args) {
405411
if (args[i] instanceof PUnionType) {
406412
SequenceStorage storage = ((PUnionType) args[i]).getArgs().getSequenceStorage();
407413
for (int j = 0; j < storage.length(); j++) {
408-
flattenedArgs[pos++] = storage.getItemNormalized(j);
414+
flattenedArgs[pos++] = getItemUncached(storage, j);
409415
}
410416
} else {
411417
flattenedArgs[pos++] = args[i] == PNone.NONE ? PythonBuiltinClassType.PNone : args[i];

0 commit comments

Comments
 (0)