Skip to content

Commit bc357a0

Browse files
committed
avoid non-leaf typecheck in GetClassNode for PythonObject for small polymorphism
1 parent 635af15 commit bc357a0

File tree

1 file changed

+22
-6
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/object

1 file changed

+22
-6
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/object/GetClassNode.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,6 @@ protected PythonClass getIt(@SuppressWarnings("unused") GetSetDescriptor object)
8585
return getCore().lookupType(PythonBuiltinClassType.GetSetDescriptor);
8686
}
8787

88-
@Specialization(guards = "!isNone(object)")
89-
protected PythonClass getIt(PythonObject object,
90-
@Cached("createIdentityProfile()") ValueProfile profile) {
91-
return profile.profile(object.getPythonClass());
92-
}
93-
9488
@Specialization(assumptions = "singleContextAssumption()")
9589
protected PythonClass getIt(@SuppressWarnings("unused") PNone object,
9690
@Cached("getIt(object)") PythonClass klass) {
@@ -197,6 +191,28 @@ protected PythonClass getIt(PythonNativeObject object,
197191
return getNativeClassNode.execute(object);
198192
}
199193

194+
@SuppressWarnings("unchecked")
195+
protected Class<? extends PythonObject> asPythonObjectSubclass(Class<? extends Object> clazz) {
196+
Class<? extends PythonObject> retval = null;
197+
if (PythonObject.class.isAssignableFrom(clazz)) {
198+
retval = (Class<? extends PythonObject>) clazz;
199+
}
200+
return retval;
201+
}
202+
203+
@Specialization(guards = {"object.getClass() == cachedClass"}, limit = "5")
204+
protected PythonClass getPythonClassCached(Object object,
205+
@Cached("asPythonObjectSubclass(object.getClass())") Class<? extends PythonObject> cachedClass,
206+
@Cached("createIdentityProfile()") ValueProfile profile) {
207+
return profile.profile(cachedClass.cast(object).getPythonClass());
208+
}
209+
210+
@Specialization(replaces = "getPythonClassCached")
211+
protected PythonClass getPythonClassGeneric(PythonObject object,
212+
@Cached("createIdentityProfile()") ValueProfile profile) {
213+
return profile.profile(object.getPythonClass());
214+
}
215+
200216
@Specialization(guards = "isForeignObject(object)", assumptions = "singleContextAssumption()")
201217
protected PythonClass getIt(@SuppressWarnings("unused") TruffleObject object,
202218
@Cached("getIt(object)") PythonClass klass) {

0 commit comments

Comments
 (0)