Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
assignee=Noneclosed_at=<Date2014-10-04.07:07:00.198>created_at=<Date2014-10-02.11:39:12.578>labels= ['interpreter-core', 'performance']
title='speed up isinstance and issubclass for the usual cases'updated_at=<Date2014-10-04.07:07:00.198>user='https://github.com/birkenfeld'
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 bpo-12029.
Traceback (most recent call last):
File "/root/buildarea/3.x.angelico-debian-amd64/build/Lib/test/test_decimal.py", line 2404, in test_abcself.assertTrue(issubclass(Decimal, numbers.Number))
AssertionError: False is not true