From 93080c58d03310382b0a51853030e081844202e0 Mon Sep 17 00:00:00 2001 From: Itamar Ostricher Date: Fri, 5 Nov 2021 15:52:46 -0700 Subject: [PATCH] Use PyObject_TypeCheck in type_call instead of IsSubtype equality test Summary: In D14351113, the equality check in IsSubtype was introduced as an optimization for the hot path of instance creation. When upstreaming it ([bpo-45697](https://bugs.python.org/issue45697)) it was pointed out that it's preferable to use the `PyObject_TypeCheck` in the hot callsite (which performs an inline equality check before calling to IsSubtype) instead of adding a test to all paths (including those that already use `PyObject_TypeCheck`, hence would hit the equality check twice). This diff ports the [merged upstream PR](https://github.com/python/cpython/pull/29392) to cinder. Test Plan: `make testcinder` passes and sandcastle Reviewers: mpage, carljm, #python_efficiency, #cinder Reviewed By: carljm Subscribers: orvid Differential Revision: https://phabricator.intern.facebook.com/D32175230 Tasks: T104755769 Signature: 32175230:1636152684:21f153f68470c360b16a4a567fa52cb49c375148 --- Objects/typeobject.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index d6e4199204e..7ff3804b2b7 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -1137,7 +1137,7 @@ type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) /* If the returned object is not an instance of type, it won't be initialized. */ - if (!PyType_IsSubtype(Py_TYPE(obj), type)) + if (!PyObject_TypeCheck(obj, type)) return obj; type = Py_TYPE(obj); @@ -1531,10 +1531,6 @@ PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) { PyObject *mro; - if (a == b) { - return 1; - } - mro = a->tp_mro; if (mro != NULL) { /* Deal with multiple inheritance without recursion