Skip to content

Commit 17534a4

Browse files
committed
avoid allocation of exception object in common case when globals is a PDict or PMappingproxy builtin
1 parent 3b308d8 commit 17534a4

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

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

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@
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;
3132
import com.oracle.graal.python.builtins.objects.PNone;
33+
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
34+
import com.oracle.graal.python.builtins.objects.dict.PDict;
3235
import com.oracle.graal.python.builtins.objects.function.PArguments;
36+
import com.oracle.graal.python.builtins.objects.mappingproxy.PMappingproxy;
37+
import com.oracle.graal.python.builtins.objects.object.PythonObject;
38+
import com.oracle.graal.python.nodes.SpecialMethodNames;
3339
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
40+
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
3441
import com.oracle.graal.python.nodes.expression.ExpressionNode;
3542
import com.oracle.graal.python.nodes.statement.StatementNode;
3643
import com.oracle.graal.python.nodes.subscript.GetItemNode;
@@ -42,6 +49,7 @@
4249
import com.oracle.truffle.api.dsl.Specialization;
4350
import com.oracle.truffle.api.frame.VirtualFrame;
4451
import com.oracle.truffle.api.nodes.NodeInfo;
52+
import com.oracle.truffle.api.profiles.BranchProfile;
4553
import com.oracle.truffle.api.profiles.ConditionProfile;
4654

4755
@NodeInfo(shortName = "read_global")
@@ -52,6 +60,8 @@ public abstract class ReadGlobalOrBuiltinNode extends ExpressionNode implements
5260
protected final String attributeId;
5361
protected final ConditionProfile isGlobalProfile = ConditionProfile.createBinaryProfile();
5462
protected final ConditionProfile isBuiltinProfile = ConditionProfile.createBinaryProfile();
63+
@Child private HashingStorageNodes.GetItemNode getHashingItemNode;
64+
@Child private GetItemNode readFromDictNode;
5565

5666
protected ReadGlobalOrBuiltinNode(String attributeId) {
5767
this.attributeId = attributeId;
@@ -72,19 +82,49 @@ protected Object readGlobal(VirtualFrame frame) {
7282
return returnGlobalOrBuiltin(result);
7383
}
7484

85+
protected BinaryComparisonNode createContainsNode() {
86+
return BinaryComparisonNode.create(SpecialMethodNames.__CONTAINS__, null, "in");
87+
}
88+
7589
@Specialization(guards = "isInDict(frame)", rewriteOn = PException.class)
7690
protected Object readGlobalDict(VirtualFrame frame,
77-
@Cached("create()") GetItemNode readFromDictNode) {
78-
Object result = readFromDictNode.execute(PArguments.getGlobals(frame), attributeId);
79-
return returnGlobalOrBuiltin(result);
91+
@Cached("create()") BranchProfile isDict,
92+
@Cached("create()") BranchProfile isMappingproxy) {
93+
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+
}
105+
}
106+
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;
80121
}
81122

82123
@Specialization(guards = "isInDict(frame)")
83124
protected Object readGlobalDictWithException(VirtualFrame frame,
84-
@Cached("create()") GetItemNode readFromDictNode,
85125
@Cached("createBinaryProfile()") ConditionProfile errorProfile) {
86126
try {
87-
Object result = readFromDictNode.execute(PArguments.getGlobals(frame), attributeId);
127+
Object result = getReadFromDict().execute(PArguments.getGlobals(frame), attributeId);
88128
return returnGlobalOrBuiltin(result);
89129
} catch (PException e) {
90130
e.expect(KeyError, getCore(), errorProfile);

0 commit comments

Comments
 (0)