Author georg.brandl
Recipients georg.brandl, ncoghlan, pitrou
Date 2014-10-02.11:39:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
With the introduction of ABCs, PyObject_IsInstance (and for this issue, everything is also valid for PyObject_IsSubclass) has to check for a type's __instancecheck__ before falling back to the built-in behavior.

However, the "type" type has an __instancecheck__ method (that calls the built-in behavior too), probably for consistency on the Python side.

This means that the fast path is never taken, and every isinstance() call is slowed down unnecessarily.

This patch introduces a new fast path by checking for PyType_Type exactly before looking up the __instancecheck__.

It also introduces a check for exact match in PyObject_IsSubclass() analogous to one that is already present in PyObject_IsInstance().  Note that this will break __subclasscheck__ implementations that would deny issubclass(C, C).

This patch is not only useful for speeding up isinstance() and issubclass() calls, but also has other effects such as not slowing down the improvement of issue #12029.
Date User Action Args
2014-10-02 11:39:12georg.brandlsetrecipients: + georg.brandl, ncoghlan, pitrou
2014-10-02 11:39:12georg.brandlsetmessageid: <>
2014-10-02 11:39:12georg.brandllinkissue22540 messages
2014-10-02 11:39:12georg.brandlcreate