[New-bugs-announce] [issue22540] speed up isinstance and issubclass for the usual cases

Georg Brandl report at bugs.python.org
Thu Oct 2 13:39:12 CEST 2014


New submission from Georg Brandl:

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.

----------
components: Interpreter Core
files: pyobject_issubclass_isinstance_speedup.patch
keywords: patch
messages: 228218
nosy: georg.brandl, ncoghlan, pitrou
priority: normal
severity: normal
stage: patch review
status: open
title: speed up isinstance and issubclass for the usual cases
type: performance
versions: Python 3.5
Added file: http://bugs.python.org/file36781/pyobject_issubclass_isinstance_speedup.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22540>
_______________________________________


More information about the New-bugs-announce mailing list