Skip to content

bpo-37207: Use PEP 590 vectorcall to speed up tuple() #18936

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 2 commits into from
Mar 13, 2020
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 ``tuple()`` by using the :pep:`590` ``vectorcall`` calling
convention. Patch by Dong-hee Na.
21 changes: 21 additions & 0 deletions Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,26 @@ tuple_new_impl(PyTypeObject *type, PyObject *iterable)
return PySequence_Tuple(iterable);
}

static PyObject *
tuple_vectorcall(PyObject *type, PyObject * const*args,
size_t nargsf, PyObject *kwnames)
{
if (kwnames && PyTuple_GET_SIZE(kwnames) != 0) {
PyErr_Format(PyExc_TypeError, "tuple() takes no keyword arguments");
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to add a function similar to _PyArg_NoKeywords() but accept a kwnames tuple. But it should be done in a separated PR.

return NULL;
}
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
if (nargs > 1) {
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 be nice to have an helper function to raise the exception for use: function taking nargsf and calling PyVectorcall_NARGS() for us. Again, it should be done in a separated PR.

What does Argument Clinic use for that? Does it duplicate the code?

I'm also worried but the Python binary will contains tons of ""xxx takes no keyword arguments" duplicated strings, where only xxx changes. _PyArg_NoKeywords() builds a string from the function name and so doesn't have the issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree with your all concerns :)
I will submit the PR to deal with!

PyErr_Format(PyExc_TypeError, "tuple() expected at most 1 argument, got %zd", nargs);
return NULL;
}

if (nargs) {
return tuple_new_impl((PyTypeObject *)type, args[0]);
}
return PyTuple_New(0);
}

static PyObject *
tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
{
Expand Down Expand Up @@ -863,6 +883,7 @@ PyTypeObject PyTuple_Type = {
0, /* tp_alloc */
tuple_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
.tp_vectorcall = tuple_vectorcall,
};

/* The following function breaks the notion that tuples are immutable:
Expand Down