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.
This commit is contained in:
parent
f428521481
commit
a9ba1ab21b
|
@ -1156,27 +1156,43 @@ map_traverse(mapobject *lz, visitproc visit, void *arg)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
map_next(mapobject *lz)
|
map_next(mapobject *lz)
|
||||||
{
|
{
|
||||||
PyObject *val;
|
PyObject *small_stack[5];
|
||||||
PyObject *argtuple;
|
PyObject **stack;
|
||||||
PyObject *result;
|
Py_ssize_t niters, nargs, i;
|
||||||
Py_ssize_t numargs, i;
|
PyObject *result = NULL;
|
||||||
|
|
||||||
numargs = PyTuple_GET_SIZE(lz->iters);
|
niters = PyTuple_GET_SIZE(lz->iters);
|
||||||
argtuple = PyTuple_New(numargs);
|
if (niters <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
|
||||||
if (argtuple == NULL)
|
stack = small_stack;
|
||||||
return NULL;
|
}
|
||||||
|
else {
|
||||||
for (i=0 ; i<numargs ; i++) {
|
stack = PyMem_Malloc(niters * sizeof(PyObject*));
|
||||||
PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
|
if (stack == NULL) {
|
||||||
val = Py_TYPE(it)->tp_iternext(it);
|
PyErr_NoMemory();
|
||||||
if (val == NULL) {
|
|
||||||
Py_DECREF(argtuple);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
PyTuple_SET_ITEM(argtuple, i, val);
|
|
||||||
}
|
}
|
||||||
result = PyObject_Call(lz->func, argtuple, NULL);
|
|
||||||
Py_DECREF(argtuple);
|
nargs = 0;
|
||||||
|
for (i=0; i < niters; i++) {
|
||||||
|
PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
|
||||||
|
PyObject *val = Py_TYPE(it)->tp_iternext(it);
|
||||||
|
if (val == NULL) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue