gh-81057: Move Global Variables Holding Objects to _PyRuntimeState. (gh-99487)

This moves nearly all remaining object-holding globals in core code (other than static types).

https://github.com/python/cpython/issues/81057
This commit is contained in:
Eric Snow 2022-11-14 13:50:56 -07:00 committed by GitHub
parent 619cadcda6
commit a088290f9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 66 additions and 55 deletions

View file

@ -10,6 +10,7 @@ extern "C" {
#include "pycore_gc.h" // PyGC_Head
#include "pycore_global_strings.h" // struct _Py_global_strings
#include "pycore_typeobject.h" // pytype_slotdef
// These would be in pycore_long.h if it weren't for an include cycle.
@ -20,6 +21,13 @@ extern "C" {
// Only immutable objects should be considered runtime-global.
// All others must be per-interpreter.
#define _Py_CACHED_OBJECT(NAME) \
_PyRuntime.cached_objects.NAME
struct _Py_cached_objects {
PyObject *str_replace_inf;
};
#define _Py_GLOBAL_OBJECT(NAME) \
_PyRuntime.global_objects.NAME
#define _Py_SINGLETON(NAME) \
@ -54,6 +62,10 @@ struct _Py_global_objects {
struct _Py_interp_cached_objects {
int _not_set;
/* object.__reduce__ */
PyObject *objreduce;
PyObject *type_slots_pname;
pytype_slotdef *type_slots_ptrs[MAX_EQUIV];
};
#define _Py_INTERP_STATIC_OBJECT(interp, NAME) \

View file

@ -136,7 +136,15 @@ typedef struct pyruntimestate {
struct _Py_unicode_runtime_ids unicode_ids;
struct {
/* Used to set PyTypeObject.tp_version_tag */
// bpo-42745: next_version_tag remains shared by all interpreters
// because of static types.
unsigned int next_version_tag;
} types;
/* All the objects that are shared by the runtime's interpreters. */
struct _Py_cached_objects cached_objects;
struct _Py_global_objects global_objects;
/* The following fields are here to avoid allocation during init.

View file

@ -36,6 +36,9 @@ extern "C" {
until _PyInterpreterState_Enable() is called. */ \
.next_id = -1, \
}, \
.types = { \
.next_version_tag = 1, \
}, \
.global_objects = { \
.singletons = { \
.small_ints = _Py_small_ints_INIT, \

View file

@ -18,6 +18,15 @@ extern void _PyTypes_Fini(PyInterpreterState *);
/* other API */
/* Length of array of slotdef pointers used to store slots with the
same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
the same __name__, for any __name__. Since that's a static property, it is
appropriate to declare fixed-size arrays for this. */
#define MAX_EQUIV 10
typedef struct wrapperbase pytype_slotdef;
// Type attribute lookup cache: speed up attribute and method lookups,
// see _PyType_Lookup().
struct type_cache_entry {