gh-117398: Use Per-Interpreter State for the _datetime Static Types (… · python/cpython@105f22e

GitHub

@@ -44,10 +44,12 @@ struct type_cache {

44444545/* For now we hard-code this to a value for which we are confident

4646 all the static builtin types will fit (for all builds). */

47-#define_Py_MAX_STATIC_BUILTIN_TYPES 200

47+#define_Py_MAX_MANAGED_STATIC_BUILTIN_TYPES 200

48+#define_Py_MAX_MANAGED_STATIC_EXT_TYPES 10

48494950typedefstruct {

5051PyTypeObject*type;

52+intisbuiltin;

5153intreadying;

5254intready;

5355// XXX tp_dict can probably be statically allocated,

@@ -59,7 +61,7 @@ typedef struct {

5961 are also some diagnostic uses for the list of weakrefs,

6062 so we still keep it. */

6163PyObject*tp_weaklist;

62-} static_builtin_state;

64+} managed_static_type_state;

63656466structtypes_state {

6567/* Used to set PyTypeObject.tp_version_tag.

@@ -105,8 +107,16 @@ struct types_state {

105107 num_builtins_initialized is incremented once for each static

106108 builtin type. Once initialization is over for a subinterpreter,

107109 the value will be the same as for all other interpreters. */

108-size_tnum_builtins_initialized;

109-static_builtin_statebuiltins[_Py_MAX_STATIC_BUILTIN_TYPES];

110+struct {

111+size_tnum_initialized;

112+managed_static_type_stateinitialized[_Py_MAX_MANAGED_STATIC_BUILTIN_TYPES];

113+ } builtins;

114+/* We apply a similar strategy for managed extension modules. */

115+struct {

116+size_tnum_initialized;

117+size_tnext_index;

118+managed_static_type_stateinitialized[_Py_MAX_MANAGED_STATIC_EXT_TYPES];

119+ } for_extensions;

110120PyMutexmutex;

111121};

112122@@ -130,12 +140,35 @@ typedef struct wrapperbase pytype_slotdef;

130140131141132142staticinlinePyObject**

133-_PyStaticType_GET_WEAKREFS_LISTPTR(static_builtin_state*state)

143+_PyStaticType_GET_WEAKREFS_LISTPTR(managed_static_type_state*state)

134144{

135145assert(state!=NULL);

136146return&state->tp_weaklist;

137147}

138148149+externint_PyStaticType_InitBuiltin(

150+PyInterpreterState*interp,

151+PyTypeObject*type);

152+externvoid_PyStaticType_FiniBuiltin(

153+PyInterpreterState*interp,

154+PyTypeObject*type);

155+externvoid_PyStaticType_ClearWeakRefs(

156+PyInterpreterState*interp,

157+PyTypeObject*type);

158+externmanaged_static_type_state*_PyStaticType_GetState(

159+PyInterpreterState*interp,

160+PyTypeObject*type);

161+162+// Export for '_datetime' shared extension.

163+PyAPI_FUNC(int) _PyStaticType_InitForExtension(

164+PyInterpreterState*interp,

165+PyTypeObject*self);

166+PyAPI_FUNC(void) _PyStaticType_FiniForExtension(

167+PyInterpreterState*interp,

168+PyTypeObject*self,

169+intfinal);

170+171+139172/* Like PyType_GetModuleState, but skips verification

140173 * that type is a heap type with an associated module */

141174staticinlinevoid*

@@ -151,11 +184,6 @@ _PyType_GetModuleState(PyTypeObject *type)

151184}

152185153186154-externint_PyStaticType_InitBuiltin(PyInterpreterState*, PyTypeObject*type);

155-externstatic_builtin_state*_PyStaticType_GetState(PyInterpreterState*, PyTypeObject*);

156-externvoid_PyStaticType_ClearWeakRefs(PyInterpreterState*, PyTypeObject*type);

157-externvoid_PyStaticType_Dealloc(PyInterpreterState*, PyTypeObject*);

158-159187// Export for 'math' shared extension, used via _PyType_IsReady() static inline

160188// function

161189PyAPI_FUNC(PyObject*) _PyType_GetDict(PyTypeObject*);