From 51931ffed8c8ffba8b3c1802773f1be716022b92 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Thu, 12 Mar 2020 02:27:37 +0900 Subject: [PATCH 1/2] bpo-37207: Use PEP 590 vectorcall to speed up tuple() 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 --- .../2020-03-12-02-41-12.bpo-37207.ye7OM3.rst | 2 ++ Objects/tupleobject.c | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-03-12-02-41-12.bpo-37207.ye7OM3.rst 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 00000000000000..468f7f60c1f535 --- /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 52ecb5446fe8fc..14ab53fca22705 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -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) { + 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: From b70d8c08760aee03c97b09328a452b9a67525096 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Fri, 13 Mar 2020 18:03:15 +0900 Subject: [PATCH 2/2] bpo-37207: Update NEWS.d --- .../Core and Builtins/2020-03-12-02-41-12.bpo-37207.ye7OM3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 468f7f60c1f535..ecbadf95a8f92a 100644 --- 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 @@ -1,2 +1,2 @@ -Speed up calls to ``tuple()`` by using the PEP 590 ``vectorcall`` calling +Speed up calls to ``tuple()`` by using the :pep:`590` ``vectorcall`` calling convention. Patch by Dong-hee Na.