Skip to content

Commit f15795c

Browse files
authored
gh-111968: Rename freelist related struct names to Eric's suggestion (gh-115329)
1 parent 518af37 commit f15795c

19 files changed

+190
-207
lines changed

Diff for: Include/internal/pycore_dict.h

-6
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ typedef struct {
6767
extern PyObject* _PyDictView_New(PyObject *, PyTypeObject *);
6868
extern PyObject* _PyDictView_Intersect(PyObject* self, PyObject *other);
6969

70-
71-
/* runtime lifecycle */
72-
73-
extern void _PyDict_Fini(PyInterpreterState *state);
74-
75-
7670
/* other API */
7771

7872
typedef struct {

Diff for: Include/internal/pycore_freelist.h

+26-26
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ extern "C" {
3333
# define _PyObjectStackChunk_MAXFREELIST 0
3434
#endif
3535

36-
struct _Py_list_state {
36+
struct _Py_list_freelist {
3737
#ifdef WITH_FREELISTS
3838
PyListObject *free_list[PyList_MAXFREELIST];
3939
int numfree;
4040
#endif
4141
};
4242

43-
struct _Py_tuple_state {
43+
struct _Py_tuple_freelist {
4444
#if WITH_FREELISTS
4545
/* There is one freelist for each size from 1 to PyTuple_MAXSAVESIZE.
4646
The empty tuple is handled separately.
@@ -57,7 +57,7 @@ struct _Py_tuple_state {
5757
#endif
5858
};
5959

60-
struct _Py_float_state {
60+
struct _Py_float_freelist {
6161
#ifdef WITH_FREELISTS
6262
/* Special free list
6363
free_list is a singly-linked list of available PyFloatObjects,
@@ -77,23 +77,23 @@ struct _Py_dict_freelist {
7777
#endif
7878
};
7979

80-
struct _Py_slice_state {
80+
struct _Py_slice_freelist {
8181
#ifdef WITH_FREELISTS
8282
/* Using a cache is very effective since typically only a single slice is
8383
created and then deleted again. */
8484
PySliceObject *slice_cache;
8585
#endif
8686
};
8787

88-
struct _Py_context_state {
88+
struct _Py_context_freelist {
8989
#ifdef WITH_FREELISTS
9090
// List of free PyContext objects
9191
PyContext *freelist;
9292
int numfree;
9393
#endif
9494
};
9595

96-
struct _Py_async_gen_state {
96+
struct _Py_async_gen_freelist {
9797
#ifdef WITH_FREELISTS
9898
/* Freelists boost performance 6-10%; they also reduce memory
9999
fragmentation, as _PyAsyncGenWrappedValue and PyAsyncGenASend
@@ -109,31 +109,31 @@ struct _Py_async_gen_state {
109109

110110
struct _PyObjectStackChunk;
111111

112-
struct _Py_object_stack_state {
112+
struct _Py_object_stack_freelist {
113113
struct _PyObjectStackChunk *free_list;
114114
Py_ssize_t numfree;
115115
};
116116

117-
typedef struct _Py_freelist_state {
118-
struct _Py_float_state floats;
119-
struct _Py_tuple_state tuples;
120-
struct _Py_list_state lists;
117+
struct _Py_object_freelists {
118+
struct _Py_float_freelist floats;
119+
struct _Py_tuple_freelist tuples;
120+
struct _Py_list_freelist lists;
121121
struct _Py_dict_freelist dicts;
122-
struct _Py_slice_state slices;
123-
struct _Py_context_state contexts;
124-
struct _Py_async_gen_state async_gens;
125-
struct _Py_object_stack_state object_stacks;
126-
} _PyFreeListState;
127-
128-
extern void _PyObject_ClearFreeLists(_PyFreeListState *state, int is_finalization);
129-
extern void _PyTuple_ClearFreeList(_PyFreeListState *state, int is_finalization);
130-
extern void _PyFloat_ClearFreeList(_PyFreeListState *state, int is_finalization);
131-
extern void _PyList_ClearFreeList(_PyFreeListState *state, int is_finalization);
132-
extern void _PySlice_ClearFreeList(_PyFreeListState *state, int is_finalization);
133-
extern void _PyDict_ClearFreeList(_PyFreeListState *state, int is_finalization);
134-
extern void _PyAsyncGen_ClearFreeLists(_PyFreeListState *state, int is_finalization);
135-
extern void _PyContext_ClearFreeList(_PyFreeListState *state, int is_finalization);
136-
extern void _PyObjectStackChunk_ClearFreeList(_PyFreeListState *state, int is_finalization);
122+
struct _Py_slice_freelist slices;
123+
struct _Py_context_freelist contexts;
124+
struct _Py_async_gen_freelist async_gens;
125+
struct _Py_object_stack_freelist object_stacks;
126+
};
127+
128+
extern void _PyObject_ClearFreeLists(struct _Py_object_freelists *freelists, int is_finalization);
129+
extern void _PyTuple_ClearFreeList(struct _Py_object_freelists *freelists, int is_finalization);
130+
extern void _PyFloat_ClearFreeList(struct _Py_object_freelists *freelists, int is_finalization);
131+
extern void _PyList_ClearFreeList(struct _Py_object_freelists *freelists, int is_finalization);
132+
extern void _PySlice_ClearFreeList(struct _Py_object_freelists *freelists, int is_finalization);
133+
extern void _PyDict_ClearFreeList(struct _Py_object_freelists *freelists, int is_finalization);
134+
extern void _PyAsyncGen_ClearFreeLists(struct _Py_object_freelists *freelists, int is_finalization);
135+
extern void _PyContext_ClearFreeList(struct _Py_object_freelists *freelists, int is_finalization);
136+
extern void _PyObjectStackChunk_ClearFreeList(struct _Py_object_freelists *freelists, int is_finalization);
137137

138138
#ifdef __cplusplus
139139
}

Diff for: Include/internal/pycore_interp.h

-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ extern "C" {
2020
#include "pycore_dtoa.h" // struct _dtoa_state
2121
#include "pycore_exceptions.h" // struct _Py_exc_state
2222
#include "pycore_floatobject.h" // struct _Py_float_state
23-
#include "pycore_freelist.h" // struct _Py_freelist_state
2423
#include "pycore_function.h" // FUNC_MAX_WATCHERS
2524
#include "pycore_gc.h" // struct _gc_runtime_state
2625
#include "pycore_genobject.h" // struct _Py_async_gen_state
@@ -222,9 +221,6 @@ struct _is {
222221
// One bit is set for each non-NULL entry in code_watchers
223222
uint8_t active_code_watchers;
224223

225-
#if !defined(Py_GIL_DISABLED)
226-
struct _Py_freelist_state freelist_state;
227-
#endif
228224
struct _py_object_state object_state;
229225
struct _Py_unicode_state unicode;
230226
struct _Py_long_state long_state;

Diff for: Include/internal/pycore_object_state.h

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
#include "pycore_freelist.h" // _PyObject_freelists
1112
#include "pycore_hashtable.h" // _Py_hashtable_t
1213

1314
struct _py_object_runtime_state {
@@ -18,6 +19,9 @@ struct _py_object_runtime_state {
1819
};
1920

2021
struct _py_object_state {
22+
#if !defined(Py_GIL_DISABLED)
23+
struct _Py_object_freelists freelists;
24+
#endif
2125
#ifdef Py_REF_DEBUG
2226
Py_ssize_t reftotal;
2327
#endif

Diff for: Include/internal/pycore_pystate.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -268,17 +268,17 @@ PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void);
268268
// See also PyInterpreterState_Get() and _PyInterpreterState_GET().
269269
extern PyInterpreterState* _PyGILState_GetInterpreterStateUnsafe(void);
270270

271-
static inline _PyFreeListState* _PyFreeListState_GET(void)
271+
static inline struct _Py_object_freelists* _Py_object_freelists_GET(void)
272272
{
273273
PyThreadState *tstate = _PyThreadState_GET();
274274
#ifdef Py_DEBUG
275275
_Py_EnsureTstateNotNULL(tstate);
276276
#endif
277277

278278
#ifdef Py_GIL_DISABLED
279-
return &((_PyThreadStateImpl*)tstate)->freelist_state;
279+
return &((_PyThreadStateImpl*)tstate)->freelists;
280280
#else
281-
return &tstate->interp->freelist_state;
281+
return &tstate->interp->object_state.freelists;
282282
#endif
283283
}
284284

Diff for: Include/internal/pycore_tstate.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ typedef struct _PyThreadStateImpl {
2929

3030
#ifdef Py_GIL_DISABLED
3131
struct _mimalloc_thread_state mimalloc;
32-
struct _Py_freelist_state freelist_state;
32+
struct _Py_object_freelists freelists;
3333
struct _brc_thread_state brc;
3434
#endif
3535

Diff for: Objects/dictobject.c

+20-31
Original file line numberDiff line numberDiff line change
@@ -271,19 +271,19 @@ dict_setdefault_ref_lock_held(PyObject *d, PyObject *key, PyObject *default_valu
271271

272272
#ifdef WITH_FREELISTS
273273
static struct _Py_dict_freelist *
274-
get_dict_state(void)
274+
get_dict_freelist(void)
275275
{
276-
_PyFreeListState *state = _PyFreeListState_GET();
277-
return &state->dicts;
276+
struct _Py_object_freelists *freelists = _Py_object_freelists_GET();
277+
return &freelists->dicts;
278278
}
279279
#endif
280280

281281

282282
void
283-
_PyDict_ClearFreeList(_PyFreeListState *freelist_state, int is_finalization)
283+
_PyDict_ClearFreeList(struct _Py_object_freelists *freelists, int is_finalization)
284284
{
285285
#ifdef WITH_FREELISTS
286-
struct _Py_dict_freelist *state = &freelist_state->dicts;
286+
struct _Py_dict_freelist *state = &freelists->dicts;
287287
while (state->numfree > 0) {
288288
PyDictObject *op = state->free_list[--state->numfree];
289289
assert(PyDict_CheckExact(op));
@@ -299,17 +299,6 @@ _PyDict_ClearFreeList(_PyFreeListState *freelist_state, int is_finalization)
299299
#endif
300300
}
301301

302-
void
303-
_PyDict_Fini(PyInterpreterState *Py_UNUSED(interp))
304-
{
305-
// With Py_GIL_DISABLED:
306-
// the freelists for the current thread state have already been cleared.
307-
#ifndef Py_GIL_DISABLED
308-
_PyFreeListState *state = _PyFreeListState_GET();
309-
_PyDict_ClearFreeList(state, 1);
310-
#endif
311-
}
312-
313302
static inline Py_hash_t
314303
unicode_get_hash(PyObject *o)
315304
{
@@ -322,9 +311,9 @@ void
322311
_PyDict_DebugMallocStats(FILE *out)
323312
{
324313
#ifdef WITH_FREELISTS
325-
struct _Py_dict_freelist *state = get_dict_state();
314+
struct _Py_dict_freelist *dict_freelist = get_dict_freelist();
326315
_PyDebugAllocatorStats(out, "free PyDictObject",
327-
state->numfree, sizeof(PyDictObject));
316+
dict_freelist->numfree, sizeof(PyDictObject));
328317
#endif
329318
}
330319

@@ -674,9 +663,9 @@ new_keys_object(PyInterpreterState *interp, uint8_t log2_size, bool unicode)
674663
}
675664

676665
#ifdef WITH_FREELISTS
677-
struct _Py_dict_freelist *state = get_dict_state();
678-
if (log2_size == PyDict_LOG_MINSIZE && unicode && state->keys_numfree > 0) {
679-
dk = state->keys_free_list[--state->keys_numfree];
666+
struct _Py_dict_freelist *dict_freelist = get_dict_freelist();
667+
if (log2_size == PyDict_LOG_MINSIZE && unicode && dict_freelist->keys_numfree > 0) {
668+
dk = dict_freelist->keys_free_list[--dict_freelist->keys_numfree];
680669
OBJECT_STAT_INC(from_freelist);
681670
}
682671
else
@@ -709,12 +698,12 @@ static void
709698
free_keys_object(PyDictKeysObject *keys)
710699
{
711700
#ifdef WITH_FREELISTS
712-
struct _Py_dict_freelist *state = get_dict_state();
701+
struct _Py_dict_freelist *dict_freelist = get_dict_freelist();
713702
if (DK_LOG_SIZE(keys) == PyDict_LOG_MINSIZE
714-
&& state->keys_numfree < PyDict_MAXFREELIST
715-
&& state->keys_numfree >= 0
703+
&& dict_freelist->keys_numfree < PyDict_MAXFREELIST
704+
&& dict_freelist->keys_numfree >= 0
716705
&& DK_IS_UNICODE(keys)) {
717-
state->keys_free_list[state->keys_numfree++] = keys;
706+
dict_freelist->keys_free_list[dict_freelist->keys_numfree++] = keys;
718707
OBJECT_STAT_INC(to_freelist);
719708
return;
720709
}
@@ -754,9 +743,9 @@ new_dict(PyInterpreterState *interp,
754743
PyDictObject *mp;
755744
assert(keys != NULL);
756745
#ifdef WITH_FREELISTS
757-
struct _Py_dict_freelist *state = get_dict_state();
758-
if (state->numfree > 0) {
759-
mp = state->free_list[--state->numfree];
746+
struct _Py_dict_freelist *dict_freelist = get_dict_freelist();
747+
if (dict_freelist->numfree > 0) {
748+
mp = dict_freelist->free_list[--dict_freelist->numfree];
760749
assert (mp != NULL);
761750
assert (Py_IS_TYPE(mp, &PyDict_Type));
762751
OBJECT_STAT_INC(from_freelist);
@@ -2604,10 +2593,10 @@ dict_dealloc(PyObject *self)
26042593
dictkeys_decref(interp, keys);
26052594
}
26062595
#ifdef WITH_FREELISTS
2607-
struct _Py_dict_freelist *state = get_dict_state();
2608-
if (state->numfree < PyDict_MAXFREELIST && state->numfree >=0 &&
2596+
struct _Py_dict_freelist *dict_freelist = get_dict_freelist();
2597+
if (dict_freelist->numfree < PyDict_MAXFREELIST && dict_freelist->numfree >=0 &&
26092598
Py_IS_TYPE(mp, &PyDict_Type)) {
2610-
state->free_list[state->numfree++] = mp;
2599+
dict_freelist->free_list[dict_freelist->numfree++] = mp;
26112600
OBJECT_STAT_INC(to_freelist);
26122601
}
26132602
else

Diff for: Objects/floatobject.c

+19-19
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "pycore_dtoa.h" // _Py_dg_dtoa()
99
#include "pycore_floatobject.h" // _PyFloat_FormatAdvancedWriter()
1010
#include "pycore_initconfig.h" // _PyStatus_OK()
11-
#include "pycore_interp.h" // _PyInterpreterState.float_state
11+
#include "pycore_interp.h" // _Py_float_freelist
1212
#include "pycore_long.h" // _PyLong_GetOne()
1313
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
1414
#include "pycore_object.h" // _PyObject_Init(), _PyDebugAllocatorStats()
@@ -27,12 +27,12 @@ class float "PyObject *" "&PyFloat_Type"
2727
#include "clinic/floatobject.c.h"
2828

2929
#ifdef WITH_FREELISTS
30-
static struct _Py_float_state *
31-
get_float_state(void)
30+
static struct _Py_float_freelist *
31+
get_float_freelist(void)
3232
{
33-
_PyFreeListState *state = _PyFreeListState_GET();
34-
assert(state != NULL);
35-
return &state->floats;
33+
struct _Py_object_freelists *freelists = _Py_object_freelists_GET();
34+
assert(freelists != NULL);
35+
return &freelists->floats;
3636
}
3737
#endif
3838

@@ -129,11 +129,11 @@ PyFloat_FromDouble(double fval)
129129
{
130130
PyFloatObject *op;
131131
#ifdef WITH_FREELISTS
132-
struct _Py_float_state *state = get_float_state();
133-
op = state->free_list;
132+
struct _Py_float_freelist *float_freelist = get_float_freelist();
133+
op = float_freelist->free_list;
134134
if (op != NULL) {
135-
state->free_list = (PyFloatObject *) Py_TYPE(op);
136-
state->numfree--;
135+
float_freelist->free_list = (PyFloatObject *) Py_TYPE(op);
136+
float_freelist->numfree--;
137137
OBJECT_STAT_INC(from_freelist);
138138
}
139139
else
@@ -245,14 +245,14 @@ _PyFloat_ExactDealloc(PyObject *obj)
245245
assert(PyFloat_CheckExact(obj));
246246
PyFloatObject *op = (PyFloatObject *)obj;
247247
#ifdef WITH_FREELISTS
248-
struct _Py_float_state *state = get_float_state();
249-
if (state->numfree >= PyFloat_MAXFREELIST || state->numfree < 0) {
248+
struct _Py_float_freelist *float_freelist = get_float_freelist();
249+
if (float_freelist->numfree >= PyFloat_MAXFREELIST || float_freelist->numfree < 0) {
250250
PyObject_Free(op);
251251
return;
252252
}
253-
state->numfree++;
254-
Py_SET_TYPE(op, (PyTypeObject *)state->free_list);
255-
state->free_list = op;
253+
float_freelist->numfree++;
254+
Py_SET_TYPE(op, (PyTypeObject *)float_freelist->free_list);
255+
float_freelist->free_list = op;
256256
OBJECT_STAT_INC(to_freelist);
257257
#else
258258
PyObject_Free(op);
@@ -1990,10 +1990,10 @@ _PyFloat_InitTypes(PyInterpreterState *interp)
19901990
}
19911991

19921992
void
1993-
_PyFloat_ClearFreeList(_PyFreeListState *freelist_state, int is_finalization)
1993+
_PyFloat_ClearFreeList(struct _Py_object_freelists *freelists, int is_finalization)
19941994
{
19951995
#ifdef WITH_FREELISTS
1996-
struct _Py_float_state *state = &freelist_state->floats;
1996+
struct _Py_float_freelist *state = &freelists->floats;
19971997
PyFloatObject *f = state->free_list;
19981998
while (f != NULL) {
19991999
PyFloatObject *next = (PyFloatObject*) Py_TYPE(f);
@@ -2021,10 +2021,10 @@ void
20212021
_PyFloat_DebugMallocStats(FILE *out)
20222022
{
20232023
#ifdef WITH_FREELISTS
2024-
struct _Py_float_state *state = get_float_state();
2024+
struct _Py_float_freelist *float_freelist = get_float_freelist();
20252025
_PyDebugAllocatorStats(out,
20262026
"free PyFloatObject",
2027-
state->numfree, sizeof(PyFloatObject));
2027+
float_freelist->numfree, sizeof(PyFloatObject));
20282028
#endif
20292029
}
20302030

0 commit comments

Comments
 (0)