Skip to content

Commit

Permalink
Use PyObject_TypeCheck in type_call instead of IsSubtype equality test
Browse files Browse the repository at this point in the history
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](python/cpython#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
  • Loading branch information
itamaro authored and Service User committed Nov 5, 2021
1 parent f9685ca commit 93080c5
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 93080c5

Please sign in to comment.