Skip to content

Commit 7b14abe

Browse files
committed
GetAttributeNode: simplify and remove (unused) boxing optimizations
1 parent a723fec commit 7b14abe

File tree

1 file changed

+35
-134
lines changed

1 file changed

+35
-134
lines changed

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

Lines changed: 35 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646

4747
import com.oracle.graal.python.builtins.objects.PNone;
4848
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
49-
import com.oracle.graal.python.nodes.attributes.GetAttributeNodeGen.GetAnyAttributeNodeGen;
50-
import com.oracle.graal.python.nodes.attributes.GetAttributeNodeGen.GetFixedAttributeNodeGen;
5149
import com.oracle.graal.python.nodes.call.special.CallBinaryMethodNode;
5250
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
5351
import com.oracle.graal.python.nodes.call.special.LookupSpecialMethodNode;
@@ -58,88 +56,68 @@
5856
import com.oracle.graal.python.runtime.exception.PException;
5957
import com.oracle.truffle.api.CompilerDirectives;
6058
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
61-
import com.oracle.truffle.api.dsl.NodeChild;
62-
import com.oracle.truffle.api.dsl.Specialization;
6359
import com.oracle.truffle.api.frame.VirtualFrame;
6460
import com.oracle.truffle.api.nodes.Node;
65-
import com.oracle.truffle.api.nodes.UnexpectedResultException;
6661
import com.oracle.truffle.api.profiles.ConditionProfile;
6762

