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

Master:

./python.exe -m pyperf timeit "tuple((1, 2, 3, 4, 5))"
Mean +- std dev: 361 ns +- 15 ns

PEP-590:

./python.exe -m pyperf timeit "tuple((1, 2, 3, 4, 5))"
Mean +- std dev: 203 ns +- 13 ns
This commit is contained in:
Dong-hee Na 2020-03-13 22:57:00 +09:00 committed by GitHub
parent 3f2f4fefca
commit 9ee88cde1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 0 deletions

View File

@ -0,0 +1,2 @@
Speed up calls to ``tuple()`` by using the :pep:`590` ``vectorcall`` calling
convention. Patch by Dong-hee Na.

View File

@ -705,6 +705,26 @@ tuple_new_impl(PyTypeObject *type, PyObject *iterable)
return PySequence_Tuple(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) {
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 * static PyObject *
tuple_subtype_new(PyTypeObject *type, PyObject *iterable) tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
{ {
@ -863,6 +883,7 @@ PyTypeObject PyTuple_Type = {
0, /* tp_alloc */ 0, /* tp_alloc */
tuple_new, /* tp_new */ tuple_new, /* tp_new */
PyObject_GC_Del, /* tp_free */ PyObject_GC_Del, /* tp_free */
.tp_vectorcall = tuple_vectorcall,
}; };
/* The following function breaks the notion that tuples are immutable: /* The following function breaks the notion that tuples are immutable: