Skip to content

Commit 6acf43d

Browse files
committed
gh-111968: Introduce _Py_freelist_state and _PyFreeListState_GET API
1 parent 88cb972 commit 6acf43d

File tree

7 files changed

+55
-12
lines changed

7 files changed

+55
-12
lines changed

Include/internal/pycore_gc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ extern Py_ssize_t _PyGC_CollectNoFail(PyThreadState *tstate);
209209
// Functions to clear types free lists
210210
extern void _PyTuple_ClearFreeList(PyInterpreterState *interp);
211211
extern void _PyFloat_ClearFreeList(PyInterpreterState *interp);
212-
extern void _PyList_ClearFreeList(PyInterpreterState *interp);
212+
extern void _PyList_ClearFreeList(PyFreeListState *state);
213213
extern void _PyDict_ClearFreeList(PyInterpreterState *interp);
214214
extern void _PyAsyncGen_ClearFreeLists(PyInterpreterState *interp);
215215
extern void _PyContext_ClearFreeList(PyInterpreterState *interp);

Include/internal/pycore_interp.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ struct _is {
174174
// One bit is set for each non-NULL entry in code_watchers
175175
uint8_t active_code_watchers;
176176

177+
#if !defined(Py_GIL_DISABLED)
178+
struct _Py_freelist_state freelist_state;
179+
#endif
177180
struct _py_object_state object_state;
178181
struct _Py_unicode_state unicode;
179182
struct _Py_float_state float_state;
@@ -185,7 +188,6 @@ struct _is {
185188
PySliceObject *slice_cache;
186189

187190
struct _Py_tuple_state tuple;
188-
struct _Py_list_state list;
189191
struct _Py_dict_state dict_state;
190192
struct _Py_async_gen_state async_gen;
191193
struct _Py_context_state context;

Include/internal/pycore_pystate.h

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern "C" {
99
#endif
1010

1111
#include "pycore_runtime.h" // _PyRuntime
12+
#include "pycore_tstate.h" // _PyThreadStateImpl
1213

1314

1415
// Values for PyThreadState.state. A thread must be in the "attached" state
@@ -239,6 +240,19 @@ PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void);
239240
// See also PyInterpreterState_Get() and _PyInterpreterState_GET().
240241
extern PyInterpreterState* _PyGILState_GetInterpreterStateUnsafe(void);
241242

243+
static inline PyFreeListState* _PyFreeListState_GET(void) {
244+
PyThreadState *tstate = _PyThreadState_GET();
245+
#ifdef Py_DEBUG
246+
_Py_EnsureTstateNotNULL(tstate);
247+
#endif
248+
249+
#ifdef Py_GIL_DISABLED
250+
return &((_PyThreadStateImpl*)tstate)->freelist_state;
251+
#else
252+
return &tstate->interp->freelist_state;
253+
#endif
254+
}
255+
242256
#ifdef __cplusplus
243257
}
244258
#endif

Include/internal/pycore_tstate.h

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ extern "C" {
1111
#include "pycore_mimalloc.h" // struct _mimalloc_thread_state
1212

1313

14+
typedef struct _Py_freelist_state {
15+
struct _Py_list_state list;
16+
} _Py_freelist_state;
17+
1418
// Every PyThreadState is actually allocated as a _PyThreadStateImpl. The
1519
// PyThreadState fields are exposed as part of the C API, although most fields
1620
// are intended to be private. The _PyThreadStateImpl fields not exposed.
@@ -20,6 +24,7 @@ typedef struct _PyThreadStateImpl {
2024

2125
#ifdef Py_GIL_DISABLED
2226
struct _mimalloc_thread_state mimalloc;
27+
struct _Py_freelist_state freelist_state;
2328
#endif
2429

2530
} _PyThreadStateImpl;

Include/pytypedefs.h

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ typedef struct _frame PyFrameObject;
2323

2424
typedef struct _ts PyThreadState;
2525
typedef struct _is PyInterpreterState;
26+
typedef struct _Py_freelist_state PyFreeListState;
2627

2728
#ifdef __cplusplus
2829
}

Modules/gcmodule.c

+21-1
Original file line numberDiff line numberDiff line change
@@ -1070,14 +1070,33 @@ delete_garbage(PyThreadState *tstate, GCState *gcstate,
10701070
static void
10711071
clear_freelists(PyInterpreterState *interp)
10721072
{
1073+
// TODO: Unify with clear_all_freelists
10731074
_PyTuple_ClearFreeList(interp);
10741075
_PyFloat_ClearFreeList(interp);
1075-
_PyList_ClearFreeList(interp);
10761076
_PyDict_ClearFreeList(interp);
10771077
_PyAsyncGen_ClearFreeLists(interp);
10781078
_PyContext_ClearFreeList(interp);
10791079
}
10801080

1081+
static void
1082+
clear_all_freelists(PyInterpreterState *interp)
1083+
{
1084+
clear_freelists(interp);
1085+
#if defined(Py_GIL_DISABLED)
1086+
HEAD_LOCK(&_PyRuntime);
1087+
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *)interp->threads.head;
1088+
while (tstate != NULL) {
1089+
_PyList_ClearFreeList(&tstate->freelist_state);
1090+
tstate = tstate->base.next;
1091+
}
1092+
HEAD_UNLOCK(&_PyRuntime);
1093+
#else
1094+
// Only free-lists per interpreter are existed.
1095+
PyFreeListState* state = _PyFreeListState_GET();
1096+
_PyList_ClearFreeList(state);
1097+
#endif
1098+
}
1099+
10811100
// Show stats for objects in each generations
10821101
static void
10831102
show_stats_each_generations(GCState *gcstate)
@@ -1486,6 +1505,7 @@ gc_collect_main(PyThreadState *tstate, int generation, _PyGC_Reason reason)
14861505
* generation */
14871506
if (generation == NUM_GENERATIONS-1) {
14881507
clear_freelists(tstate->interp);
1508+
clear_all_freelists(tstate->interp);
14891509
}
14901510

14911511
if (_PyErr_Occurred(tstate)) {

Objects/listobject.c

+10-9
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ _Py_DECLARE_STR(list_err, "list index out of range");
2424
static struct _Py_list_state *
2525
get_list_state(void)
2626
{
27-
PyInterpreterState *interp = _PyInterpreterState_GET();
28-
return &interp->list;
27+
PyFreeListState *state = _PyFreeListState_GET();
28+
assert(state != NULL);
29+
return &state->list;
2930
}
3031
#endif
3132

@@ -120,12 +121,12 @@ list_preallocate_exact(PyListObject *self, Py_ssize_t size)
120121
}
121122

122123
void
123-
_PyList_ClearFreeList(PyInterpreterState *interp)
124+
_PyList_ClearFreeList(PyFreeListState *state)
124125
{
125126
#if PyList_MAXFREELIST > 0
126-
struct _Py_list_state *state = &interp->list;
127-
while (state->numfree) {
128-
PyListObject *op = state->free_list[--state->numfree];
127+
struct _Py_list_state *list_state = &state->list;
128+
while (list_state->numfree) {
129+
PyListObject *op = list_state->free_list[--list_state->numfree];
129130
assert(PyList_CheckExact(op));
130131
PyObject_GC_Del(op);
131132
}
@@ -135,10 +136,10 @@ _PyList_ClearFreeList(PyInterpreterState *interp)
135136
void
136137
_PyList_Fini(PyInterpreterState *interp)
137138
{
138-
_PyList_ClearFreeList(interp);
139+
PyFreeListState *state = _PyFreeListState_GET();
140+
_PyList_ClearFreeList(state);
139141
#if defined(Py_DEBUG) && PyList_MAXFREELIST > 0
140-
struct _Py_list_state *state = &interp->list;
141-
state->numfree = -1;
142+
state->list.numfree = -1;
142143
#endif
143144
}
144145

0 commit comments

Comments
 (0)