-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3754856
bpo-46323: Fix stack allocation to avoid stackoverflow
corona10 cbab14b
bpo-46323: Address code review
corona10 4aaf1e6
bpo-46323: Fix Windows ARM64
corona10 71a82b5
bpo-46323: Address code review
corona10 07c12ff
bpo-46323: Address code review
corona10 f4a276f
bpo-46323: Add NEWS entry
corona10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Core and Builtins/2022-02-10-02-29-12.bpo-46323.HK_cs0.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
to pass arguments while calling a Python callback function. | ||
Patch by Dong-hee Na. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -13,9 +13,18 @@ | |||||||||||
|
||||||||||||
#include <stdbool.h> | ||||||||||||
|
||||||||||||
#ifdef MS_WIN32 | ||||||||||||
# include <malloc.h> | ||||||||||||
#endif | ||||||||||||
|
||||||||||||
#include <ffi.h> | ||||||||||||
#include "ctypes.h" | ||||||||||||
|
||||||||||||
#ifdef HAVE_ALLOCA_H | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same workaroud cpython/Modules/_ctypes/callproc.c Lines 80 to 84 in 06e1701
|
||||||||||||
/* AIX needs alloca.h for alloca() */ | ||||||||||||
#include <alloca.h> | ||||||||||||
#endif | ||||||||||||
|
||||||||||||
/**************************************************************/ | ||||||||||||
|
||||||||||||
static void | ||||||||||||
|
@@ -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); | ||||||||||||
corona10 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
PyObject **args = NULL; | ||||||||||||
if (nargs > 0) { | ||||||||||||
args = alloca(nargs * sizeof(PyObject *)); | ||||||||||||
} | ||||||||||||
|
||||||||||||
PyObject **cnvs = PySequence_Fast_ITEMS(converters); | ||||||||||||
|
@@ -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); | ||||||||||||
} | ||||||||||||
|
||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.