From e5f6207ba6cb510d9370519ba869296be01787be Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Sun, 2 Jun 2019 01:32:18 +0500 Subject: bpo-34303: Micro-optimizations in functools.reduce() (GH-8598) --- .../NEWS.d/next/Library/2018-08-03-09-47-20.bpo-34303.tOE2HP.rst | 2 ++ Modules/_functoolsmodule.c | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-08-03-09-47-20.bpo-34303.tOE2HP.rst diff --git a/Misc/NEWS.d/next/Library/2018-08-03-09-47-20.bpo-34303.tOE2HP.rst b/Misc/NEWS.d/next/Library/2018-08-03-09-47-20.bpo-34303.tOE2HP.rst new file mode 100644 index 0000000..94c1299 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-08-03-09-47-20.bpo-34303.tOE2HP.rst @@ -0,0 +1,2 @@ +Performance of :func:`functools.reduce` is slightly improved. Patch by +Sergey Fedoseev. diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c index aca5bad..a101363 100644 --- a/Modules/_functoolsmodule.c +++ b/Modules/_functoolsmodule.c @@ -626,10 +626,13 @@ functools_reduce(PyObject *self, PyObject *args) if (result == NULL) result = op2; else { - PyTuple_SetItem(args, 0, result); - PyTuple_SetItem(args, 1, op2); - if ((result = PyEval_CallObject(func, args)) == NULL) + /* Update the args tuple in-place */ + assert(args->ob_refcnt == 1); + Py_XSETREF(_PyTuple_ITEMS(args)[0], result); + Py_XSETREF(_PyTuple_ITEMS(args)[1], op2); + if ((result = PyObject_Call(func, args, NULL)) == NULL) { goto Fail; + } } } -- cgit v0.12