diff options
-rw-r--r-- | Misc/NEWS.d/next/Core and Builtins/2020-03-12-02-41-12.bpo-37207.ye7OM3.rst | 2 | ||||
-rw-r--r-- | Objects/tupleobject.c | 21 |
2 files changed, 23 insertions, 0 deletions
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-03-12-02-41-12.bpo-37207.ye7OM3.rst b/Misc/NEWS.d/next/Core and Builtins/2020-03-12-02-41-12.bpo-37207.ye7OM3.rst new file mode 100644 index 0000000..ecbadf9 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-03-12-02-41-12.bpo-37207.ye7OM3.rst @@ -0,0 +1,2 @@ +Speed up calls to ``tuple()`` by using the :pep:`590` ``vectorcall`` calling +convention. Patch by Dong-hee Na. diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 52ecb54..14ab53f 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -706,6 +706,26 @@ tuple_new_impl(PyTypeObject *type, PyObject *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 * tuple_subtype_new(PyTypeObject *type, PyObject *iterable) { PyObject *tmp, *newobj, *item; @@ -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: |