From 7a5ac11a566e8f26c82b57805a84c3459d450748 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sat, 3 Apr 2021 02:21:06 +0900 Subject: [PATCH 1/3] bpo-43706: Use PEP 590 vectorcall to speed up enumerate() --- .../2021-04-03-02-44-15.bpo-43706.jjsXlT.rst | 2 + Objects/enumobject.c | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-04-03-02-44-15.bpo-43706.jjsXlT.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-04-03-02-44-15.bpo-43706.jjsXlT.rst b/Misc/NEWS.d/next/Core and Builtins/2021-04-03-02-44-15.bpo-43706.jjsXlT.rst new file mode 100644 index 00000000000000..ee859e37911cd0 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-04-03-02-44-15.bpo-43706.jjsXlT.rst @@ -0,0 +1,2 @@ +Speed up calls to ``enumerate()`` by using the :pep:`590` ``vectorcall`` +calling convention. Patch by Dong-hee Na. diff --git a/Objects/enumobject.c b/Objects/enumobject.c index 4513831545b9a3..10f939976b2ee9 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -81,6 +81,42 @@ enum_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start) return (PyObject *)en; } +// TODO: Use AC when bpo-43447 is supported +static PyObject * +enum_vectorcall(PyObject *type, PyObject * const*args, + size_t nargsf, PyObject *kwnames) +{ + assert(PyType_Check(type)); + PyTypeObject *tp = (PyTypeObject *)type; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + Py_ssize_t nkwargs = 0; + if (nargs == 0) { + PyErr_SetString(PyExc_TypeError, "enumerate() missing required argument 'iterable'"); + return NULL; + } + if (kwnames != NULL) { + nkwargs = PyTuple_GET_SIZE(kwnames); + } + + if (nargs + nkwargs == 2) { + if (nkwargs == 1) { + PyObject *kw = PyTuple_GET_ITEM(kwnames, 0); + if (!_PyUnicode_EqualToASCIIString(kw, "start")) { + PyErr_Format(PyExc_TypeError, "'%S' is an invalid keyword argument for enumerate()", kw); + return NULL; + } + } + return enum_new_impl(tp, args[0], args[1]); + } + + if (nargs == 1 && nkwargs == 0) { + return enum_new_impl(tp, args[0], NULL); + } + + PyErr_Format(PyExc_TypeError, "enumerate() takes at most 2 arguments (%d given)", nargs + nkwargs); + return NULL; +} + static void enum_dealloc(enumobject *en) { @@ -261,6 +297,7 @@ PyTypeObject PyEnum_Type = { PyType_GenericAlloc, /* tp_alloc */ enum_new, /* tp_new */ PyObject_GC_Del, /* tp_free */ + .tp_vectorcall = (vectorcallfunc)enum_vectorcall }; /* Reversed Object ***************************************************************/ From 93eaff81790fc63eb5b8d61dc925f009335e3fdc Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sat, 3 Apr 2021 08:21:01 +0900 Subject: [PATCH 2/3] bpo-43706: lint --- Objects/enumobject.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Objects/enumobject.c b/Objects/enumobject.c index 10f939976b2ee9..914372579e983d 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -83,7 +83,7 @@ enum_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start) // TODO: Use AC when bpo-43447 is supported static PyObject * -enum_vectorcall(PyObject *type, PyObject * const*args, +enum_vectorcall(PyObject *type, PyObject *const *args, size_t nargsf, PyObject *kwnames) { assert(PyType_Check(type)); @@ -91,7 +91,8 @@ enum_vectorcall(PyObject *type, PyObject * const*args, Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); Py_ssize_t nkwargs = 0; if (nargs == 0) { - PyErr_SetString(PyExc_TypeError, "enumerate() missing required argument 'iterable'"); + PyErr_SetString(PyExc_TypeError, + "enumerate() missing required argument 'iterable'"); return NULL; } if (kwnames != NULL) { @@ -102,7 +103,8 @@ enum_vectorcall(PyObject *type, PyObject * const*args, if (nkwargs == 1) { PyObject *kw = PyTuple_GET_ITEM(kwnames, 0); if (!_PyUnicode_EqualToASCIIString(kw, "start")) { - PyErr_Format(PyExc_TypeError, "'%S' is an invalid keyword argument for enumerate()", kw); + PyErr_Format(PyExc_TypeError, + "'%S' is an invalid keyword argument for enumerate()", kw); return NULL; } } @@ -113,7 +115,8 @@ enum_vectorcall(PyObject *type, PyObject * const*args, return enum_new_impl(tp, args[0], NULL); } - PyErr_Format(PyExc_TypeError, "enumerate() takes at most 2 arguments (%d given)", nargs + nkwargs); + PyErr_Format(PyExc_TypeError, + "enumerate() takes at most 2 arguments (%d given)", nargs + nkwargs); return NULL; } From 276e8011ef1052fda8acdfa6e7db07c17788448e Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Thu, 21 Oct 2021 22:39:15 +0900 Subject: [PATCH 3/3] bpo-43706: Address code review --- Objects/enumobject.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Objects/enumobject.c b/Objects/enumobject.c index 914372579e983d..b78230ddaebaaa 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -83,8 +83,8 @@ enum_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start) // TODO: Use AC when bpo-43447 is supported static PyObject * -enum_vectorcall(PyObject *type, PyObject *const *args, - size_t nargsf, PyObject *kwnames) +enumerate_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) { assert(PyType_Check(type)); PyTypeObject *tp = (PyTypeObject *)type; @@ -300,7 +300,7 @@ PyTypeObject PyEnum_Type = { PyType_GenericAlloc, /* tp_alloc */ enum_new, /* tp_new */ PyObject_GC_Del, /* tp_free */ - .tp_vectorcall = (vectorcallfunc)enum_vectorcall + .tp_vectorcall = (vectorcallfunc)enumerate_vectorcall }; /* Reversed Object ***************************************************************/