]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-111968: Refactor _PyXXX_Fini to integrate with _PyObject_ClearFreeLists (gh-114899)
authorDonghee Na <donghee.na@python.org>
Sat, 10 Feb 2024 00:57:04 +0000 (09:57 +0900)
committerGitHub <noreply@github.com>
Sat, 10 Feb 2024 00:57:04 +0000 (00:57 +0000)
20 files changed:
Include/internal/pycore_context.h
Include/internal/pycore_floatobject.h
Include/internal/pycore_freelist.h
Include/internal/pycore_gc.h
Include/internal/pycore_genobject.h
Include/internal/pycore_list.h
Include/internal/pycore_object_stack.h
Include/internal/pycore_sliceobject.h
Include/internal/pycore_tuple.h
Objects/floatobject.c
Objects/genobject.c
Objects/listobject.c
Objects/object.c
Objects/sliceobject.c
Objects/tupleobject.c
Python/context.c
Python/gc_free_threading.c
Python/gc_gil.c
Python/pylifecycle.c
Python/pystate.c

index 3284efba2b6f4cb8cd720037e5e729412a90b9d3..ae5c47f195eb7f0729138f136e4d98a8557f5e35 100644 (file)
@@ -14,7 +14,6 @@ extern PyTypeObject _PyContextTokenMissing_Type;
 /* runtime lifecycle */
 
 PyStatus _PyContext_Init(PyInterpreterState *);
-void _PyContext_Fini(_PyFreeListState *);
 
 
 /* other API */
index 038578e1f9680a65089d76af5c4613476d3561ea..3767df5506d43fbdb4346222a4c17b3f2fc51f6f 100644 (file)
@@ -15,7 +15,6 @@ extern "C" {
 
 extern void _PyFloat_InitState(PyInterpreterState *);
 extern PyStatus _PyFloat_InitTypes(PyInterpreterState *);
-extern void _PyFloat_Fini(_PyFreeListState *);
 extern void _PyFloat_FiniType(PyInterpreterState *);
 
 
index 82a42300991eccda0e478a8310e69019793262c4..1bc551914794f0c524bc4dd8cf0aa75d337c1369 100644 (file)
@@ -125,6 +125,16 @@ typedef struct _Py_freelist_state {
     struct _Py_object_stack_state object_stacks;
 } _PyFreeListState;
 
+extern void _PyObject_ClearFreeLists(_PyFreeListState *state, int is_finalization);
+extern void _PyTuple_ClearFreeList(_PyFreeListState *state, int is_finalization);
+extern void _PyFloat_ClearFreeList(_PyFreeListState *state, int is_finalization);
+extern void _PyList_ClearFreeList(_PyFreeListState *state, int is_finalization);
+extern void _PySlice_ClearFreeList(_PyFreeListState *state, int is_finalization);
+extern void _PyDict_ClearFreeList(_PyFreeListState *state, int is_finalization);
+extern void _PyAsyncGen_ClearFreeLists(_PyFreeListState *state, int is_finalization);
+extern void _PyContext_ClearFreeList(_PyFreeListState *state, int is_finalization);
+extern void _PyObjectStackChunk_ClearFreeList(_PyFreeListState *state, int is_finalization);
+
 #ifdef __cplusplus
 }
 #endif
index 8d0bc2a218e48de64626cec717701b63ae4bec68..582a16bf5218cef3be3b6c29af898b578ac8a4d7 100644 (file)
@@ -279,14 +279,6 @@ extern PyObject *_PyGC_GetReferrers(PyInterpreterState *interp, PyObject *objs);
 
 // Functions to clear types free lists
 extern void _PyGC_ClearAllFreeLists(PyInterpreterState *interp);
-extern void _Py_ClearFreeLists(_PyFreeListState *state, int is_finalization);
-extern void _PyTuple_ClearFreeList(_PyFreeListState *state, int is_finalization);
-extern void _PyFloat_ClearFreeList(_PyFreeListState *state, int is_finalization);
-extern void _PyList_ClearFreeList(_PyFreeListState *state, int is_finalization);
-extern void _PySlice_ClearCache(_PyFreeListState *state);
-extern void _PyDict_ClearFreeList(_PyFreeListState *state, int is_finalization);
-extern void _PyAsyncGen_ClearFreeLists(_PyFreeListState *state, int is_finalization);
-extern void _PyContext_ClearFreeList(_PyFreeListState *state, int is_finalization);
 extern void _Py_ScheduleGC(PyInterpreterState *interp);
 extern void _Py_RunGC(PyThreadState *tstate);
 