68-
@NodeChild(value = "object", type = ExpressionNode.class)
69-
public abstract class GetAttributeNode extends ExpressionNode implements ReadNode {
63+
public final class GetAttributeNode extends ExpressionNode implements ReadNode {
7064

7165
@Child private GetFixedAttributeNode getFixedAttributeNode;
66+
@Child private ExpressionNode objectExpression;
7267

73-
public abstract int executeInt(VirtualFrame frame, Object object);
74-
75-
public abstract boolean executeBoolean(VirtualFrame frame, Object object);
76-
77-
public abstract Object executeObject(VirtualFrame frame, Object object);
78-
79-
protected GetAttributeNode(String key) {
80-
getFixedAttributeNode = GetFixedAttributeNode.create(key);
68+
@Override
69+
public Object execute(VirtualFrame frame) {
70+
return executeObject(frame, objectExpression.execute(frame));
8171
}
8272

83-
@Specialization(rewriteOn = UnexpectedResultException.class)
84-
protected int doItInt(VirtualFrame frame, Object object) throws UnexpectedResultException {
85-
return getFixedAttributeNode.executeInt(frame, object);
86-
}
87-
88-
@Specialization(rewriteOn = UnexpectedResultException.class)
89-
protected boolean doItBoolean(VirtualFrame frame, Object object) throws UnexpectedResultException {
90-
return getFixedAttributeNode.executeBoolean(frame, object);
73+
public Object executeObject(VirtualFrame frame, Object object) {
74+
return getFixedAttributeNode.executeObject(frame, object);
9175
}
9276

93-
@Specialization(replaces = {"doItInt", "doItBoolean"})
94-
protected Object doIt(VirtualFrame frame, Object object) {
95-
return getFixedAttributeNode.executeObject(frame, object);
77+
protected GetAttributeNode(String key, ExpressionNode object) {
78+
getFixedAttributeNode = GetFixedAttributeNode.create(key);
79+
objectExpression = object;
9680
}
9781

9882
public final String getKey() {
9983
return getFixedAttributeNode.key;
10084
}
10185

10286
public static GetAttributeNode create(String key, ExpressionNode object) {
103-
return GetAttributeNodeGen.create(key, object);
87+
return new GetAttributeNode(key, object);
10488
}
10589

10690
public static GetAttributeNode create(String key) {
107-
return GetAttributeNodeGen.create(key, null);
91+
return new GetAttributeNode(key, null);
10892
}
10993

94+
@Override
11095
public final StatementNode makeWriteNode(ExpressionNode rhs) {
11196
return SetAttributeNode.create(getFixedAttributeNode.key, getObject(), rhs);
11297
}
11398

114-
public abstract ExpressionNode getObject();
99+
public ExpressionNode getObject() {
100+
return objectExpression;
101+
}
115102

116103
abstract static class GetAttributeBaseNode extends Node {
117104

105+
@Child protected LookupAndCallBinaryNode dispatchNode = LookupAndCallBinaryNode.create(__GETATTRIBUTE__);
106+
@Child protected IsBuiltinClassProfile isBuiltinClassProfile = IsBuiltinClassProfile.create();
107+
118108
@Child private LookupSpecialMethodNode lookupGetattrNode;
119109
@Child private CallBinaryMethodNode callBinaryMethodNode;
120-
@Child private PythonObjectLibrary lib = PythonObjectLibrary.getFactory().createDispatched(1);
110+
@Child private PythonObjectLibrary lib;
121111

122112
@CompilationFinal private ConditionProfile hasGetattrProfile;
123113

124-
int dispatchGetAttrOrRethrowInt(VirtualFrame frame, Object object, Object key, PException pe) throws UnexpectedResultException {
125-
return ensureCallGetattrNode().executeInt(frame, lookupGetattrOrRethrow(frame, object, pe), object, key);
126-
}
127-
128-
long dispatchGetAttrOrRethrowLong(VirtualFrame frame, Object object, Object key, PException pe) throws UnexpectedResultException {
129-
return ensureCallGetattrNode().executeLong(frame, lookupGetattrOrRethrow(frame, object, pe), object, key);
130-
}
131-
132-
boolean dispatchGetAttrOrRethrowBool(VirtualFrame frame, Object object, Object key, PException pe) throws UnexpectedResultException {
133-
return ensureCallGetattrNode().executeBool(frame, lookupGetattrOrRethrow(frame, object, pe), object, key);
134-
}
135-
136114
Object dispatchGetAttrOrRethrowObject(VirtualFrame frame, Object object, Object key, PException pe) {
137115
return ensureCallGetattrNode().executeObject(frame, lookupGetattrOrRethrow(frame, object, pe), object, key);
138116
}
139117

140118
/** Lookup {@code __getattr__} or rethrow {@code pe} if it does not exist. */
141119
private Object lookupGetattrOrRethrow(VirtualFrame frame, Object object, PException pe) {
142-
Object getattrAttribute = ensureLookupGetattrNode().execute(frame, lib.getLazyPythonClass(object), object);
120+
Object getattrAttribute = ensureLookupGetattrNode().execute(frame, ensurePythonObjLib().getLazyPythonClass(object), object);
143121
if (ensureHasGetattrProfile().profile(getattrAttribute == PNone.NO_VALUE)) {
144122
throw pe;
145123
}
@@ -169,15 +147,19 @@ private ConditionProfile ensureHasGetattrProfile() {
169147
}
170148
return hasGetattrProfile;
171149
}
172-
}
173150

174-
public abstract static class GetFixedAttributeNode extends GetAttributeBaseNode {
151+
public PythonObjectLibrary ensurePythonObjLib() {
152+
if (lib == null) {
153+
CompilerDirectives.transferToInterpreterAndInvalidate();
154+
lib = insert(PythonObjectLibrary.getFactory().createDispatched(1));
155+
}
156+
return lib;
157+
}
158+
}
175159

160+
public static final class GetFixedAttributeNode extends GetAttributeBaseNode {
176161
private final String key;
177162

178-
@Child private LookupAndCallBinaryNode dispatchNode = LookupAndCallBinaryNode.create(__GETATTRIBUTE__);
179-
@Child private IsBuiltinClassProfile isBuiltinClassProfile = IsBuiltinClassProfile.create();
180-
181163
public GetFixedAttributeNode(String key) {
182164
this.key = key;
183165
}
@@ -186,46 +168,7 @@ public final String getKey() {
186168
return key;
187169
}
188170

189-
public abstract int executeInt(VirtualFrame frame, Object object) throws UnexpectedResultException;
190-
191-
public abstract long executeLong(VirtualFrame frame, Object object) throws UnexpectedResultException;
192-
193-
public abstract boolean executeBoolean(VirtualFrame frame, Object object) throws UnexpectedResultException;
194-
195-
public abstract Object executeObject(VirtualFrame frame, Object object);
196-
197-
@Specialization(rewriteOn = UnexpectedResultException.class)
198-
protected int doItInt(VirtualFrame frame, Object object) throws UnexpectedResultException {
199-
try {
200-
return dispatchNode.executeInt(frame, object, key);
201-
} catch (PException pe) {
202-
pe.expect(AttributeError, isBuiltinClassProfile);
203-
return dispatchGetAttrOrRethrowInt(frame, object, key, pe);
204-
}
205-
}
206-
207-
@Specialization(rewriteOn = UnexpectedResultException.class)
208-
protected long doItLong(VirtualFrame frame, Object object) throws UnexpectedResultException {
209-
try {
210-
return dispatchNode.executeLong(frame, object, key);
211-
} catch (PException pe) {
212-
pe.expect(AttributeError, isBuiltinClassProfile);
213-
return dispatchGetAttrOrRethrowLong(frame, object, key, pe);
214-
}
215-
}
216-
217-
@Specialization(rewriteOn = UnexpectedResultException.class)
218-
protected boolean doItBoolean(VirtualFrame frame, Object object) throws UnexpectedResultException {
219-
try {
220-
return dispatchNode.executeBool(frame, object, key);
221-
} catch (PException pe) {
222-
pe.expect(AttributeError, isBuiltinClassProfile);
223-
return dispatchGetAttrOrRethrowBool(frame, object, key, pe);
224-
}
225-
}
226-
227-
@Specialization(replaces = {"doItInt", "doItBoolean"})
228-
protected Object doIt(VirtualFrame frame, Object object) {
171+
public Object executeObject(VirtualFrame frame, Object object) {
229172
try {
230173
return dispatchNode.executeObject(frame, object, key);
231174
} catch (PException pe) {
@@ -235,55 +178,13 @@ protected Object doIt(VirtualFrame frame, Object object) {
235178
}
236179

237180
public static GetFixedAttributeNode create(String key) {
238-
return GetFixedAttributeNodeGen.create(key);
181+
return new GetFixedAttributeNode(key);
239182
}
240183
}
241184

242-
public abstract static class GetAnyAttributeNode extends GetAttributeBaseNode {
243-
244-
@Child private LookupAndCallBinaryNode dispatchNode = LookupAndCallBinaryNode.create(__GETATTRIBUTE__);
245-
@Child private IsBuiltinClassProfile isBuiltinClassProfile = IsBuiltinClassProfile.create();
246-
247-
public abstract int executeInt(VirtualFrame frame, Object object, Object key) throws UnexpectedResultException;
248-
249-
public abstract long executeLong(VirtualFrame frame, Object object, Object key) throws UnexpectedResultException;
250-
251-
public abstract boolean executeBoolean(VirtualFrame frame, Object object, Object key) throws UnexpectedResultException;
252-
253-
public abstract Object executeObject(VirtualFrame frame, Object object, Object key);
254-
255-
@Specialization(rewriteOn = UnexpectedResultException.class)
256-
protected int doItInt(VirtualFrame frame, Object object, String key) throws UnexpectedResultException {
257-
try {
258-
return dispatchNode.executeInt(frame, object, key);
259-
} catch (PException pe) {
260-
pe.expect(AttributeError, isBuiltinClassProfile);
261-
return dispatchGetAttrOrRethrowInt(frame, object, key, pe);
262-
}
263-
}
264-
265-
@Specialization(rewriteOn = UnexpectedResultException.class)
266-
protected long doItLong(VirtualFrame frame, Object object, Object key) throws UnexpectedResultException {
267-
try {
268-
return dispatchNode.executeLong(frame, object, key);
269-
} catch (PException pe) {
270-
pe.expect(AttributeError, isBuiltinClassProfile);
271-
return dispatchGetAttrOrRethrowLong(frame, object, key, pe);
272-
}
273-
}
274-
275-
@Specialization(rewriteOn = UnexpectedResultException.class)
276-
protected boolean doItBoolean(VirtualFrame frame, Object object, Object key) throws UnexpectedResultException {
277-
try {
278-
return dispatchNode.executeBool(frame, object, key);
279-
} catch (PException pe) {
280-
pe.expect(AttributeError, isBuiltinClassProfile);
281-
return dispatchGetAttrOrRethrowBool(frame, object, key, pe);
282-
}
283-
}
185+
public static final class GetAnyAttributeNode extends GetAttributeBaseNode {
284186

285-
@Specialization(replaces = {"doItInt", "doItBoolean"})
286-
protected Object doIt(VirtualFrame frame, Object object, Object key) {
187+
public Object executeObject(VirtualFrame frame, Object object, Object key) {
287188
try {
288189
return dispatchNode.executeObject(frame, object, key);
289190
} catch (PException pe) {
@@ -293,7 +194,7 @@ protected Object doIt(VirtualFrame frame, Object object, Object key) {
293194
}
294195

295196
public static GetAnyAttributeNode create() {
296-
return GetAnyAttributeNodeGen.create();
197+
return new GetAnyAttributeNode();
297198
}
298199
}
299200
}

0 commit comments

Comments
 (0)