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

Dong-hee Na webhook-mailer at python.org
Fri Mar 13 09:57:16 EDT 2020


https://github.com/python/cpython/commit/9ee88cde1abf7f274cc55a0571b1c2cdb1263743
commit: 9ee88cde1abf7f274cc55a0571b1c2cdb1263743
branch: master
author: Dong-hee Na <donghee.na92 at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020年03月13日T14:57:00+01:00
summary:
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
files:
A Misc/NEWS.d/next/Core and Builtins/2020-03-12-02-41-12.bpo-37207.ye7OM3.rst
M Objects/tupleobject.c
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 0000000000000..ecbadf95a8f92
--- /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 52ecb5446fe8f..14ab53fca2270 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:


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /