Skip to content

Commit 39a2681

Browse files
committed
enhance ReadAttributeFromObjectNode with functionality to read from object dict if set
1 parent 917a300 commit 39a2681

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/ObjectBuiltins.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import com.oracle.graal.python.builtins.objects.PNone;
5858
import com.oracle.graal.python.builtins.objects.cext.PythonNativeClass;
5959
import com.oracle.graal.python.builtins.objects.cext.PythonNativeObject;
60-
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
6160
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
6261
import com.oracle.graal.python.builtins.objects.dict.PDict;
6362
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
@@ -402,19 +401,6 @@ private LazyPythonClass getDataClass(Object descr) {
402401
@Builtin(name = SpecialMethodNames.__GETATTR__, fixedNumOfPositionalArgs = 2)
403402
@GenerateNodeFactory
404403
public abstract static class GetattrNode extends PythonBinaryBuiltinNode {
405-
@Specialization
406-
Object getattr(PythonObject object, Object key,
407-
@Cached("create()") HashingStorageNodes.GetItemNode getItemNode) {
408-
PHashingCollection dict = object.getDict();
409-
if (dict != null) {
410-
Object value = getItemNode.execute(dict.getDictStorage(), key);
411-
if (value != null) {
412-
return value;
413-
}
414-
}
415-
throw raise(AttributeError, "'%p' object has no attribute %s", object, key);
416-
}
417-
418404
@Specialization
419405
Object getattr(Object object, Object key) {
420406
throw raise(AttributeError, "'%p' object has no attribute %s", object, key);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/attributes/ReadAttributeFromObjectNode.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@
4141
package com.oracle.graal.python.nodes.attributes;
4242

4343
import com.oracle.graal.python.builtins.objects.PNone;
44+
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
45+
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
4446
import com.oracle.graal.python.builtins.objects.object.PythonObject;
4547
import com.oracle.graal.python.builtins.objects.str.PString;
48+
import com.oracle.graal.python.nodes.PGuards;
4649
import com.oracle.graal.python.nodes.PNodeWithContext;
4750
import com.oracle.graal.python.nodes.interop.PForeignToPTypeNode;
48-
import com.oracle.graal.python.nodes.PGuards;
4951
import com.oracle.graal.python.runtime.PythonOptions;
5052
import com.oracle.truffle.api.Assumption;
5153
import com.oracle.truffle.api.CompilerDirectives;
@@ -102,6 +104,7 @@ protected Object attrKey(Object key) {
102104
@Specialization(limit = "1", //
103105
guards = {
104106
"object == cachedObject",
107+
"cachedDict == null",
105108
"checkShape(object, cachedObject, cachedShape)",
106109
"key == cachedKey",
107110
"!isNull(loc)",
@@ -116,6 +119,7 @@ protected Object readDirectFinal(PythonObject object, Object key,
116119
@Cached("key") Object cachedKey,
117120
@Cached("attrKey(key)") Object attrKey,
118121
@Cached("object.getStorage().getShape()") Shape cachedShape,
122+
@Cached("object.getDict()") PHashingCollection cachedDict,
119123
@Cached("cachedShape.getValidAssumption()") Assumption layoutAssumption,
120124
@Cached("getLocationOrNull(cachedShape.getProperty(attrKey))") Location loc,
121125
@Cached("loc.getFinalAssumption()") Assumption finalAssumption,
@@ -133,6 +137,7 @@ private static boolean assertFinal(PythonObject object, Object key, Object cache
133137
@Specialization(limit = "getIntOption(getContext(), AttributeAccessInlineCacheMaxDepth)", //
134138
guards = {
135139
"object.getStorage().getShape() == cachedShape",
140+
"cachedDict == null",
136141
"key == cachedKey",
137142
"isNull(loc) || !loc.isAssumedFinal()",
138143
}, //
@@ -141,6 +146,7 @@ protected Object readDirect(PythonObject object, Object key,
141146
@Cached("key") Object cachedKey,
142147
@Cached("attrKey(cachedKey)") Object attrKey,
143148
@Cached("object.getStorage().getShape()") Shape cachedShape,
149+
@Cached("object.getDict()") PHashingCollection cachedDict,
144150
@Cached("cachedShape.getValidAssumption()") Assumption layoutAssumption,
145151
@Cached("getLocationOrNull(cachedShape.getProperty(attrKey))") Location loc) {
146152
if (loc == null) {
@@ -153,18 +159,20 @@ protected Object readDirect(PythonObject object, Object key,
153159
@SuppressWarnings("unused")
154160
@Specialization(guards = {
155161
"object.getStorage().getShape() == cachedShape",
162+
"cachedDict == null",
156163
"!layoutAssumption.isValid()"
157164
})
158165
protected Object updateShapeAndRead(PythonObject object, Object key,
159166
@Cached("object.getStorage().getShape()") Shape cachedShape,
167+
@Cached("object.getDict()") PHashingCollection cachedDict,
160168
@Cached("cachedShape.getValidAssumption()") Assumption layoutAssumption,
161169
@Cached("create()") ReadAttributeFromObjectNode nextNode) {
162170
CompilerDirectives.transferToInterpreter();
163171
object.getStorage().updateShape();
164172
return nextNode.execute(object, key);
165173
}
166174

167-
@Specialization(replaces = "readDirect")
175+
@Specialization(guards = "object.getDict() == null", replaces = "readDirect")
168176
protected Object readIndirect(PythonObject object, Object key) {
169177
Object value = object.getStorage().get(attrKey(key));
170178
if (value == null) {
@@ -174,6 +182,17 @@ protected Object readIndirect(PythonObject object, Object key) {
174182
}
175183
}
176184

185+
@Specialization(guards = "object.getDict() != null")
186+
protected Object readFromDict(PythonObject object, Object key,
187+
@Cached("create()") HashingStorageNodes.GetItemNode getItemNode) {
188+
Object value = getItemNode.execute(object.getDict().getDictStorage(), key);
189+
if (value == null) {
190+
return PNone.NO_VALUE;
191+
} else {
192+
return value;
193+
}
194+
}
195+
177196
@Specialization(guards = "isForeignObject(object)")
178197
protected Object readForeign(TruffleObject object, Object key,
179198
@Cached("create()") PForeignToPTypeNode fromForeign,

0 commit comments

Comments
 (0)