index 5ad63658051e86d1eb8712a08988bcbca28528e7..b2aa017598409f7c857184d2b8ef7af7cb41de65 100644 (file)
@@ -26,10 +26,6 @@ extern PyTypeObject _PyCoroWrapper_Type;
 extern PyTypeObject _PyAsyncGenWrappedValue_Type;
 extern PyTypeObject _PyAsyncGenAThrow_Type;
 
-/* runtime lifecycle */
-
-extern void _PyAsyncGen_Fini(_PyFreeListState *);
-
 #ifdef __cplusplus
 }
 #endif
index 4536f90e414493975d0a4a2dfa86e0556255d051..50dc13c4da4487caa41e5550187824cd2a2161d5 100644 (file)
@@ -13,12 +13,6 @@ extern "C" {
 extern PyObject* _PyList_Extend(PyListObject *, PyObject *);
 extern void _PyList_DebugMallocStats(FILE *out);
 
-
-/* runtime lifecycle */
-
-extern void _PyList_Fini(_PyFreeListState *);
-
-
 #define _PyList_ITEMS(op) _Py_RVALUE(_PyList_CAST(op)->ob_item)
 
 extern int
index d042be2a98090a0e2194f6c97d00520c7d941564..fc130b1e9920b41da33f77b5ee4457afa70bccc3 100644 (file)
@@ -34,9 +34,6 @@ _PyObjectStackChunk_New(void);
 extern void
 _PyObjectStackChunk_Free(_PyObjectStackChunk *);
 
-extern void
-_PyObjectStackChunk_ClearFreeList(_PyFreeListState *state, int is_finalization);
-
 // Push an item onto the stack. Return -1 on allocation failure, 0 on success.
 static inline int
 _PyObjectStack_Push(_PyObjectStack *stack, PyObject *obj)
index 0c72d3ee6225c51d488bd65687f4c5308861838d..89086f67683a2f5ca1da8d099218904221b3a6b6 100644 (file)
@@ -11,8 +11,6 @@ extern "C" {
 
 /* runtime lifecycle */
 
-extern void _PySlice_Fini(_PyFreeListState *);
-
 extern PyObject *
 _PyBuildSlice_ConsumeRefs(PyObject *start, PyObject *stop);
 
index b348339a505b0f73817cfb705afbf94513a56cf5..4605f355ccbc38ffb8c28ea1fb48c387500725f3 100644 (file)
@@ -14,7 +14,6 @@ extern void _PyTuple_DebugMallocStats(FILE *out);
 /* runtime lifecycle */
 
 extern PyStatus _PyTuple_InitGlobalObjects(PyInterpreterState *);
-extern void _PyTuple_Fini(_PyFreeListState *);
 
 
 /* other API */
index c440e0dab0e79faae0361d507ce62d4bfd175a91..9b322c52d4daea656f36477b181e2b5007df1e48 100644 (file)
@@ -2010,16 +2010,6 @@ _PyFloat_ClearFreeList(_PyFreeListState *freelist_state, int is_finalization)
 #endif
 }
 
-void
-_PyFloat_Fini(_PyFreeListState *state)
-{
-    // With Py_GIL_DISABLED:
-    // the freelists for the current thread state have already been cleared.
-#ifndef Py_GIL_DISABLED
-    _PyFloat_ClearFreeList(state, 1);
-#endif
-}
-
 void
 _PyFloat_FiniType(PyInterpreterState *interp)
 {
index ab523e46cceaa317a75702bd0fec48b0748ab12d..59ab7abf6180bd1d7f6ba25c192e7ae44502eda1 100644 (file)
@@ -1682,17 +1682,6 @@ _PyAsyncGen_ClearFreeLists(_PyFreeListState *freelist_state, int is_finalization
 #endif
 }
 
-void
-_PyAsyncGen_Fini(_PyFreeListState *state)
-{
-    // With Py_GIL_DISABLED:
-    // the freelists for the current thread state have already been cleared.
-#ifndef Py_GIL_DISABLED
-    _PyAsyncGen_ClearFreeLists(state, 1);
-#endif
-}
-
-
 static PyObject *
 async_gen_unwrap_value(PyAsyncGenObject *gen, PyObject *result)
 {
index 307b8f1bd76cac853d5803c5b432ed74261ef197..7fdb91eab890b5f7f07f9b1d9901e62377ef4af8 100644 (file)
@@ -135,16 +135,6 @@ _PyList_ClearFreeList(_PyFreeListState *freelist_state, int is_finalization)
 #endif
 }
 
-void
-_PyList_Fini(_PyFreeListState *state)
-{
-    // With Py_GIL_DISABLED:
-    // the freelists for the current thread state have already been cleared.
-#ifndef Py_GIL_DISABLED
-    _PyList_ClearFreeList(state, 1);
-#endif
-}
-
 /* Print summary info about the state of the optimized allocator */
 void
 _PyList_DebugMallocStats(FILE *out)
index 61e6131c6e99bbb97afbe6777808f40080291aed..275aa6713c8c217f4ec056b7e5e11716d0ffd144 100644 (file)
@@ -793,6 +793,21 @@ PyObject_Bytes(PyObject *v)
     return PyBytes_FromObject(v);
 }
 
+void
+_PyObject_ClearFreeLists(_PyFreeListState *state, int is_finalization)
+{
+    // In the free-threaded build, freelists are per-PyThreadState and cleared in PyThreadState_Clear()
+    // In the default build, freelists are per-interpreter and cleared in finalize_interp_types()
+    _PyFloat_ClearFreeList(state, is_finalization);
+    _PyTuple_ClearFreeList(state, is_finalization);
+    _PyList_ClearFreeList(state, is_finalization);
+    _PyDict_ClearFreeList(state, is_finalization);
+    _PyContext_ClearFreeList(state, is_finalization);
+    _PyAsyncGen_ClearFreeLists(state, is_finalization);
+    // Only be cleared if is_finalization is true.
+    _PyObjectStackChunk_ClearFreeList(state, is_finalization);
+    _PySlice_ClearFreeList(state, is_finalization);
+}
 
 /*
 def _PyObject_FunctionStr(x):
index 8b9d6bbfd858b75964931abaef090445a0616acd..9880c123c80f95e1e91dfdd3f17eba5e358d5ca6 100644 (file)
@@ -103,8 +103,11 @@ PyObject _Py_EllipsisObject = _PyObject_HEAD_INIT(&PyEllipsis_Type);
 
 /* Slice object implementation */
 
-void _PySlice_ClearCache(_PyFreeListState *state)
+void _PySlice_ClearFreeList(_PyFreeListState *state, int is_finalization)
 {
+    if (!is_finalization) {
+        return;
+    }
 #ifdef WITH_FREELISTS
     PySliceObject *obj = state->slices.slice_cache;
     if (obj != NULL) {
@@ -114,13 +117,6 @@ void _PySlice_ClearCache(_PyFreeListState *state)
 #endif
 }
 
-void _PySlice_Fini(_PyFreeListState *state)
-{
-#ifdef WITH_FREELISTS
-    _PySlice_ClearCache(state);
-#endif
-}
-
 /* start, stop, and step are python objects with None indicating no
    index is present.
 */
index b9bf6cd48f612927b847128dbd6b74d70f47519c..7d73c3fb0f7f2ccc7f5d7d7711ab31300f4266dd 100644 (file)
@@ -964,11 +964,6 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
 
 static void maybe_freelist_clear(_PyFreeListState *, int);
 
-void
-_PyTuple_Fini(_PyFreeListState *state)
-{
-    maybe_freelist_clear(state, 1);
-}
 
 void
 _PyTuple_ClearFreeList(_PyFreeListState *state, int is_finalization)
index 793dfa2b72c7e3f2dcb5b938778dfdef9317c20e..e44fef705c36e0e9e3f901eab435ce0e028ff77b 100644 (file)
@@ -1284,17 +1284,6 @@ _PyContext_ClearFreeList(_PyFreeListState *freelist_state, int is_finalization)
 }
 
 
-void
-_PyContext_Fini(_PyFreeListState *state)
-{
-    // With Py_GIL_DISABLED:
-    // the freelists for the current thread state have already been cleared.
-#ifndef Py_GIL_DISABLED
-    _PyContext_ClearFreeList(state, 1);
-#endif
-}
-
-
 PyStatus
 _PyContext_Init(PyInterpreterState *interp)
 {
index 5d3b097dee93e871ac7da2948f8f3b7b8549a911..93e1168002b6f7d3e3631b89796bdb352e2495e8 100644 (file)
@@ -1721,7 +1721,7 @@ _PyGC_ClearAllFreeLists(PyInterpreterState *interp)
     HEAD_LOCK(&_PyRuntime);
     _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)interp->threads.head;
     while (tstate != NULL) {
-        _Py_ClearFreeLists(&tstate->freelist_state, 0);
+        _PyObject_ClearFreeLists(&tstate->freelist_state, 0);
         tstate = (_PyThreadStateImpl *)tstate->base.next;
     }
     HEAD_UNLOCK(&_PyRuntime);
index 4e2aa8f7af746c2018a68f5c2c667648db463d3c..5f1365f509deb0aac1a7308270c2ade75a9c5fd4 100644 (file)
@@ -11,7 +11,7 @@
 void
 _PyGC_ClearAllFreeLists(PyInterpreterState *interp)
 {
-    _Py_ClearFreeLists(&interp->freelist_state, 0);
+    _PyObject_ClearFreeLists(&interp->freelist_state, 0);
 }
 
 #endif
index 0cac7109340129c177c31f3f383fbc5ba2f6ae0b..61c9d4f9ea95754033a6c3c0903aba011c1623c9 100644 (file)
@@ -1790,16 +1790,14 @@ finalize_interp_types(PyInterpreterState *interp)
     // a dict internally.
     _PyUnicode_ClearInterned(interp);
 
-    _PyDict_Fini(interp);
     _PyUnicode_Fini(interp);
 
+#ifndef Py_GIL_DISABLED
+    // With Py_GIL_DISABLED:
+    // the freelists for the current thread state have already been cleared.
     _PyFreeListState *state = _PyFreeListState_GET();
-    _PyTuple_Fini(state);
-    _PyList_Fini(state);
-    _PyFloat_Fini(state);
-    _PySlice_Fini(state);
-    _PyContext_Fini(state);
-    _PyAsyncGen_Fini(state);
+    _PyObject_ClearFreeLists(state, 1);
+#endif
 
 #ifdef Py_DEBUG
     _PyStaticObjects_CheckRefcnt(interp);
index 6cd034743ddf4c325e7660bd1bedc56019e8ec69..937c43033b068d4b8842be638354d70316a2fcff 100644 (file)
@@ -1468,20 +1468,6 @@ clear_datastack(PyThreadState *tstate)
     }
 }
 
