From a9ba1ab21b44abea9775b076573f9c69c9e7153a Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 23 Aug 2016 17:56:06 +0200 Subject: Issue #27809: map_next() uses fast call Use a small stack allocated in the C stack for up to 5 iterator functions, otherwise allocates a stack on the heap memory. --- Python/bltinmodule.c | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 00a85b5..a77dfe8 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1156,27 +1156,43 @@ map_traverse(mapobject *lz, visitproc visit, void *arg) static PyObject * map_next(mapobject *lz) { - PyObject *val; - PyObject *argtuple; - PyObject *result; - Py_ssize_t numargs, i; + PyObject *small_stack[5]; + PyObject **stack; + Py_ssize_t niters, nargs, i; + PyObject *result = NULL; - numargs = PyTuple_GET_SIZE(lz->iters); - argtuple = PyTuple_New(numargs); - if (argtuple == NULL) - return NULL; + niters = PyTuple_GET_SIZE(lz->iters); + if (niters <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) { + stack = small_stack; + } + else { + stack = PyMem_Malloc(niters * sizeof(PyObject*)); + if (stack == NULL) { + PyErr_NoMemory(); + return NULL; + } + } - for (i=0 ; iiters, i); - val = Py_TYPE(it)->tp_iternext(it); + PyObject *val = Py_TYPE(it)->tp_iternext(it); if (val == NULL) { - Py_DECREF(argtuple); - return NULL; + goto exit; } - PyTuple_SET_ITEM(argtuple, i, val); + stack[i] = val; + nargs++; + } + + result = _PyObject_FastCall(lz->func, stack, nargs); + +exit: + for (i=0; i < nargs; i++) { + Py_DECREF(stack[i]); + } + if (stack != small_stack) { + PyMem_Free(stack); } - result = PyObject_Call(lz->func, argtuple, NULL); - Py_DECREF(argtuple); return result; } -- cgit v0.12