Skip to content

Commit dd7e6c1

Browse files
committed
change SetAttributeNode to use a fixed key, use lookupStableAssumption only for mro changes, use "mro" optimization also for unsuccessful lookups
1 parent d3a568b commit dd7e6c1

File tree

10 files changed

+78
-53
lines changed

10 files changed

+78
-53
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ Object round(Object x, Object n,
11451145
public abstract static class SetAttrNode extends PythonBuiltinNode {
11461146
@Specialization
11471147
public Object setAttr(Object object, Object key, Object value,
1148-
@Cached("create()") SetAttributeNode setAttrNode) {
1148+
@Cached("new()") SetAttributeNode.Dynamic setAttrNode) {
11491149
return setAttrNode.execute(object, key, value);
11501150
}
11511151
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/NodeFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ public PNode createComparisonOperation(String operator, PNode left, PNode right)
394394
}
395395

396396
public PNode createGetAttribute(PNode primary, String name) {
397-
return GetAttributeNode.create(primary, createStringLiteral(name));
397+
return GetAttributeNode.create(name, primary);
398398
}
399399

400400
public PNode createGetItem(PNode primary, String name) {
@@ -522,7 +522,7 @@ public PNode callBuiltin(String string, PNode argument) {
522522
}
523523

524524
public PNode createSetAttribute(PNode object, String key, PNode rhs) {
525-
return SetAttributeNode.create(object, createStringLiteral(key), rhs);
525+
return SetAttributeNode.create(key, object, rhs);
526526
}
527527

528528
public PNode createDestructuringAssignment(PNode rhs, List<ReadNode> slots, int starredIndex, PNode[] assignments) {

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,39 +46,46 @@
4646
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
4747
import com.oracle.graal.python.nodes.frame.ReadNode;
4848
import com.oracle.truffle.api.dsl.NodeChild;
49-
import com.oracle.truffle.api.dsl.NodeChildren;
5049
import com.oracle.truffle.api.dsl.Specialization;
5150
import com.oracle.truffle.api.nodes.UnexpectedResultException;
5251

53-
@NodeChildren({@NodeChild(value = "object", type = PNode.class), @NodeChild(value = "key", type = PNode.class)})
52+
@NodeChild(value = "object", type = PNode.class)
5453
public abstract class GetAttributeNode extends PNode implements ReadNode {
5554

55+
private final String key;
56+
5657
@Child LookupAndCallBinaryNode dispatchNode = LookupAndCallBinaryNode.create(__GETATTRIBUTE__);
5758

58-
public PNode makeWriteNode(PNode rhs) {
59-
return SetAttributeNode.create(getObject(), getKey(), rhs);
59+
protected GetAttributeNode(String key) {
60+
this.key = key;
6061
}
6162

62-
public static GetAttributeNode create(PNode object, PNode key) {
63-
return GetAttributeNodeGen.create(object, key);
63+
public final String getKey() {
64+
return key;
6465
}
6566

66-
public abstract PNode getObject();
67+
public final PNode makeWriteNode(PNode rhs) {
68+
return SetAttributeNode.create(key, getObject(), rhs);
69+
}
6770

68-
public abstract PNode getKey();
71+
public static GetAttributeNode create(String key, PNode object) {
72+
return GetAttributeNodeGen.create(key, object);
73+
}
74+
75+
public abstract PNode getObject();
6976

7077
@Specialization(rewriteOn = UnexpectedResultException.class)
71-
protected int doItInt(Object object, Object key) throws UnexpectedResultException {
78+
protected int doItInt(Object object) throws UnexpectedResultException {
7279
return dispatchNode.executeInt(object, key);
7380
}
7481

7582
@Specialization(rewriteOn = UnexpectedResultException.class)
76-
protected boolean doItBoolean(Object object, Object key) throws UnexpectedResultException {
83+
protected boolean doItBoolean(Object object) throws UnexpectedResultException {
7784
return dispatchNode.executeBool(object, key);
7885
}
7986

8087
@Specialization(replaces = {"doItInt", "doItBoolean"})
81-
protected Object doIt(Object object, Object key) {
88+
protected Object doIt(Object object) {
8289
return dispatchNode.executeObject(object, key);
8390
}
8491
}

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

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,62 +40,75 @@
4040
*/
4141
package com.oracle.graal.python.nodes.attributes;
4242

43-
import com.oracle.graal.python.builtins.objects.type.PythonClass;
43+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETATTR__;
44+
45+
import com.oracle.graal.python.nodes.PBaseNode;
4446
import com.oracle.graal.python.nodes.PNode;
4547
import com.oracle.graal.python.nodes.call.special.CallTernaryMethodNode;
4648
import com.oracle.graal.python.nodes.frame.WriteNode;
4749
import com.oracle.graal.python.nodes.object.GetClassNode;
48-
import com.oracle.truffle.api.CompilerDirectives;
4950
import com.oracle.truffle.api.dsl.Cached;
5051
import com.oracle.truffle.api.dsl.NodeChild;
5152
import com.oracle.truffle.api.dsl.NodeChildren;
5253
import com.oracle.truffle.api.dsl.Specialization;
5354
import com.oracle.truffle.api.frame.VirtualFrame;
5455
import com.oracle.truffle.api.profiles.ValueProfile;
5556

56-
@NodeChildren({@NodeChild(value = "object", type = PNode.class), @NodeChild(value = "key", type = PNode.class), @NodeChild(value = "rhs", type = PNode.class)})
57+
@NodeChildren({@NodeChild(value = "object", type = PNode.class), @NodeChild(value = "rhs", type = PNode.class)})
5758
public abstract class SetAttributeNode extends PNode implements WriteNode {
58-
protected abstract PNode getObject();
5959

60-
protected abstract PNode getKey();
60+
public static final class Dynamic extends PBaseNode {
61+
private final ValueProfile setAttributeProfile = ValueProfile.createIdentityProfile();
62+
@Child private GetClassNode getClassNode = GetClassNode.create();
63+
@Child private LookupAttributeInMRONode setAttributeLookup = LookupAttributeInMRONode.create(__SETATTR__);
64+
@Child private CallTernaryMethodNode callSetAttribute = CallTernaryMethodNode.create();
65+
66+
public Object execute(Object object, Object key, Object value) {
67+
Object descr = setAttributeProfile.profile(setAttributeLookup.execute(getClassNode.execute(object)));
68+
return callSetAttribute.execute(descr, object, key, value);
69+
}
70+
}
71+
72+
private final String key;
73+
74+
protected SetAttributeNode(String key) {
75+
this.key = key;
76+
}
77+
78+
protected abstract PNode getObject();
6179

6280
public abstract PNode getRhs();
6381

64-
public static SetAttributeNode create() {
65-
return create(null, null, null);
82+
public static SetAttributeNode create(String key) {
83+
return create(key, null, null);
6684
}
6785

68-
public static SetAttributeNode create(PNode object, PNode key, PNode rhs) {
69-
return SetAttributeNodeGen.create(object, key, rhs);
86+
public static SetAttributeNode create(String key, PNode object, PNode rhs) {
87+
return SetAttributeNodeGen.create(key, object, rhs);
7088
}
7189

72-
public Object doWrite(VirtualFrame frame, Object value) {
73-
return execute(getObject().execute(frame), getKey().execute(frame), value);
90+
@Override
91+
public final Object doWrite(VirtualFrame frame, Object value) {
92+
return execute(getObject().execute(frame), value);
7493
}
7594

76-
public abstract Object execute(Object object, Object key, Object value);
95+
public abstract Object execute(Object object, Object value);
7796

7897
public String getAttributeId() {
79-
Object key = getKey().execute(null);
80-
if (!(key instanceof String)) {
81-
CompilerDirectives.transferToInterpreter();
82-
throw new AssertionError();
83-
}
84-
return (String) key;
98+
return key;
8599
}
86100

87101
public PNode getPrimaryNode() {
88102
return getObject();
89103
}
90104

91105
@Specialization
92-
protected Object doIt(Object object, Object key, Object value,
106+
protected Object doIt(Object object, Object value,
93107
@Cached("createIdentityProfile()") ValueProfile setAttributeProfile,
94108
@Cached("create()") GetClassNode getClassNode,
95109
@Cached("create(__SETATTR__)") LookupAttributeInMRONode setAttributeLookup,
96110
@Cached("create()") CallTernaryMethodNode callSetAttribute) {
97-
PythonClass type = getClassNode.execute(object);
98-
Object descr = setAttributeProfile.profile(setAttributeLookup.execute(type));
111+
Object descr = setAttributeProfile.profile(setAttributeLookup.execute(getClassNode.execute(object)));
99112
return callSetAttribute.execute(descr, object, key, value);
100113
}
101114
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/PythonCallNode.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ public static PythonCallNode create(PNode calleeNode, PNode[] argumentNodes, PNo
8787
if (calleeNode instanceof ReadGlobalOrBuiltinNode) {
8888
calleeName = ((ReadGlobalOrBuiltinNode) calleeNode).getAttributeId();
8989
} else if (calleeNode instanceof GetAttributeNode) {
90-
PNode key = ((GetAttributeNode) calleeNode).getKey();
91-
getCallableNode = GetCallAttributeNodeGen.create(((StringLiteralNode) key).getValue(), ((GetAttributeNode) calleeNode).getObject());
90+
getCallableNode = GetCallAttributeNodeGen.create(((GetAttributeNode) calleeNode).getKey(), ((GetAttributeNode) calleeNode).getObject());
9291
}
9392
KeywordArgumentsNode keywordArgumentsNode = kwArgs == null && keywords.length == 0 ? null : KeywordArgumentsNode.create(keywords, kwArgs);
9493
if (starArgs == null) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/expression/CastToListNode.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
5252
import com.oracle.graal.python.nodes.builtins.ListNodes.ConstructListNode;
5353
import com.oracle.graal.python.nodes.literal.BuiltinsLiteralNode;
54-
import com.oracle.graal.python.nodes.literal.StringLiteralNode;
5554
import com.oracle.graal.python.nodes.object.GetClassNode;
5655
import com.oracle.graal.python.runtime.exception.PException;
5756
import com.oracle.truffle.api.CompilerDirectives;
@@ -117,7 +116,7 @@ protected PList starredList(PList v) {
117116
}
118117

119118
protected GetAttributeNode getList() {
120-
return GetAttributeNode.create(new BuiltinsLiteralNode(), new StringLiteralNode("list"));
119+
return GetAttributeNode.create("list", new BuiltinsLiteralNode());
121120
}
122121

123122
@Specialization(rewriteOn = PException.class)

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
3535
import com.oracle.graal.python.nodes.subscript.GetItemNode;
3636
import com.oracle.graal.python.runtime.exception.PException;
37+
import com.oracle.truffle.api.CompilerDirectives;
3738
import com.oracle.truffle.api.dsl.Cached;
3839
import com.oracle.truffle.api.dsl.Specialization;
3940
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -43,8 +44,8 @@
4344
@NodeInfo(shortName = "read_global")
4445
public abstract class ReadGlobalOrBuiltinNode extends GlobalNode implements ReadNode {
4546
@Child private ReadAttributeFromObjectNode readFromModuleNode = ReadAttributeFromObjectNode.create();
46-
@Child private GetItemNode readFromDictNode = GetItemNode.create();
47-
@Child private ReadAttributeFromObjectNode readFromBuiltinsNode = ReadAttributeFromObjectNode.create();
47+
@Child private ReadAttributeFromObjectNode readFromBuiltinsNode;
48+
4849
protected final String attributeId;
4950
protected final ConditionProfile isGlobalProfile = ConditionProfile.createBinaryProfile();
5051
protected final ConditionProfile isBuiltinProfile = ConditionProfile.createBinaryProfile();
@@ -64,21 +65,23 @@ public PNode makeWriteNode(PNode rhs) {
6465

6566
@Specialization(guards = "isInModule(frame)")
6667
protected Object readGlobal(VirtualFrame frame) {
67-
final Object result = readFromModuleNode.execute(PArguments.getGlobals(frame), attributeId);
68+
Object result = readFromModuleNode.execute(PArguments.getGlobals(frame), attributeId);
6869
return returnGlobalOrBuiltin(result);
6970
}
7071

7172
@Specialization(guards = "isInDict(frame)", rewriteOn = PException.class)
72-
protected Object readGlobalDict(VirtualFrame frame) {
73-
final Object result = readFromDictNode.execute(PArguments.getGlobals(frame), attributeId);
73+
protected Object readGlobalDict(VirtualFrame frame,
74+
@Cached("create()") GetItemNode readFromDictNode) {
75+
Object result = readFromDictNode.execute(PArguments.getGlobals(frame), attributeId);
7476
return returnGlobalOrBuiltin(result);
7577
}
7678

7779
@Specialization(guards = "isInDict(frame)")
7880
protected Object readGlobalDictWithException(VirtualFrame frame,
81+
@Cached("create()") GetItemNode readFromDictNode,
7982
@Cached("createBinaryProfile()") ConditionProfile errorProfile) {
8083
try {
81-
final Object result = readFromDictNode.execute(PArguments.getGlobals(frame), attributeId);
84+
Object result = readFromDictNode.execute(PArguments.getGlobals(frame), attributeId);
8285
return returnGlobalOrBuiltin(result);
8386
} catch (PException e) {
8487
e.expect(KeyError, getCore(), errorProfile);
@@ -90,11 +93,15 @@ public Object readGlobal(Object globals) {
9093
return returnGlobalOrBuiltin(globals);
9194
}
9295

93-
private Object returnGlobalOrBuiltin(final Object result) {
96+
private Object returnGlobalOrBuiltin(Object result) {
9497
if (isGlobalProfile.profile(result != PNone.NO_VALUE)) {
9598
return result;
9699
} else {
97-
final Object builtin = readFromBuiltinsNode.execute(getCore().isInitialized() ? getContext().getBuiltins() : getCore().lookupBuiltinModule("builtins"), attributeId);
100+
if (readFromBuiltinsNode == null) {
101+
CompilerDirectives.transferToInterpreterAndInvalidate();
102+
readFromBuiltinsNode = insert(ReadAttributeFromObjectNode.create());
103+
}
104+
Object builtin = readFromBuiltinsNode.execute(getCore().isInitialized() ? getContext().getBuiltins() : getCore().lookupBuiltinModule("builtins"), attributeId);
98105
if (isBuiltinProfile.profile(builtin != PNone.NO_VALUE)) {
99106
return builtin;
100107
} else {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
@NodeChildren({@NodeChild(value = "rhs", type = PNode.class)})
5555
public abstract class WriteGlobalNode extends GlobalNode implements WriteNode {
56-
private final String attributeId;
56+
protected final String attributeId;
5757

5858
WriteGlobalNode(String attributeId) {
5959
this.attributeId = attributeId;
@@ -134,8 +134,8 @@ Object writeDict1(VirtualFrame frame, Object value,
134134

135135
@Specialization(guards = "isInModule(frame)")
136136
Object writeDict(VirtualFrame frame, Object value,
137-
@Cached("create()") SetAttributeNode storeNode) {
138-
storeNode.execute(PArguments.getGlobals(frame), attributeId, value);
137+
@Cached("create(attributeId)") SetAttributeNode storeNode) {
138+
storeNode.execute(PArguments.getGlobals(frame), value);
139139
return PNone.NONE;
140140
}
141141

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/ImportStarNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class ImportStarNode extends AbstractImportNode {
4242
private final int level;
4343

4444
@Child private SetItemNode dictWriteNode = null;
45-
@Child private SetAttributeNode setAttributeNode = null;
45+
@Child private SetAttributeNode.Dynamic setAttributeNode = null;
4646

4747
// TODO: remove once we removed PythonModule globals
4848

@@ -56,7 +56,7 @@ public void writeAttribute(PythonObject globals, String name, Object value) {
5656
} else {
5757
if (setAttributeNode == null) {
5858
CompilerDirectives.transferToInterpreterAndInvalidate();
59-
setAttributeNode = insert(SetAttributeNode.create());
59+
setAttributeNode = insert(new SetAttributeNode.Dynamic());
6060
}
6161
setAttributeNode.execute(globals, name, value);
6262
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/interop/PythonMessageResolution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ Object access(Object object) {
304304
@Resolve(message = "WRITE")
305305
abstract static class WriteNode extends Node {
306306
@Child private SetItemNode setItemNode = SetItemNode.create();
307-
@Child private SetAttributeNode writeNode = SetAttributeNode.create();
307+
@Child private SetAttributeNode.Dynamic writeNode = new SetAttributeNode.Dynamic();
308308
@Child private IsMappingNode isMapping = IsMappingNode.create();
309309
@Child private HasSetItem hasSetItem = new HasSetItem();
310310
@Child private KeyForAttributeAccess getAttributeKey = new KeyForAttributeAccess();

0 commit comments

Comments
 (0)