-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
speed up isinstance and issubclass for the usual cases #66730
Comments
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. |
Addressing review comments. |
New changeset 4f33a4a2b425 by Georg Brandl in branch 'default': |
A buildbot is failing since your change. ====================================================================== Traceback (most recent call last):
File "/root/buildarea/3.x.angelico-debian-amd64/build/Lib/test/test_decimal.py", line 1711, in test_decimal_fraction_comparison
self.assertLess(D(0), F(1,9999999999999999999999999999999999999))
File "/root/buildarea/3.x.angelico-debian-amd64/build/Lib/unittest/case.py", line 1174, in assertLess
if not a < b:
TypeError: unorderable types: decimal.Decimal() < Fraction() ====================================================================== Traceback (most recent call last):
File "/root/buildarea/3.x.angelico-debian-amd64/build/Lib/test/test_decimal.py", line 2404, in test_abc
self.assertTrue(issubclass(Decimal, numbers.Number))
AssertionError: False is not true ====================================================================== Traceback (most recent call last):
File "/root/buildarea/3.x.angelico-debian-amd64/build/Lib/test/test_numeric_tower.py", line 173, in test_mixed_comparisons
self.assertLess(first, second)
File "/root/buildarea/3.x.angelico-debian-amd64/build/Lib/unittest/case.py", line 1174, in assertLess
if not a < b:
TypeError: unorderable types: decimal.Decimal() < Fraction() |
The test_decimal failure can be reproduced using: ./python -m test -W test_datetime test_decimal |
New changeset 6cfe929d1de5 by Antoine Pitrou in branch 'default': |
Thanks for fixing! I did run "make test" locally, but that did not produce the failure. |
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
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: