Skip to content

Commit bc08ab4

Browse files
committed
Add _PY_FASTCALL_SMALL_STACK constant
Issue #28870: Add a new _PY_FASTCALL_SMALL_STACK constant, size of "small stacks" allocated on the C stack to pass positional arguments to _PyObject_FastCall(). _PyObject_Call_Prepend() now uses a small stack of 5 arguments (40 bytes) instead of 8 (64 bytes), since it is modified to use _PY_FASTCALL_SMALL_STACK.
1 parent d1e35dd commit bc08ab4

3 files changed

Lines changed: 15 additions & 4 deletions

File tree

Include/abstract.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,17 @@ PyAPI_FUNC(PyObject **) _PyStack_UnpackDict(
303303
PyObject **kwnames,
304304
PyObject *func);
305305

306+
/* Suggested size (number of positional arguments) for arrays of PyObject*
307+
allocated on a C stack to avoid allocating memory on the heap memory. Such
308+
array is used to pass positional arguments to call functions of the
309+
_PyObject_FastCall() family.
310+
311+
The size is chosen to not abuse the C stack and so limit the risk of stack
312+
overflow. The size is also chosen to allow using the small stack for most
313+
function calls of the Python standard library. On 64-bit CPU, it allocates
314+
40 bytes on the stack. */
315+
#define _PY_FASTCALL_SMALL_STACK 5
316+
306317
/* Call the callable object 'callable' with the "fast call" calling convention:
307318
args is a C array for positional arguments (nargs is the number of
308319
positional arguments), kwargs is a dictionary for keyword arguments.

Objects/abstract.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2337,7 +2337,7 @@ PyObject *
23372337
_PyObject_Call_Prepend(PyObject *callable,
23382338
PyObject *obj, PyObject *args, PyObject *kwargs)
23392339
{
2340-
PyObject *small_stack[8];
2340+
PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
23412341
PyObject **stack;
23422342
Py_ssize_t argcount;
23432343
PyObject *result;
@@ -2523,7 +2523,7 @@ static PyObject *
25232523
_PyObject_CallFunctionVa(PyObject *callable, const char *format,
25242524
va_list va, int is_size_t)
25252525
{
2526-
PyObject* small_stack[5];
2526+
PyObject* small_stack[_PY_FASTCALL_SMALL_STACK];
25272527
const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack);
25282528
PyObject **stack;
25292529
Py_ssize_t nargs, i;
@@ -2704,7 +2704,7 @@ _PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
27042704
PyObject *
27052705
_PyObject_VaCallFunctionObjArgs(PyObject *callable, va_list vargs)
27062706
{
2707-
PyObject *small_stack[5];
2707+
PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
27082708
PyObject **stack;
27092709
Py_ssize_t nargs;
27102710
PyObject *result;

Python/bltinmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ map_traverse(mapobject *lz, visitproc visit, void *arg)
11861186
static PyObject *
11871187
map_next(mapobject *lz)
11881188
{
1189-
PyObject *small_stack[5];
1189+
PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
11901190
PyObject **stack;
11911191
Py_ssize_t niters, nargs, i;
11921192
PyObject *result = NULL;

0 commit comments

Comments
 (0)