Skip to content

bpo-43706: Use PEP 590 vectorcall to speed up enumerate() #25154

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

Merged
merged 3 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Speed up calls to ``enumerate()`` by using the :pep:`590` ``vectorcall``
calling convention. Patch by Dong-hee Na.
40 changes: 40 additions & 0 deletions Objects/enumobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,45 @@ enum_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start)
return (PyObject *)en;
}

// TODO: Use AC when bpo-43447 is supported
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to track these tasks in the bpo rather than directly in the Python source code.

static PyObject *
enumerate_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)
{
Expand Down Expand Up @@ -261,6 +300,7 @@ PyTypeObject PyEnum_Type = {
PyType_GenericAlloc, /* tp_alloc */
enum_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
.tp_vectorcall = (vectorcallfunc)enumerate_vectorcall
};

/* Reversed Object ***************************************************************/
Expand Down