Skip to content

Commit 8e6fa8b

Browse files
committed
Avoid interface call on 'LazyPythonClass.getInstanceShape'.
1 parent 54873c3 commit 8e6fa8b

File tree

7 files changed

+69
-16
lines changed

7 files changed

+69
-16
lines changed

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
import com.oracle.truffle.api.frame.VirtualFrame;
179179
import com.oracle.truffle.api.nodes.UnexpectedResultException;
180180
import com.oracle.truffle.api.object.HiddenKey;
181+
import com.oracle.truffle.api.object.Shape;
181182
import com.oracle.truffle.api.profiles.BranchProfile;
182183
import com.oracle.truffle.api.profiles.ConditionProfile;
183184

@@ -1345,6 +1346,7 @@ public abstract static class ObjectNode extends PythonVarargsBuiltinNode {
13451346
@Child private PCallCapiFunction callNativeGenericNewNode;
13461347
@Children private CExtNodes.ToSulongNode[] toSulongNodes;
13471348
@Child private CExtNodes.AsPythonObjectNode asPythonObjectNode;
1349+
@Child private TypeNodes.GetInstanceShape getInstanceShapeNode;
13481350

13491351
@Override
13501352
public final Object varArgExecute(VirtualFrame frame, Object[] arguments, PKeyword[] keywords) throws VarargsBuiltinDirectInvocationNotSupported {
@@ -1353,10 +1355,9 @@ public final Object varArgExecute(VirtualFrame frame, Object[] arguments, PKeywo
13531355

13541356
@Specialization
13551357
Object doDirectConstruct(@SuppressWarnings("unused") PNone ignored, Object[] arguments, @SuppressWarnings("unused") PKeyword[] kwargs) {
1356-
if (PGuards.isNativeClass(arguments[0])) {
1357-
throw raise(PythonBuiltinClassType.SystemError, "cannot instantiate native class here");
1358-
}
1359-
return factory().createPythonObject((LazyPythonClass) arguments[0]);
1358+
assert arguments[0] != null;
1359+
LazyPythonClass first = (LazyPythonClass) first(arguments);
1360+
return factory().createPythonObject(first, getInstanceShape(first));
13601361
}
13611362

13621363
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()", guards = {"self == cachedSelf", "!self.needsNativeAllocation()"})
@@ -1370,7 +1371,7 @@ Object doObjectIndirect(PythonManagedClass self, Object[] varargs, PKeyword[] kw
13701371
if (varargs.length > 0 || kwargs.length > 0) {
13711372
// TODO: tfel: this should throw an error only if init isn't overridden
13721373
}
1373-
return factory().createPythonObject(self);
1374+
return factory().createPythonObject(self, getInstanceShape(self));
13741375
}
13751376

13761377
@Specialization(guards = "self.needsNativeAllocation()")
@@ -1422,6 +1423,22 @@ private Object callNativeGenericNewNode(PythonNativeClass self, Object[] varargs
14221423
return asPythonObjectNode.execute(
14231424
callNativeGenericNewNode.call(toSulongNodes[0].execute(self), toSulongNodes[1].execute(self), toSulongNodes[2].execute(targs), toSulongNodes[3].execute(dkwargs)));
14241425
}
1426+
1427+
private Shape getInstanceShape(LazyPythonClass clazz) {
1428+
if (getInstanceShapeNode == null) {
1429+
CompilerDirectives.transferToInterpreterAndInvalidate();
1430+
getInstanceShapeNode = insert(TypeNodes.GetInstanceShape.create());
1431+
}
1432+
return getInstanceShapeNode.execute(clazz);
1433+
}
1434+
1435+
protected static Object first(Object[] arguments) {
1436+
return arguments[0];
1437+
}
1438+
1439+
protected static Class<? extends LazyPythonClass> getJavaClass(Object arg) {
1440+
return ((LazyPythonClass) arg).getClass();
1441+
}
14251442
}
14261443

14271444
// range(stop)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
import com.oracle.graal.python.builtins.objects.type.PythonClass;
125125
import com.oracle.graal.python.builtins.objects.type.PythonManagedClass;
126126
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
127+
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetInstanceShape;
127128
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetTypeFlagsNode;
128129
import com.oracle.graal.python.nodes.PGuards;
129130
import com.oracle.graal.python.nodes.PNodeWithContext;
@@ -209,7 +210,7 @@ public void initialize(PythonCore core) {
209210
PythonClass errorHandlerClass = core.factory().createPythonClass(PythonBuiltinClassType.PythonClass, "CErrorHandler",
210211
new PythonAbstractClass[]{core.lookupType(PythonBuiltinClassType.PythonObject)});
211212
builtinConstants.put("CErrorHandler", errorHandlerClass);
212-
builtinConstants.put(ERROR_HANDLER, core.factory().createPythonObject(errorHandlerClass));
213+
builtinConstants.put(ERROR_HANDLER, core.factory().createPythonObject(errorHandlerClass, GetInstanceShape.doSlowPath(errorHandlerClass)));
213214
builtinConstants.put(NATIVE_NULL, new PythonNativeNull());
214215
}
215216

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
import com.oracle.graal.python.builtins.PythonBuiltins;
5252
import com.oracle.graal.python.builtins.objects.object.PythonObject;
5353
import com.oracle.graal.python.builtins.objects.str.PString;
54+
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
55+
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetInstanceShape;
5456
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
5557
import com.oracle.graal.python.runtime.PythonCore;
5658
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -137,7 +139,8 @@ public static String getUnicodeVersion() {
137139
public void initialize(PythonCore core) {
138140
super.initialize(core);
139141
builtinConstants.put("version", getUnicodeVersion());
140-
PythonObject ucd_3_2_0 = core.factory().createPythonObject(core.lookupType(PythonBuiltinClassType.PythonObject));
142+
PythonBuiltinClass objectType = core.lookupType(PythonBuiltinClassType.PythonObject);
143+
PythonObject ucd_3_2_0 = core.factory().createPythonObject(objectType, GetInstanceShape.doSlowPath(objectType));
141144
ucd_3_2_0.setAttribute("unidata_version", "3.2.0");
142145
builtinConstants.put("ucd_3_2_0", ucd_3_2_0); // TODO this is a fake object, just satisfy
143146
// pip installer import

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/PythonObject.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
3737
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
3838
import com.oracle.graal.python.builtins.objects.type.PythonManagedClass;
39+
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
3940
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
4041
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetNameNode;
4142
import com.oracle.truffle.api.Assumption;
@@ -59,7 +60,7 @@ public class PythonObject extends PythonAbstractObject {
5960
public PythonObject(LazyPythonClass pythonClass) {
6061
assert pythonClass != null : getClass().getSimpleName();
6162
this.pythonClass = pythonClass;
62-
storage = pythonClass.getInstanceShape().newInstance();
63+
storage = TypeNodes.GetInstanceShape.doSlowPath(pythonClass).newInstance();
6364
}
6465

6566
public PythonObject(LazyPythonClass pythonClass, Shape instanceShape) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/LazyPythonClass.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,5 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.type;
4242

43-
import com.oracle.truffle.api.object.Shape;
44-
4543
public interface LazyPythonClass {
46-
47-
Shape getInstanceShape();
4844
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TypeNodes.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
6565
import com.oracle.graal.python.builtins.objects.type.PythonManagedClass.FlagsContainer;
6666
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.GetBaseClassesNodeGen;
67+
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.GetInstanceShapeNodeGen;
6768
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.GetMroStorageNodeGen;
6869
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.GetNameNodeGen;
6970
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.GetSubclassesNodeGen;
@@ -94,6 +95,7 @@
9495
import com.oracle.truffle.api.interop.UnknownIdentifierException;
9596
import com.oracle.truffle.api.interop.UnsupportedMessageException;
9697
import com.oracle.truffle.api.nodes.Node;
98+
import com.oracle.truffle.api.object.Shape;
9799
import com.oracle.truffle.api.profiles.ConditionProfile;
98100
import com.oracle.truffle.api.profiles.ValueProfile;
99101

@@ -749,4 +751,37 @@ public static IsTypeNode create() {
749751
return IsTypeNodeGen.create();
750752
}
751753
}
754+
755+
public abstract static class GetInstanceShape extends PNodeWithContext {
756+
757+
public abstract Shape execute(LazyPythonClass clazz);
758+
759+
@Specialization
760+
Shape doBuiltinClassType(PythonBuiltinClassType clazz) {
761+
return clazz.getInstanceShape();
762+
}
763+
764+
@Specialization
765+
Shape doManagedClass(PythonManagedClass clazz) {
766+
return clazz.getInstanceShape();
767+
}
768+
769+
@Fallback
770+
Shape doError(@SuppressWarnings("unused") LazyPythonClass clazz) {
771+
throw raise(PythonBuiltinClassType.SystemError, "cannot get shape of native class");
772+
}
773+
774+
public static Shape doSlowPath(LazyPythonClass clazz) {
775+
if (clazz instanceof PythonBuiltinClassType) {
776+
return ((PythonBuiltinClassType) clazz).getInstanceShape();
777+
} else if (clazz instanceof PythonManagedClass) {
778+
return ((PythonManagedClass) clazz).getInstanceShape();
779+
}
780+
throw PythonLanguage.getCore().raise(PythonBuiltinClassType.SystemError, "cannot get shape of native class");
781+
}
782+
783+
public static GetInstanceShape create() {
784+
return GetInstanceShapeNodeGen.create();
785+
}
786+
}
752787
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,23 @@ private boolean reportAllocations() {
188188

189189
@CompilationFinal private Optional<Shape> cachedInstanceShape = Optional.empty();
190190

191-
public PythonObject createPythonObject(LazyPythonClass cls) {
191+
public PythonObject createPythonObject(LazyPythonClass cls, Shape instanceShape) {
192192
assert cls != null;
193193
Optional<Shape> cached = cachedInstanceShape;
194194
if (cached != null) {
195195
if (cached.isPresent()) {
196-
if (cached.get() == cls.getInstanceShape()) {
196+
if (cached.get() == instanceShape) {
197197
return trace(new PythonObject(cls, cached.get()));
198198
} else {
199199
CompilerDirectives.transferToInterpreterAndInvalidate();
200200
cachedInstanceShape = null;
201201
}
202202
} else {
203203
CompilerDirectives.transferToInterpreterAndInvalidate();
204-
cachedInstanceShape = Optional.of(cls.getInstanceShape());
204+
cachedInstanceShape = Optional.of(instanceShape);
205205
}
206206
}
207-
return trace(new PythonObject(cls, cls.getInstanceShape()));
207+
return trace(new PythonObject(cls, instanceShape));
208208
}
209209

210210
public PythonNativeObject createNativeObjectWrapper(TruffleObject obj) {

0 commit comments

Comments
 (0)