summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDonghee Na <donghee.na@python.org>2024-02-10 00:57:04 (GMT)
committerGitHub <noreply@github.com>2024-02-10 00:57:04 (GMT)
commitd4d5bae1471788b345155e8e93a2fe4ab92d09dc (patch)
tree989f7efdc60aff606679d12b4f1853174dd4365d
parent564385612cdf72c2fa8e629a68225fb2cd3b3d99 (diff)
downloadcpython-d4d5bae1471788b345155e8e93a2fe4ab92d09dc.zip
cpython-d4d5bae1471788b345155e8e93a2fe4ab92d09dc.tar.gz
cpython-d4d5bae1471788b345155e8e93a2fe4ab92d09dc.tar.bz2
gh-111968: Refactor _PyXXX_Fini to integrate with _PyObject_ClearFreeLists (gh-114899)
-rw-r--r--Include/internal/pycore_context.h1
-rw-r--r--Include/internal/pycore_floatobject.h1
-rw-r--r--Include/internal/pycore_freelist.h10
-rw-r--r--Include/internal/pycore_gc.h8
-rw-r--r--Include/internal/pycore_genobject.h4
-rw-r--r--Include/internal/pycore_list.h6
-rw-r--r--Include/internal/pycore_object_stack.h3
-rw-r--r--Include/internal/pycore_sliceobject.h2
-rw-r--r--Include/internal/pycore_tuple.h1
-rw-r--r--Objects/floatobject.c10
-rw-r--r--Objects/genobject.c11
-rw-r--r--Objects/listobject.c10
-rw-r--r--Objects/object.c15
-rw-r--r--Objects/sliceobject.c12
-rw-r--r--Objects/tupleobject.c5
-rw-r--r--Python/context.c11
-rw-r--r--Python/gc_free_threading.c2
-rw-r--r--Python/gc_gil.c2
-rw-r--r--Python/pylifecycle.c12
-rw-r--r--Python/pystate.c19
20 files changed, 38 insertions, 107 deletions
diff --git a/Include/internal/pycore_context.h b/Include/internal/pycore_context.h
index 3284efb..ae5c47f 100644
--- a/Include/internal/pycore_context.h
+++ b/Include/internal/pycore_context.h
@@ -14,7 +14,6 @@ extern PyTypeObject _PyContextTokenMissing_Type;
/* runtime lifecycle */
PyStatus _PyContext_Init(PyInterpreterState *);
-void _PyContext_Fini(_PyFreeListState *);
/* other API */
diff --git a/Include/internal/pycore_floatobject.h b/Include/internal/pycore_floatobject.h
index 038578e..3767df5 100644
--- a/Include/internal/pycore_floatobject.h
+++ b/Include/internal/pycore_floatobject.h
@@ -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 *);
diff --git a/Include/internal/pycore_freelist.h b/Include/internal/pycore_freelist.h
index 82a4230..1bc5519 100644
--- a/Include/internal/pycore_freelist.h
+++ b/Include/internal/pycore_freelist.h
@@ -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
diff --git a/Include/internal/pycore_gc.h b/Include/internal/pycore_gc.h
index 8d0bc2a..582a16b 100644
--- a/Include/internal/pycore_gc.h
+++ b/Include/internal/pycore_gc.h
@@ -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);
diff --git a/Include/internal/pycore_genobject.h b/Include/internal/pycore_genobject.h
index 5ad6365..b2aa017 100644
--- a/Include/internal/pycore_genobject.h
+++ b/Include/internal/pycore_genobject.h
@@ -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
diff --git a/Include/internal/pycore_list.h b/Include/internal/pycore_list.h
index 4536f90..50dc13c 100644
--- a/Include/internal/pycore_list.h
+++ b/Include/internal/pycore_list.h
@@ -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
diff --git a/Include/internal/pycore_object_stack.h b/Include/internal/pycore_object_stack.h
index d042be2..fc130b1 100644
--- a/Include/internal/pycore_object_stack.h
+++ b/Include/internal/pycore_object_stack.h
@@ -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)
diff --git a/Include/internal/pycore_sliceobject.h b/Include/internal/pycore_sliceobject.h
index 0c72d3e..89086f6 100644
--- a/Include/internal/pycore_sliceobject.h
+++ b/Include/internal/pycore_sliceobject.h
@@ -11,8 +11,6 @@ extern "C" {
/* runtime lifecycle */
-extern void _PySlice_Fini(_PyFreeListState *);
-
extern PyObject *
_PyBuildSlice_ConsumeRefs(PyObject *start, PyObject *stop);
diff --git a/Include/internal/pycore_tuple.h b/Include/internal/pycore_tuple.h
index b348339..4605f35 100644
--- a/Include/internal/pycore_tuple.h
+++ b/Include/internal/pycore_tuple.h
@@ -14,7 +14,6 @@ extern void _PyTuple_DebugMallocStats(FILE *out);
/* runtime lifecycle */
extern PyStatus _PyTuple_InitGlobalObjects(PyInterpreterState *);
-extern void _PyTuple_Fini(_PyFreeListState *);
/* other API */
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index c440e0d..9b322c5 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -2011,16 +2011,6 @@ _PyFloat_ClearFreeList(_PyFreeListState *freelist_state, int is_finalization)
}
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)
{
_PyStructSequence_FiniBuiltin(interp, &FloatInfoType);
diff --git a/Objects/genobject.c b/Objects/genobject.c
index ab523e4..59ab7ab 100644
--- a/Objects/genobject.c
+++ b/Objects/genobject.c
@@ -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)
{
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 307b8f1..7fdb91e 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -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)
diff --git a/Objects/object.c b/Objects/object.c
index 61e6131..275aa671 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -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):
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
index 8b9d6bb..9880c12 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -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.
*/
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index b9bf6cd..7d73c3f 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -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)
diff --git a/Python/context.c b/Python/context.c
index 793dfa2..e44fef7 100644
--- a/Python/context.c
+++ b/Python/context.c
@@ -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)
{
diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c
index 5d3b097..93e1168 100644
--- a/Python/gc_free_threading.c
+++ b/Python/gc_free_threading.c
@@ -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);
diff --git a/Python/gc_gil.c b/Python/gc_gil.c
index 4e2aa8f..5f1365f 100644
--- a/Python/gc_gil.c
+++ b/Python/gc_gil.c
@@ -11,7 +11,7 @@
void
_PyGC_ClearAllFreeLists(PyInterpreterState *interp)
{
- _Py_ClearFreeLists(&interp->freelist_state, 0);
+ _PyObject_ClearFreeLists(&interp->freelist_state, 0);
}
#endif
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 0cac710..61c9d4f 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -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);
diff --git a/Python/pystate.c b/Python/pystate.c
index 6cd0347..937c430 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -1469,20 +1469,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)
{
assert(tstate->_status.initialized && !tstate->_status.cleared);
@@ -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);