8000 bpo-46323: Reduce stack usage of ctypes python callback function. by corona10 · Pull Request #31224 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-46323: Reduce stack usage 8000 of ctypes python callback function. #31224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:mod:`ctypes` now allocates memory on the stack instead of on the heap
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was no heap allocation directly before this PR, and there will be no heap allocation after, right? It's just eliminating that dead code path.

Copy link
Member Author
@corona10 corona10 Feb 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sweeneyde Yeah but this is the result of bpo-46323

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Victor and I decided to add NEWS entry for all this change at this moment.
Theoretically, we had to add NEWS entry at the first PR, but it's too late.

to pass arguments while calling a Python callback function.
Patch by Dong-hee Na.
35 changes: 13 additions & 22 deletions Modules/_ctypes/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@

#include <stdbool.h>

#ifdef MS_WIN32
# include <malloc.h>
#endif

#include <ffi.h>
#include "ctypes.h"

#ifdef HAVE_ALLOCA_H
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same workaroud

#include "ctypes.h"
#ifdef HAVE_ALLOCA_H
/* AIX needs alloca.h for alloca() */
#include <alloca.h>
#endif

/* AIX needs alloca.h for alloca() */
#include <alloca.h>
#endif

/**************************************************************/

static void
Expand Down Expand Up @@ -147,32 +156,17 @@ static void _CallPythonObject(void *mem,
void **pArgs)
{
PyObject *result = NULL;
PyObject **args = NULL;
Py_ssize_t i = 0, j = 0, nargs = 0;
PyObject *error_object = NULL;
int *space;
PyGILState_STATE state = PyGILState_Ensure();

assert(PyTuple_Check(converters));
nargs = PyTuple_GET_SIZE(converters);
/* Hm. What to return in case of error?
For COM, 0xFFFFFFFF seems better than 0.
*/
if (nargs < 0) {
PrintError("BUG: PySequence_Length");
goto Done;
}

PyObject *args_stack[CTYPES_MAX_ARGCOUNT];
if (nargs <= CTYPES_MAX_ARGCOUNT) {
args = args_stack;
}
else {
args = PyMem_Malloc(nargs * sizeof(PyObject *));
if (args == NULL) {
PyErr_NoMemory();
goto Done;
}
assert(nargs <= CTYPES_MAX_ARGCOUNT);
PyObject **args = NULL;
if (nargs > 0) {
args = alloca(nargs * sizeof(PyObject *));
}

PyObject **cnvs = PySequence_Fast_ITEMS(converters);
Expand Down Expand Up @@ -309,9 +303,6 @@ static void _CallPythonObject(void *mem,
for (j = 0; j < i; j++) {
Py_DECREF(args[j]);
}
if (args != args_stack) {
PyMem_Free(args);
}
PyGILState_Release(state);
}

Expand Down
6 changes: 1 addition & 5 deletions Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1160,11 +1160,7 @@ PyObject *_ctypes_callproc(PPROC pProc,
return NULL;
}

args = (struct argument *)alloca(sizeof(struct argument) * argcount);
if (!args) {
PyErr_NoMemory();
return NULL;
}
args = alloca(sizeof(struct argument) * argcount);
memset(args, 0, sizeof(struct argument) * argcount);
argtype_count = argtypes ? PyTuple_GET_SIZE(argtypes) : 0;
#ifdef MS_WIN32
Expand Down
0