bpo-45953: Statically allocate the main interpreter (and initial thread state). (gh-29883)

Previously, the main interpreter was allocated on the heap during runtime initialization.  Here we instead embed it into _PyRuntimeState, which means it is statically allocated as part of the _PyRuntime global.  The same goes for the initial thread state (of each interpreter, including the main one).  Consequently there are fewer allocations during runtime/interpreter init, fewer possible failures, and better memory locality.

FYI, this also helps efforts to consolidate globals, which in turns helps work on subinterpreter isolation.

https://bugs.python.org/issue45953
This commit is contained in:
Eric Snow 2022-01-12 16:28:46 -07:00 committed by GitHub
parent 0bbf30e2b9
commit ed57b36c32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 115 additions and 34 deletions

View file

@ -2,6 +2,9 @@
# error "this header file must not be included directly"
#endif
#include <stdbool.h>
PyAPI_FUNC(int) _PyInterpreterState_RequiresIDRef(PyInterpreterState *);
PyAPI_FUNC(void) _PyInterpreterState_RequireIDRef(PyInterpreterState *, int);
@ -83,6 +86,9 @@ struct _ts {
after allocation. */
int _initialized;
/* Was this thread state statically allocated? */
bool _static;
int recursion_remaining;
int recursion_limit;
int recursion_headroom; /* Allow 50 more calls to handle any errors. */
@ -175,9 +181,11 @@ struct _ts {
PyObject **datastack_top;
PyObject **datastack_limit;
/* XXX signal handlers should also be here */
};
/* other API */
// Alias for backward compatibility with Python 3.8
#define _PyInterpreterState_Get PyInterpreterState_Get