From: Dong-hee Na Date: Mon, 22 Mar 2021 10:01:14 +0000 (+0900) Subject: bpo-43575: Use PEP 590 vectorcall to speed up map() (GH-24955) X-Git-Tag: v3.10.0a7~114 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=86883d40e93acae980e52b90fddd7d042e439beb;p=thirdparty%2FPython%2Fcpython.git bpo-43575: Use PEP 590 vectorcall to speed up map() (GH-24955) --- diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-03-21-12-26-32.bpo-43575.pl-nSg.rst b/Misc/NEWS.d/next/Core and Builtins/2021-03-21-12-26-32.bpo-43575.pl-nSg.rst new file mode 100644 index 000000000000..102325830347 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-03-21-12-26-32.bpo-43575.pl-nSg.rst @@ -0,0 +1,2 @@ +Speed up calls to ``map()`` by using the :pep:`590` ``vectorcall`` calling +convention. Patch by Dong-hee Na. diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 4683103e0943..9f83c036d092 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1264,8 +1264,48 @@ map_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } lz->iters = iters; func = PyTuple_GET_ITEM(args, 0); - Py_INCREF(func); - lz->func = func; + lz->func = Py_NewRef(func); + + return (PyObject *)lz; +} + +static PyObject * +map_vectorcall(PyObject *type, PyObject * const*args, + size_t nargsf, PyObject *kwnames) +{ + PyTypeObject *tp = (PyTypeObject *)type; + if (tp == &PyMap_Type && !_PyArg_NoKwnames("map", kwnames)) { + return NULL; + } + + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + if (nargs < 2) { + PyErr_SetString(PyExc_TypeError, + "map() must have at least two arguments."); + return NULL; + } + + PyObject *iters = PyTuple_New(nargs-1); + if (iters == NULL) { + return NULL; + } + + for (int i=1; itp_alloc(tp, 0); + if (lz == NULL) { + Py_DECREF(iters); + return NULL; + } + lz->iters = iters; + lz->func = Py_NewRef(args[0]); return (PyObject *)lz; } @@ -1403,6 +1443,7 @@ PyTypeObject PyMap_Type = { PyType_GenericAlloc, /* tp_alloc */ map_new, /* tp_new */ PyObject_GC_Del, /* tp_free */ + .tp_vectorcall = (vectorcallfunc)map_vectorcall };