[Python-checkins] cpython: Closes #22540: speed up PyObject_IsInstance and PyObject_IsSubclass in the

georg.brandl python-checkins at python.org
Fri Oct 3 09:32:48 CEST 2014


https://hg.python.org/cpython/rev/4f33a4a2b425
changeset: 92766:4f33a4a2b425
user: Georg Brandl <georg at python.org>
date: Fri Oct 03 09:26:37 2014 +0200
summary:
 Closes #22540: speed up PyObject_IsInstance and PyObject_IsSubclass in the common case that the second argument has metaclass "type".
files:
 Misc/NEWS | 3 +++
 Objects/abstract.c | 15 +++++++++++++++
 2 files changed, 18 insertions(+), 0 deletions(-)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #22540: speed up `PyObject_IsInstance` and `PyObject_IsSubclass` in the
+ common case that the second argument has metaclass `type`.
+
 - Issue #18711: Add a new `PyErr_FormatV` function, similar to `PyErr_Format`
 but accepting a `va_list` argument.
 
diff --git a/Objects/abstract.c b/Objects/abstract.c
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -2538,6 +2538,11 @@
 if (Py_TYPE(inst) == (PyTypeObject *)cls)
 return 1;
 
+ /* We know what type's __instancecheck__ does. */
+ if (PyType_CheckExact(cls)) {
+ return recursive_isinstance(inst, cls);
+ }
+
 if (PyTuple_Check(cls)) {
 Py_ssize_t i;
 Py_ssize_t n;
@@ -2576,6 +2581,7 @@
 }
 else if (PyErr_Occurred())
 return -1;
+ /* Probably never reached anymore. */
 return recursive_isinstance(inst, cls);
 }
 
@@ -2603,6 +2609,14 @@
 _Py_IDENTIFIER(__subclasscheck__);
 PyObject *checker;
 
+ /* We know what type's __subclasscheck__ does. */
+ if (PyType_CheckExact(cls)) {
+ /* Quick test for an exact match */
+ if (derived == cls)
+ return 1;
+ return recursive_issubclass(derived, cls);
+ }
+
 if (PyTuple_Check(cls)) {
 Py_ssize_t i;
 Py_ssize_t n;
@@ -2641,6 +2655,7 @@
 }
 else if (PyErr_Occurred())
 return -1;
+ /* Probably never reached anymore. */
 return recursive_issubclass(derived, cls);
 }
 
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /