-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"); | ||
return NULL; | ||
} | ||
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); | ||
if (nargs > 1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with your all concerns :) |
||
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) | ||
{ | ||
|
@@ -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: | ||
|
There was a problem hiding this comment.
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.