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 <rep...@bugs.python.org>
<http://bugs.python.org/issue22540>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to