Skip to content

Commit 82ea460

Browse files
committed
Further improve 'PyType_IsSubtype'.
1 parent a7a9bce commit 82ea460

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

graalpython/com.oracle.graal.python.cext/src/capi.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,18 @@ inline void* native_to_java(PyObject* obj) {
183183
}
184184
}
185185

186+
__attribute__((always_inline))
187+
inline void* native_type_to_java(PyTypeObject* type) {
188+
PyObject* obj = (PyObject*)type;
189+
void* refcnt = obj->ob_refcnt;
190+
if (!truffle_cannot_be_handle(refcnt)) {
191+
return resolve_handle(cache, refcnt);
192+
} else if (IS_POINTER(refcnt)) {
193+
return refcnt;
194+
}
195+
return obj;
196+
}
197+
186198
extern void* to_java(PyObject* obj);
187199
extern void* to_java_type(PyTypeObject* cls);
188200
extern PyObject* to_sulong(void *o);

graalpython/com.oracle.graal.python.cext/src/typeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ PyTypeObject PySuper_Type = PY_TRUFFLE_TYPE("super", &PyType_Type, Py_TPFLAGS_DE
5454

5555
UPCALL_ID(PyType_IsSubtype);
5656
int PyType_IsSubtype(PyTypeObject* a, PyTypeObject* b) {
57-
return UPCALL_CEXT_I(_jls_PyType_IsSubtype, native_to_java((PyObject*)a), native_to_java((PyObject*)b));
57+
return ((int (*)(void* a, void* b))_jls_PyType_IsSubtype)(native_type_to_java(a), native_type_to_java(b));
5858
}
5959

6060
static int add_subclass(PyTypeObject *base, PyTypeObject *type) {

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,27 +2042,28 @@ long doGeneric(Object n) {
20422042

20432043
@Builtin(name = "PyType_IsSubtype", fixedNumOfPositionalArgs = 2)
20442044
@GenerateNodeFactory
2045-
abstract static class PyType_IsSubtype extends PythonBinaryBuiltinNode {
2046-
private static long calls = 0;
2045+
abstract static class PyType_IsSubtype extends PythonBuiltinNode {
20472046

20482047
@Child private IsSubtypeNode isSubtypeNode = IsSubtypeNode.create();
2048+
@Child private CExtNodes.AsPythonObjectNode asPythonObjectNode = CExtNodes.AsPythonObjectNode.create();
20492049

2050-
@Specialization
2051-
int doI(PythonClass a, PythonClass b) {
2052-
calls++;
2053-
if (calls % 1000 == 0) {
2054-
print(calls);
2055-
}
2056-
if (isSubtypeNode.execute(a, b)) {
2057-
return 1;
2058-
}
2059-
return 0;
2050+
@Specialization(guards = {"a == cachedA", "b == cachedB"})
2051+
int doCached(@SuppressWarnings("unused") PythonNativeWrapper a, @SuppressWarnings("unused") PythonNativeWrapper b,
2052+
@Cached("a") @SuppressWarnings("unused") PythonNativeWrapper cachedA,
2053+
@Cached("b") @SuppressWarnings("unused") PythonNativeWrapper cachedB,
2054+
@Cached("isNativeSubtype(a, b)") int result) {
2055+
return result;
20602056
}
20612057

2062-
@TruffleBoundary
2063-
private static void print(long calls2) {
2064-
System.out.println("######### CALLS: " + calls2);
2065-
System.out.flush();
2058+
@Specialization(replaces = "doCached")
2059+
int doGeneric(PythonNativeWrapper a, PythonNativeWrapper b) {
2060+
return isNativeSubtype(a, b);
2061+
}
2062+
2063+
protected int isNativeSubtype(PythonNativeWrapper a, PythonNativeWrapper b) {
2064+
assert a instanceof PythonClassNativeWrapper || a instanceof PythonClassInitNativeWrapper;
2065+
assert b instanceof PythonClassNativeWrapper || b instanceof PythonClassInitNativeWrapper;
2066+
return isSubtypeNode.execute(asPythonObjectNode.execute(a), asPythonObjectNode.execute(b)) ? 1 : 0;
20662067
}
20672068
}
20682069

0 commit comments

Comments
 (0)