Skip to content

Commit d1151fc

Browse files
committed
globals must always be dict subclasses, so just specialize on builtin dict type and normal dicts
1 parent 67ab9ff commit d1151fc

File tree

3 files changed

+46
-59
lines changed

3 files changed

+46
-59
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/GlobalNode.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,23 @@
4242

4343
import com.oracle.graal.python.builtins.objects.dict.PDict;
4444
import com.oracle.graal.python.builtins.objects.function.PArguments;
45-
import com.oracle.graal.python.builtins.objects.mappingproxy.PMappingproxy;
4645
import com.oracle.graal.python.builtins.objects.module.PythonModule;
46+
import com.oracle.graal.python.builtins.objects.object.PythonObject;
47+
import com.oracle.graal.python.nodes.PGuards;
4748
import com.oracle.truffle.api.frame.VirtualFrame;
4849

4950
public interface GlobalNode {
5051
default boolean isInModule(VirtualFrame frame) {
5152
return PArguments.getGlobals(frame) instanceof PythonModule;
5253
}
5354

55+
default boolean isInBuiltinDict(VirtualFrame frame) {
56+
Object globals = PArguments.getGlobals(frame);
57+
return globals instanceof PDict && PGuards.isPythonBuiltinClass(((PythonObject) globals).getPythonClass());
58+
}
59+
5460
default boolean isInDict(VirtualFrame frame) {
5561
Object globals = PArguments.getGlobals(frame);
56-
return globals instanceof PDict || globals instanceof PMappingproxy;
62+
return globals instanceof PDict;
5763
}
5864
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/ReadGlobalOrBuiltinNode.java

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,12 @@
2828
import static com.oracle.graal.python.runtime.exception.PythonErrorType.KeyError;
2929
import static com.oracle.graal.python.runtime.exception.PythonErrorType.NameError;
3030

31-
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
3231
import com.oracle.graal.python.builtins.objects.PNone;
3332
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
3433
import com.oracle.graal.python.builtins.objects.dict.PDict;
3534
import com.oracle.graal.python.builtins.objects.function.PArguments;
36-
import com.oracle.graal.python.builtins.objects.mappingproxy.PMappingproxy;
3735
import com.oracle.graal.python.builtins.objects.object.PythonObject;
38-
import com.oracle.graal.python.nodes.SpecialMethodNames;
3936
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
40-
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
4137
import com.oracle.graal.python.nodes.expression.ExpressionNode;
4238
import com.oracle.graal.python.nodes.statement.StatementNode;
4339
import com.oracle.graal.python.nodes.subscript.GetItemNode;
@@ -49,7 +45,6 @@
4945
import com.oracle.truffle.api.dsl.Specialization;
5046
import com.oracle.truffle.api.frame.VirtualFrame;
5147
import com.oracle.truffle.api.nodes.NodeInfo;
52-
import com.oracle.truffle.api.profiles.BranchProfile;
5348
import com.oracle.truffle.api.profiles.ConditionProfile;
5449

5550
@NodeInfo(shortName = "read_global")
@@ -82,60 +77,33 @@ protected Object readGlobal(VirtualFrame frame) {
8277
return returnGlobalOrBuiltin(result);
8378
}
8479

85-
protected BinaryComparisonNode createContainsNode() {
86-
return BinaryComparisonNode.create(SpecialMethodNames.__CONTAINS__, null, "in");
87-
}
88-
89-
@Specialization(guards = "isInDict(frame)", rewriteOn = PException.class)
80+
@Specialization(guards = "isInBuiltinDict(frame)")
9081
protected Object readGlobalDict(VirtualFrame frame,
91-
@Cached("create()") BranchProfile isDict,
92-
@Cached("create()") BranchProfile isMappingproxy) {
82+
@Cached("create()") HashingStorageNodes.GetItemNode getItemNode) {
9383
PythonObject globals = PArguments.getGlobals(frame);
94-
if (globals instanceof PMappingproxy && globals.getPythonClass() == lookupClass(PythonBuiltinClassType.PMappingproxy)) {
95-
isMappingproxy.enter();
96-
Object result = getGetItemNode().execute(((PMappingproxy) globals).getDictStorage(), attributeId);
97-
return returnGlobalOrBuiltin(result == null ? PNone.NO_VALUE : result);
98-
} else if (globals instanceof PDict && globals.getPythonClass() == lookupClass(PythonBuiltinClassType.PDict)) {
99-
isDict.enter();
100-
Object result = getGetItemNode().execute(((PDict) globals).getDictStorage(), attributeId);
101-
return returnGlobalOrBuiltin(result == null ? PNone.NO_VALUE : result);
102-
} else {
103-
return returnGlobalOrBuiltin(getReadFromDict().execute(globals, attributeId));
104-
}
84+
Object result = getItemNode.execute(((PDict) globals).getDictStorage(), attributeId);
85+
return returnGlobalOrBuiltin(result == null ? PNone.NO_VALUE : result);
10586
}
10687

107-
private GetItemNode getReadFromDict() {
108-
if (readFromDictNode == null) {
109-
CompilerDirectives.transferToInterpreterAndInvalidate();
110-
readFromDictNode = insert(GetItemNode.create());
111-
}
112-
return readFromDictNode;
113-
}
114-
115-
private HashingStorageNodes.GetItemNode getGetItemNode() {
116-
if (getHashingItemNode == null) {
117-
CompilerDirectives.transferToInterpreterAndInvalidate();
118-
getHashingItemNode = insert(HashingStorageNodes.GetItemNode.create());
119-
}
120-
return getHashingItemNode;
88+
@Specialization(guards = "isInDict(frame)", rewriteOn = PException.class)
89+
protected Object readGlobalDict(VirtualFrame frame,
90+
@Cached("create()") GetItemNode getItemNode) {
91+
return returnGlobalOrBuiltin(getItemNode.execute(PArguments.getGlobals(frame), attributeId));
12192
}
12293

12394
@Specialization(guards = "isInDict(frame)")
12495
protected Object readGlobalDictWithException(VirtualFrame frame,
96+
@Cached("create()") GetItemNode getItemNode,
12597
@Cached("createBinaryProfile()") ConditionProfile errorProfile) {
12698
try {
127-
Object result = getReadFromDict().execute(PArguments.getGlobals(frame), attributeId);
99+
Object result = getItemNode.execute(PArguments.getGlobals(frame), attributeId);
128100
return returnGlobalOrBuiltin(result);
129101
} catch (PException e) {
130102
e.expect(KeyError, getCore(), errorProfile);
131103
return returnGlobalOrBuiltin(PNone.NO_VALUE);
132104
}
133105
}
134106

135-
public Object readGlobal(Object globals) {
136-
return returnGlobalOrBuiltin(globals);
137-
}
138-
139107
private Object returnGlobalOrBuiltin(Object result) {
140108
if (isGlobalProfile.profile(result != PNone.NO_VALUE)) {
141109
return result;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/WriteGlobalNode.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
*/
4141
package com.oracle.graal.python.nodes.frame;
4242

43+
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
44+
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
45+
import com.oracle.graal.python.builtins.objects.dict.PDict;
4346
import com.oracle.graal.python.builtins.objects.function.PArguments;
4447
import com.oracle.graal.python.nodes.attributes.SetAttributeNode;
4548
import com.oracle.graal.python.nodes.expression.ExpressionNode;
@@ -97,38 +100,48 @@ public void doWrite(VirtualFrame frame, Object value) {
97100

98101
public abstract void executeWithValue(VirtualFrame frame, Object value);
99102

100-
@Specialization(guards = "isInDict(frame)")
103+
private static HashingStorage getDictStorage(VirtualFrame frame) {
104+
return ((PDict) PArguments.getGlobals(frame)).getDictStorage();
105+
}
106+
107+
@Specialization(guards = "isInBuiltinDict(frame)")
101108
void writeDictBoolean(VirtualFrame frame, boolean value,
102-
@Cached("create()") SetItemNode storeNode) {
103-
storeNode.executeWith(PArguments.getGlobals(frame), attributeId, value);
109+
@Cached("create()") HashingStorageNodes.SetItemNode storeNode) {
110+
storeNode.execute(getDictStorage(frame), attributeId, value);
104111
}
105112

106-
@Specialization(guards = "isInDict(frame)")
113+
@Specialization(guards = "isInBuiltinDict(frame)")
107114
void writeDictInt(VirtualFrame frame, int value,
108-
@Cached("create()") SetItemNode storeNode) {
109-
storeNode.executeWith(PArguments.getGlobals(frame), attributeId, value);
115+
@Cached("create()") HashingStorageNodes.SetItemNode storeNode) {
116+
storeNode.execute(getDictStorage(frame), attributeId, value);
110117
}
111118

112-
@Specialization(guards = "isInDict(frame)")
119+
@Specialization(guards = "isInBuiltinDict(frame)")
113120
void writeDictLong(VirtualFrame frame, long value,
114-
@Cached("create()") SetItemNode storeNode) {
115-
storeNode.executeWith(PArguments.getGlobals(frame), attributeId, value);
121+
@Cached("create()") HashingStorageNodes.SetItemNode storeNode) {
122+
storeNode.execute(getDictStorage(frame), attributeId, value);
116123
}
117124

118-
@Specialization(guards = "isInDict(frame)")
125+
@Specialization(guards = "isInBuiltinDict(frame)")
119126
void writeDictDouble(VirtualFrame frame, double value,
120-
@Cached("create()") SetItemNode storeNode) {
121-
storeNode.executeWith(PArguments.getGlobals(frame), attributeId, value);
127+
@Cached("create()") HashingStorageNodes.SetItemNode storeNode) {
128+
storeNode.execute(getDictStorage(frame), attributeId, value);
129+
}
130+
131+
@Specialization(replaces = {"writeDictBoolean", "writeDictInt", "writeDictLong", "writeDictDouble"}, guards = "isInBuiltinDict(frame)")
132+
void writeDictObject(VirtualFrame frame, Object value,
133+
@Cached("create()") HashingStorageNodes.SetItemNode storeNode) {
134+
storeNode.execute(getDictStorage(frame), attributeId, value);
122135
}
123136

124-
@Specialization(replaces = {"writeDictBoolean", "writeDictInt", "writeDictLong", "writeDictDouble"}, guards = "isInDict(frame)")
125-
void writeDict1(VirtualFrame frame, Object value,
137+
@Specialization(replaces = {"writeDictBoolean", "writeDictInt", "writeDictLong", "writeDictDouble", "writeDictObject"}, guards = "isInDict(frame)")
138+
void writeGenericDict(VirtualFrame frame, Object value,
126139
@Cached("create()") SetItemNode storeNode) {
127140
storeNode.executeWith(PArguments.getGlobals(frame), attributeId, value);
128141
}
129142

130143
@Specialization(guards = "isInModule(frame)")
131-
void writeDict(VirtualFrame frame, Object value,
144+
void writeModule(VirtualFrame frame, Object value,
132145
@Cached("create(attributeId)") SetAttributeNode storeNode) {
133146
storeNode.executeVoid(PArguments.getGlobals(frame), value);
134147
}

0 commit comments

Comments
 (0)