Skip to content

Commit 5d01d6f

Browse files
committed
use static key in attribute lookups if possible
1 parent 59bbe5d commit 5d01d6f

29 files changed

+294
-250
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@
4545
import static com.oracle.graal.python.nodes.BuiltinNames.TYPE;
4646
import static com.oracle.graal.python.nodes.BuiltinNames.ZIP;
4747
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__FILE__;
48-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETITEM__;
49-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEW__;
50-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REVERSED__;
5148
import static com.oracle.graal.python.runtime.exception.PythonErrorType.NotImplementedError;
5249
import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
5350
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
@@ -121,6 +118,7 @@
121118
import com.oracle.graal.python.builtins.objects.type.PythonClass;
122119
import com.oracle.graal.python.nodes.PGuards;
123120
import com.oracle.graal.python.nodes.argument.CreateArgumentsNode;
121+
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
124122
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
125123
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
126124
import com.oracle.graal.python.nodes.builtins.ListNodes.ConstructListNode;
@@ -395,15 +393,17 @@ public PythonObject reversed(PythonClass cls, String value) {
395393

396394
@Specialization(guards = {"!isString(sequence)", "!isPRange(sequence)"})
397395
public Object reversed(PythonClass cls, Object sequence,
398-
@Cached("create()") LookupInheritedAttributeNode reversedNode,
396+
@Cached("create()") GetClassNode getClassNode,
397+
@Cached("create(__REVERSED__)") LookupAttributeInMRONode reversedNode,
399398
@Cached("create()") CallUnaryMethodNode callReversedNode,
400399
@Cached("create(__LEN__)") LookupAndCallUnaryNode lenNode,
401-
@Cached("create()") LookupInheritedAttributeNode getItemNode,
400+
@Cached("create(__GETITEM__)") LookupAttributeInMRONode getItemNode,
402401
@Cached("createBinaryProfile()") ConditionProfile noReversedProfile,
403402
@Cached("createBinaryProfile()") ConditionProfile noGetItemProfile) {
404-
Object reversed = reversedNode.execute(sequence, __REVERSED__);
403+
PythonClass sequenceKlass = getClassNode.execute(sequence);
404+
Object reversed = reversedNode.execute(sequenceKlass);
405405
if (noReversedProfile.profile(reversed == PNone.NO_VALUE)) {
406-
Object getItem = getItemNode.execute(sequence, __GETITEM__);
406+
Object getItem = getItemNode.execute(sequenceKlass);
407407
if (noGetItemProfile.profile(getItem == PNone.NO_VALUE)) {
408408
throw raise(TypeError, "'%p' object is not reversible", sequence);
409409
} else {
@@ -1110,12 +1110,12 @@ public Object type(Object cls, Object obj, PNone bases, PNone dict, PKeyword[] k
11101110
@TruffleBoundary
11111111
public Object type(PythonClass cls, String name, PTuple bases, PDict namespace, PKeyword[] kwds,
11121112
@Cached("create()") GetClassNode getMetaclassNode,
1113-
@Cached("create()") LookupInheritedAttributeNode getNewFuncNode,
1113+
@Cached("create(__NEW__)") LookupInheritedAttributeNode getNewFuncNode,
11141114
@Cached("create()") CallDispatchNode callNewFuncNode,
11151115
@Cached("create()") CreateArgumentsNode createArgs) {
11161116
PythonClass metaclass = calculate_metaclass(cls, bases, getMetaclassNode);
11171117
if (metaclass != cls) {
1118-
Object newFunc = getNewFuncNode.execute(metaclass, __NEW__);
1118+
Object newFunc = getNewFuncNode.execute(metaclass);
11191119
if (newFunc instanceof PBuiltinFunction && (((PBuiltinFunction) newFunc).getFunctionRootNode() == getRootNode())) {
11201120
// the new metaclass has the same __new__ function as we are in
11211121
} else {

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@
5454
import static com.oracle.graal.python.nodes.BuiltinNames.SUM;
5555
import static com.oracle.graal.python.nodes.BuiltinNames.__BREAKPOINT__;
5656
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
57-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__CALL__;
58-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__DIR__;
5957
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INSTANCECHECK__;
6058
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LEN__;
6159
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEXT__;
@@ -260,7 +258,7 @@ public boolean callable(PythonCallable callable) {
260258

261259
@Specialization
262260
public boolean callable(Object object,
263-
@Cached("create()") LookupInheritedAttributeNode getAttributeNode) {
261+
@Cached("create(__CALL__)") LookupInheritedAttributeNode getAttributeNode) {
264262
/**
265263
* Added temporarily to skip translation/execution errors in unit testing
266264
*/
@@ -269,7 +267,7 @@ public boolean callable(Object object,
269267
return true;
270268
}
271269

272-
Object callAttr = getAttributeNode.execute(object, __CALL__);
270+
Object callAttr = getAttributeNode.execute(object);
273271
if (callAttr != NO_VALUE) {
274272
return true;
275273
}
@@ -336,10 +334,10 @@ protected boolean isPException(Object object) {
336334

337335
@Specialization(guards = "!isPException(object)")
338336
public Object hash(Object object,
339-
@Cached("create()") LookupInheritedAttributeNode lookupDirNode,
337+
@Cached("create(__DIR__)") LookupInheritedAttributeNode lookupDirNode,
340338
@Cached("create(__HASH__)") LookupAndCallUnaryNode dispatchHash,
341339
@Cached("createIfTrueNode()") CastToBooleanNode trueNode) {
342-
if (trueNode.executeWith(lookupDirNode.execute(object, __DIR__))) {
340+
if (trueNode.executeWith(lookupDirNode.execute(object))) {
343341
return dispatchHash.executeObject(object);
344342
}
345343
return object.hashCode();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/PyNumberMethodsWrapper.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
package com.oracle.graal.python.builtins.objects.cext;
4040

4141
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
42+
import com.oracle.graal.python.builtins.objects.type.PythonClass;
4243
import com.oracle.truffle.api.interop.ForeignAccess;
4344
import com.oracle.truffle.api.interop.TruffleObject;
4445

@@ -47,14 +48,14 @@
4748
*/
4849
public class PyNumberMethodsWrapper extends PythonNativeWrapper {
4950

50-
private final Object delegate;
51+
private final PythonClass delegate;
5152

52-
public PyNumberMethodsWrapper(Object delegate) {
53+
public PyNumberMethodsWrapper(PythonClass delegate) {
5354
this.delegate = delegate;
5455
}
5556

5657
@Override
57-
public Object getDelegate() {
58+
public PythonClass getDelegate() {
5859
return delegate;
5960
}
6061

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/PyNumberMethodsWrapperMR.java

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@
4242
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_INDEX;
4343
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_POW;
4444
import static com.oracle.graal.python.builtins.objects.cext.NativeMemberNames.NB_TRUE_DIVIDE;
45+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ADD__;
46+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INDEX__;
47+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__POW__;
48+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__TRUEDIV__;
4549

4650
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.ToSulongNode;
47-
import com.oracle.graal.python.nodes.SpecialMethodNames;
51+
import com.oracle.graal.python.builtins.objects.type.PythonClass;
4852
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
4953
import com.oracle.truffle.api.CompilerDirectives;
5054
import com.oracle.truffle.api.interop.MessageResolution;
@@ -57,39 +61,50 @@ public class PyNumberMethodsWrapperMR {
5761

5862
@Resolve(message = "READ")
5963
abstract static class ReadNode extends Node {
60-
@Child private LookupAttributeInMRONode getAttributeNode;
64+
@Child private LookupAttributeInMRONode getAddAttributeNode;
65+
@Child private LookupAttributeInMRONode getIndexAttributeNode;
66+
@Child private LookupAttributeInMRONode getPowAttributeNode;
67+
@Child private LookupAttributeInMRONode getTrueDivAttributeNode;
6168
@Child private ToSulongNode toSulongNode;
6269

6370
public Object access(PyNumberMethodsWrapper object, String key) {
6471
// translate key to attribute name
65-
String attributeName = toAttributeName(key);
66-
67-
Object execute = getReadArrayItemNode().execute(object.getDelegate(), attributeName);
68-
return getToSulongNode().execute(execute);
69-
}
70-
71-
private static String toAttributeName(String numberMethodsMember) {
72-
switch (numberMethodsMember) {
72+
PythonClass delegate = object.getDelegate();
73+
Object result;
74+
switch (key) {
7375
case NB_ADD:
74-
return SpecialMethodNames.__ADD__;
76+
if (getAddAttributeNode == null) {
77+
CompilerDirectives.transferToInterpreterAndInvalidate();
78+
getAddAttributeNode = insert(LookupAttributeInMRONode.create(__ADD__));
79+
}
80+
result = getAddAttributeNode.execute(delegate);
81+
break;
7582
case NB_INDEX:
76-
return SpecialMethodNames.__INDEX__;
83+
if (getIndexAttributeNode == null) {
84+
CompilerDirectives.transferToInterpreterAndInvalidate();
85+
getIndexAttributeNode = insert(LookupAttributeInMRONode.create(__INDEX__));
86+
}
87+
result = getIndexAttributeNode.execute(delegate);
88+
break;
7789
case NB_POW:
78-
return SpecialMethodNames.__POW__;
90+
if (getPowAttributeNode == null) {
91+
CompilerDirectives.transferToInterpreterAndInvalidate();
92+
getPowAttributeNode = insert(LookupAttributeInMRONode.create(__POW__));
93+
}
94+
result = getPowAttributeNode.execute(delegate);
95+
break;
7996
case NB_TRUE_DIVIDE:
80-
return SpecialMethodNames.__TRUEDIV__;
97+
if (getTrueDivAttributeNode == null) {
98+
CompilerDirectives.transferToInterpreterAndInvalidate();
99+
getTrueDivAttributeNode = insert(LookupAttributeInMRONode.create(__TRUEDIV__));
100+
}
101+
result = getTrueDivAttributeNode.execute(delegate);
102+
break;
81103
default:
82104
// TODO extend list
83-
throw UnknownIdentifierException.raise(numberMethodsMember);
84-
}
85-
}
86-
87-
private LookupAttributeInMRONode getReadArrayItemNode() {
88-
if (getAttributeNode == null) {
89-
CompilerDirectives.transferToInterpreterAndInvalidate();
90-
getAttributeNode = insert(LookupAttributeInMRONode.create());
105+
throw UnknownIdentifierException.raise(key);
91106
}
92-
return getAttributeNode;
107+
return getToSulongNode().execute(result);
93108
}
94109

95110
private ToSulongNode getToSulongNode() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/PythonObjectNativeWrapperMR.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@
3838
*/
3939
package com.oracle.graal.python.builtins.objects.cext;
4040

41-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTRIBUTE__;
42-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETATTR__;
43-
4441
import com.oracle.graal.python.PythonLanguage;
4542
import com.oracle.graal.python.builtins.objects.PNone;
4643
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
@@ -158,7 +155,7 @@ public Object access(PythonNativeWrapper object, String key) {
158155
}
159156
}
160157

161-
@ImportStatic({NativeMemberNames.class, SpecialMethodNames.class})
158+
@ImportStatic({NativeMemberNames.class, SpecialMethodNames.class, SpecialAttributeNames.class})
162159
@TypeSystemReference(PythonArithmeticTypes.class)
163160
abstract static class ReadNativeMemberNode extends PBaseNode {
164161
@Child GetClassNode getClass = GetClassNode.create();
@@ -246,8 +243,8 @@ Object doTpBase(PythonClass object, @SuppressWarnings("unused") String key) {
246243

247244
@Specialization(guards = "eq(TP_ALLOC, key)")
248245
Object doTpAlloc(PythonClass object, @SuppressWarnings("unused") String key,
249-
@Cached("create()") LookupAttributeInMRONode getAllocNode) {
250-
Object result = getAllocNode.execute(object, SpecialMethodNames.__ALLOC__);
246+
@Cached("create(__ALLOC__)") LookupAttributeInMRONode getAllocNode) {
247+
Object result = getAllocNode.execute(object);
251248
return getToSulongNode().execute(result);
252249
}
253250

@@ -269,26 +266,26 @@ Object doTpAsBuffer(PythonClass object, @SuppressWarnings("unused") String key)
269266

270267
@Specialization(guards = "eq(TP_NEW, key)")
271268
Object doTpNew(PythonClass object, @SuppressWarnings("unused") String key,
272-
@Cached("create()") LookupAttributeInMRONode getAttrNode) {
273-
return ManagedMethodWrappers.createKeywords(getAttrNode.execute(object, SpecialAttributeNames.__NEW__));
269+
@Cached("create(__NEW__)") LookupAttributeInMRONode getAttrNode) {
270+
return ManagedMethodWrappers.createKeywords(getAttrNode.execute(object));
274271
}
275272

276273
@Specialization(guards = "eq(TP_HASH, key)")
277274
Object doTpHash(PythonClass object, @SuppressWarnings("unused") String key,
278-
@Cached("create()") LookupInheritedAttributeNode getHashNode) {
279-
return getToSulongNode().execute(getHashNode.execute(object, SpecialMethodNames.__HASH__));
275+
@Cached("create(__HASH__)") LookupInheritedAttributeNode getHashNode) {
276+
return getToSulongNode().execute(getHashNode.execute(object));
280277
}
281278

282279
@Specialization(guards = "eq(TP_BASICSIZE, key)")
283280
Object doTpBasicsize(PythonClass object, @SuppressWarnings("unused") String key,
284-
@Cached("create()") LookupInheritedAttributeNode getAttrNode) {
285-
return getAttrNode.execute(object, SpecialAttributeNames.__BASICSIZE__);
281+
@Cached("create(__BASICSIZE__)") LookupInheritedAttributeNode getAttrNode) {
282+
return getAttrNode.execute(object);
286283
}
287284

288285
@Specialization(guards = "eq(TP_RICHCOMPARE, key)")
289286
Object doTpRichcompare(PythonClass object, @SuppressWarnings("unused") String key,
290-
@Cached("create()") LookupInheritedAttributeNode getCmpNode) {
291-
return getToSulongNode().execute(getCmpNode.execute(object, SpecialMethodNames.RICHCMP));
287+
@Cached("create(RICHCMP)") LookupInheritedAttributeNode getCmpNode) {
288+
return getToSulongNode().execute(getCmpNode.execute(object));
292289
}
293290

294291
@Specialization(guards = "eq(TP_SUBCLASSES, key)")
@@ -311,14 +308,14 @@ Object doTpSetattr(@SuppressWarnings("unused") PythonClass object, @SuppressWarn
311308

312309
@Specialization(guards = "eq(TP_GETATTRO, key)")
313310
Object doTpGetattro(PythonClass object, @SuppressWarnings("unused") String key,
314-
@Cached("create()") LookupInheritedAttributeNode lookupAttrNode) {
315-
return PyAttributeProcsWrapper.createGetAttrWrapper(lookupAttrNode.execute(object, __GETATTRIBUTE__));
311+
@Cached("create(__GETATTRIBUTE__)") LookupInheritedAttributeNode lookupAttrNode) {
312+
return PyAttributeProcsWrapper.createGetAttrWrapper(lookupAttrNode.execute(object));
316313
}
317314

318315
@Specialization(guards = "eq(TP_SETATTRO, key)")
319316
Object doTpSetattro(PythonClass object, @SuppressWarnings("unused") String key,
320-
@Cached("create()") LookupInheritedAttributeNode lookupAttrNode) {
321-
return PyAttributeProcsWrapper.createSetAttrWrapper(lookupAttrNode.execute(object, __SETATTR__));
317+
@Cached("create(__SETATTR__)") LookupInheritedAttributeNode lookupAttrNode) {
318+
return PyAttributeProcsWrapper.createSetAttrWrapper(lookupAttrNode.execute(object));
322319
}
323320

324321
@Specialization(guards = "eq(OB_ITEM, key)")

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,9 @@ protected static boolean isPDict(PythonObject o) {
290290
protected boolean hasKeysAttribute(PythonObject o) {
291291
if (lookupKeysAttributeNode == null) {
292292
CompilerDirectives.transferToInterpreterAndInvalidate();
293-
lookupKeysAttributeNode = insert(LookupInheritedAttributeNode.create());
293+
lookupKeysAttributeNode = insert(LookupInheritedAttributeNode.create(KEYS));
294294
}
295-
return lookupKeysAttributeNode.execute(o, KEYS) != PNone.NO_VALUE;
295+
return lookupKeysAttributeNode.execute(o) != PNone.NO_VALUE;
296296
}
297297

298298
@Specialization(guards = {"!isNoValue(iterable)", "isEmpty(kwargs)"})

0 commit comments

Comments
 (0)