Skip to content

Commit 54b15eb

Browse files
committed
Mappingproxy as __dict__ is also common, and should be specialized
1 parent 6919d54 commit 54b15eb

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
*/
4141
package com.oracle.graal.python.nodes.attributes;
4242

43-
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
43+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.PDict;
44+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.PMappingproxy;
45+
4446
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
4547
import com.oracle.graal.python.builtins.objects.object.PythonObject;
4648
import com.oracle.graal.python.builtins.objects.str.PString;
@@ -68,9 +70,9 @@ protected static boolean isDictUnsetOrSameAsStorage(PythonObject object) {
6870
return object.getDict() == null;
6971
}
7072

71-
protected static boolean hasBuiltinDict(PythonObject object, IsBuiltinClassProfile profile) {
73+
protected static boolean hasBuiltinDict(PythonObject object, IsBuiltinClassProfile profileDict, IsBuiltinClassProfile profileMapping) {
7274
PHashingCollection dict = object.getDict();
73-
return dict != null && profile.profileObject(object.getDict(), PythonBuiltinClassType.PDict);
75+
return dict != null && (profileDict.profileObject(dict, PDict) || profileMapping.profileObject(dict, PMappingproxy));
7476
}
7577

7678
protected static Location getLocationOrNull(Property prop) {

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,43 +100,43 @@ protected Object readFromDynamicStorage(PythonObject object, Object key,
100100
return readAttributeFromDynamicObjectNode.execute(object.getStorage(), key);
101101
}
102102

103+
private static Object readDirectlyFromBuiltinDict(PHashingCollection dict, String key,
104+
HashingCollectionNodes.GetDictStorageNode getDictStorage,
105+
HashingStorageNodes.GetItemNode getItemNode) {
106+
// note that we don't need to pass the state here - string keys are hashable by definition
107+
Object value = getItemNode.execute(null, getDictStorage.execute(dict), key);
108+
if (value == null) {
109+
return PNone.NO_VALUE;
110+
} else {
111+
return value;
112+
}
113+
}
114+
103115
// special case for the very common module attribute read
104116
@Specialization(guards = {
105117
"cachedObject == object",
106118
"cachedObject.getDict() == cachedDict",
107-
"hasBuiltinDict(cachedObject, isBuiltinDict)",
119+
"hasBuiltinDict(cachedObject, isBuiltinDict, isBuiltinMappingproxy)",
108120
}, assumptions = "singleContextAssumption", limit = "1")
109121
protected Object readFromBuiltinModuleDict(@SuppressWarnings("unused") PythonModule object, String key,
110122
@SuppressWarnings("unused") @Cached("object") PythonModule cachedObject,
111123
@SuppressWarnings("unused") @Cached("cachedObject.getDict()") PHashingCollection cachedDict,
112124
@SuppressWarnings("unused") @Cached("singleContextAssumption()") Assumption singleContextAssumption,
113125
@Cached HashingCollectionNodes.GetDictStorageNode getDictStorage,
114126
@SuppressWarnings("unused") @Cached IsBuiltinClassProfile isBuiltinDict,
127+
@SuppressWarnings("unused") @Cached IsBuiltinClassProfile isBuiltinMappingproxy,
115128
@Cached HashingStorageNodes.GetItemNode getItemNode) {
116-
Object value = getItemNode.execute(null, getDictStorage.execute(cachedDict), key);
117-
if (value == null) {
118-
return PNone.NO_VALUE;
119-
} else {
120-
return value;
121-
}
129+
return readDirectlyFromBuiltinDict(cachedDict, key, getDictStorage, getItemNode);
122130
}
123131

124132
// read from a builtin dict
125-
@Specialization(guards = {
126-
"!isHiddenKey(key)",
127-
"hasBuiltinDict(object, isBuiltinDict)",
128-
})
133+
@Specialization(guards = {"!isHiddenKey(key)", "hasBuiltinDict(object, isBuiltinDict, isBuiltinMappingproxy)"})
129134
protected Object readFromBuiltinDict(PythonObject object, String key,
130135
@Cached HashingCollectionNodes.GetDictStorageNode getDictStorage,
131136
@SuppressWarnings("unused") @Cached IsBuiltinClassProfile isBuiltinDict,
137+
@SuppressWarnings("unused") @Cached IsBuiltinClassProfile isBuiltinMappingproxy,
132138
@Cached("create()") HashingStorageNodes.GetItemNode getItemNode) {
133-
// note that we don't need to pass the state here - string keys are hashable by definition
134-
Object value = getItemNode.execute(null, getDictStorage.execute(object.getDict()), key);
135-
if (value == null) {
136-
return PNone.NO_VALUE;
137-
} else {
138-
return value;
139-
}
139+
return readDirectlyFromBuiltinDict(object.getDict(), key, getDictStorage, getItemNode);
140140
}
141141

142142
// read from the Dict

0 commit comments

Comments
 (0)