[Python-checkins] bpo-41922: Use PEP 590 vectorcall to speed up reversed() (GH-22523)

Dong-hee Na webhook-mailer at python.org
Sat Oct 3 13:17:03 EDT 2020


https://github.com/python/cpython/commit/d646e91f5c4f4b76f96494103d440ed0b6257425
commit: d646e91f5c4f4b76f96494103d440ed0b6257425
branch: master
author: Dong-hee Na <donghee.na92 at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-10-04T02:16:56+09:00
summary:

bpo-41922: Use PEP 590 vectorcall to speed up reversed() (GH-22523)

files:
A Misc/NEWS.d/next/Core and Builtins/2020-10-04-01-02-58.bpo-41922.kHGT8I.rst
M Objects/enumobject.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-10-04-01-02-58.bpo-41922.kHGT8I.rst b/Misc/NEWS.d/next/Core and Builtins/2020-10-04-01-02-58.bpo-41922.kHGT8I.rst
new file mode 100644
index 0000000000000..3c4de2c93555f
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-10-04-01-02-58.bpo-41922.kHGT8I.rst	
@@ -0,0 +1,2 @@
+Speed up calls to ``reversed()`` by using the :pep:`590` ``vectorcall``
+calling convention. Patch by Dong-hee Na.
diff --git a/Objects/enumobject.c b/Objects/enumobject.c
index 4a83bb45aa667..9d8449bb30f2a 100644
--- a/Objects/enumobject.c
+++ b/Objects/enumobject.c
@@ -314,6 +314,24 @@ reversed_new_impl(PyTypeObject *type, PyObject *seq)
     return (PyObject *)ro;
 }
 
+static PyObject *
+reversed_vectorcall(PyObject *type, PyObject * const*args,
+                size_t nargsf, PyObject *kwnames)
+{
+    assert(PyType_Check(type));
+
+    if (!_PyArg_NoKwnames("reversed", kwnames)) {
+        return NULL;
+    }
+
+    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
+    if (!_PyArg_CheckPositional("reversed", nargs, 1, 1)) {
+        return NULL;
+    }
+
+    return reversed_new_impl((PyTypeObject *)type, args[0]);
+}
+
 static void
 reversed_dealloc(reversedobject *ro)
 {
@@ -445,4 +463,5 @@ PyTypeObject PyReversed_Type = {
     PyType_GenericAlloc,            /* tp_alloc */
     reversed_new,                   /* tp_new */
     PyObject_GC_Del,                /* tp_free */
+    .tp_vectorcall = (vectorcallfunc)reversed_vectorcall,
 };



More information about the Python-checkins mailing list