-void
-_Py_ClearFreeLists(_PyFreeListState *state, int is_finalization)
-{
-    // In the free-threaded build, freelists are per-PyThreadState and cleared in PyThreadState_Clear()
-    // In the default build, freelists are per-interpreter and cleared in finalize_interp_types()
-    _PyFloat_ClearFreeList(state, is_finalization);
-    _PyTuple_ClearFreeList(state, is_finalization);
-    _PyList_ClearFreeList(state, is_finalization);
-    _PyDict_ClearFreeList(state, is_finalization);
-    _PyContext_ClearFreeList(state, is_finalization);
-    _PyAsyncGen_ClearFreeLists(state, is_finalization);
-    _PyObjectStackChunk_ClearFreeList(state, is_finalization);
-}
-
 void
 PyThreadState_Clear(PyThreadState *tstate)
 {
@@ -1566,9 +1552,8 @@ PyThreadState_Clear(PyThreadState *tstate)
     }
 #ifdef Py_GIL_DISABLED
     // Each thread should clear own freelists in free-threading builds.
-    _PyFreeListState *freelist_state = &((_PyThreadStateImpl*)tstate)->freelist_state;
-    _Py_ClearFreeLists(freelist_state, 1);
-    _PySlice_ClearCache(freelist_state);
+    _PyFreeListState *freelist_state = _PyFreeListState_GET();
+    _PyObject_ClearFreeLists(freelist_state, 1);
 
     // Remove ourself from the biased reference counting table of threads.
     _Py_brc_remove_thread(tstate);