Skip to content

gh-111968: Rename freelist related struct names to Eric's suggestion #115329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions Include/internal/pycore_freelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ extern "C" {
# define _PyObjectStackChunk_MAXFREELIST 0
#endif

struct _Py_list_state {
struct _Py_list_freelist {
#ifdef WITH_FREELISTS
PyListObject *free_list[PyList_MAXFREELIST];
int numfree;
#endif
};

struct _Py_tuple_state {
struct _Py_tuple_freelist {
#if WITH_FREELISTS
/* There is one freelist for each size from 1 to PyTuple_MAXSAVESIZE.
The empty tuple is handled separately.
Expand All @@ -57,7 +57,7 @@ struct _Py_tuple_state {
#endif
};

struct _Py_float_state {
struct _Py_float_freelist {
#ifdef WITH_FREELISTS
/* Special free list
free_list is a singly-linked list of available PyFloatObjects,
Expand All @@ -77,23 +77,23 @@ struct _Py_dict_freelist {
#endif
};

struct _Py_slice_state {
struct _Py_slice_freelist {
#ifdef WITH_FREELISTS
/* Using a cache is very effective since typically only a single slice is
created and then deleted again. */
PySliceObject *slice_cache;
#endif
};

struct _Py_context_state {
struct _Py_context_freelist {
#ifdef WITH_FREELISTS
// List of free PyContext objects
PyContext *freelist;
int numfree;
#endif
};

struct _Py_async_gen_state {
struct _Py_async_gen_freelist {
#ifdef WITH_FREELISTS
/* Freelists boost performance 6-10%; they also reduce memory
fragmentation, as _PyAsyncGenWrappedValue and PyAsyncGenASend
Expand All @@ -109,20 +109,20 @@ struct _Py_async_gen_state {

struct _PyObjectStackChunk;

struct _Py_object_stack_state {
struct _Py_object_stack_freelist {
struct _PyObjectStackChunk *free_list;
Py_ssize_t numfree;
};

typedef struct _Py_freelist_state {
struct _Py_float_state floats;
struct _Py_tuple_state tuples;
struct _Py_list_state lists;
typedef struct _PyObject_freelists {
struct _Py_float_freelist floats;
struct _Py_tuple_freelist tuples;
struct _Py_list_freelist lists;
struct _Py_dict_freelist dicts;
struct _Py_slice_state slices;
struct _Py_context_state contexts;
struct _Py_async_gen_state async_gens;
struct _Py_object_stack_state object_stacks;
struct _Py_slice_freelist slices;
struct _Py_context_freelist contexts;
struct _Py_async_gen_freelist async_gens;
struct _Py_object_stack_freelist object_stacks;
} _PyFreeListState;

extern void _PyObject_ClearFreeLists(_PyFreeListState *state, int is_finalization);
Expand Down
4 changes: 0 additions & 4 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ extern "C" {
#include "pycore_dtoa.h" // struct _dtoa_state
#include "pycore_exceptions.h" // struct _Py_exc_state
#include "pycore_floatobject.h" // struct _Py_float_state
#include "pycore_freelist.h" // struct _Py_freelist_state
#include "pycore_function.h" // FUNC_MAX_WATCHERS
#include "pycore_gc.h" // struct _gc_runtime_state
#include "pycore_genobject.h" // struct _Py_async_gen_state
Expand Down Expand Up @@ -222,9 +221,6 @@ struct _is {
// One bit is set for each non-NULL entry in code_watchers
uint8_t active_code_watchers;

#if !defined(Py_GIL_DISABLED)
struct _Py_freelist_state freelist_state;
#endif
struct _py_object_state object_state;
struct _Py_unicode_state unicode;
struct _Py_long_state long_state;
Expand Down
4 changes: 4 additions & 0 deletions Include/internal/pycore_object_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif

#include "pycore_freelist.h" // _PyObject_freelists
#include "pycore_hashtable.h" // _Py_hashtable_t

struct _py_object_runtime_state {
Expand All @@ -18,6 +19,9 @@ struct _py_object_runtime_state {
};

struct _py_object_state {
#if !defined(Py_GIL_DISABLED)
struct _PyObject_freelists freelists;
#endif
#ifdef Py_REF_DEBUG
Py_ssize_t reftotal;
#endif
Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ static inline _PyFreeListState* _PyFreeListState_GET(void)
#ifdef Py_GIL_DISABLED
return &((_PyThreadStateImpl*)tstate)->freelist_state;
#else
return &tstate->interp->freelist_state;
return &tstate->interp->object_state.freelists;
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_tstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ typedef struct _PyThreadStateImpl {

#ifdef Py_GIL_DISABLED
struct _mimalloc_thread_state mimalloc;
struct _Py_freelist_state freelist_state;
struct _PyObject_freelists freelist_state;
struct _brc_thread_state brc;
#endif

Expand Down
12 changes: 6 additions & 6 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "pycore_dtoa.h" // _Py_dg_dtoa()
#include "pycore_floatobject.h" // _PyFloat_FormatAdvancedWriter()
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_interp.h" // _PyInterpreterState.float_state
#include "pycore_interp.h" // _Py_float_freelist
#include "pycore_long.h" // _PyLong_GetOne()
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
#include "pycore_object.h" // _PyObject_Init(), _PyDebugAllocatorStats()
Expand All @@ -27,7 +27,7 @@ class float "PyObject *" "&PyFloat_Type"
#include "clinic/floatobject.c.h"

#ifdef WITH_FREELISTS
static struct _Py_float_state *
static struct _Py_float_freelist *
get_float_state(void)
{
_PyFreeListState *state = _PyFreeListState_GET();
Expand Down Expand Up @@ -129,7 +129,7 @@ PyFloat_FromDouble(double fval)
{
PyFloatObject *op;
#ifdef WITH_FREELISTS
struct _Py_float_state *state = get_float_state();
struct _Py_float_freelist *state = get_float_state();
op = state->free_list;
if (op != NULL) {
state->free_list = (PyFloatObject *) Py_TYPE(op);
Expand Down Expand Up @@ -245,7 +245,7 @@ _PyFloat_ExactDealloc(PyObject *obj)
assert(PyFloat_CheckExact(obj));
PyFloatObject *op = (PyFloatObject *)obj;
#ifdef WITH_FREELISTS
struct _Py_float_state *state = get_float_state();
struct _Py_float_freelist *state = get_float_state();
if (state->numfree >= PyFloat_MAXFREELIST || state->numfree < 0) {
PyObject_Free(op);
return;
Expand Down Expand Up @@ -1993,7 +1993,7 @@ void
_PyFloat_ClearFreeList(_PyFreeListState *freelist_state, int is_finalization)
{
#ifdef WITH_FREELISTS
struct _Py_float_state *state = &freelist_state->floats;
struct _Py_float_freelist *state = &freelist_state->floats;
PyFloatObject *f = state->free_list;
while (f != NULL) {
PyFloatObject *next = (PyFloatObject*) Py_TYPE(f);
Expand Down Expand Up @@ -2021,7 +2021,7 @@ void
_PyFloat_DebugMallocStats(FILE *out)
{
#ifdef WITH_FREELISTS
struct _Py_float_state *state = get_float_state();
struct _Py_float_freelist *state = get_float_state();
_PyDebugAllocatorStats(out,
"free PyFloatObject",
state->numfree, sizeof(PyFloatObject));
Expand Down
14 changes: 7 additions & 7 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "pycore_ceval.h" // _PyEval_EvalFrame()
#include "pycore_frame.h" // _PyInterpreterFrame
#include "pycore_gc.h" // _PyGC_CLEAR_FINALIZED()
#include "pycore_genobject.h" // struct _Py_async_gen_state
#include "pycore_genobject.h" // struct _Py_async_gen_freelist
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_opcode_utils.h" // RESUME_AFTER_YIELD_FROM
Expand Down Expand Up @@ -1629,7 +1629,7 @@ PyTypeObject PyAsyncGen_Type = {


#ifdef WITH_FREELISTS
static struct _Py_async_gen_state *
static struct _Py_async_gen_freelist *
get_async_gen_state(void)
{
_PyFreeListState *state = _PyFreeListState_GET();
Expand Down Expand Up @@ -1659,7 +1659,7 @@ void
_PyAsyncGen_ClearFreeLists(_PyFreeListState *freelist_state, int is_finalization)
{
#ifdef WITH_FREELISTS
struct _Py_async_gen_state *state = &freelist_state->async_gens;
struct _Py_async_gen_freelist *state = &freelist_state->async_gens;

while (state->value_numfree > 0) {
_PyAsyncGenWrappedValue *o;
Expand Down Expand Up @@ -1726,7 +1726,7 @@ async_gen_asend_dealloc(PyAsyncGenASend *o)
Py_CLEAR(o->ags_gen);
Py_CLEAR(o->ags_sendval);
#ifdef WITH_FREELISTS
struct _Py_async_gen_state *state = get_async_gen_state();
struct _Py_async_gen_freelist *state = get_async_gen_state();
if (state->asend_numfree >= 0 && state->asend_numfree < _PyAsyncGen_MAXFREELIST) {
assert(PyAsyncGenASend_CheckExact(o));
_PyGC_CLEAR_FINALIZED((PyObject *)o);
Expand Down Expand Up @@ -1896,7 +1896,7 @@ async_gen_asend_new(PyAsyncGenObject *gen, PyObject *sendval)
{
PyAsyncGenASend *o;
#ifdef WITH_FREELISTS
struct _Py_async_gen_state *state = get_async_gen_state();
struct _Py_async_gen_freelist *state = get_async_gen_state();
if (state->asend_numfree > 0) {
state->asend_numfree--;
o = state->asend_freelist[state->asend_numfree];
Expand Down Expand Up @@ -1931,7 +1931,7 @@ async_gen_wrapped_val_dealloc(_PyAsyncGenWrappedValue *o)
_PyObject_GC_UNTRACK((PyObject *)o);
Py_CLEAR(o->agw_val);
#ifdef WITH_FREELISTS
struct _Py_async_gen_state *state = get_async_gen_state();
struct _Py_async_gen_freelist *state = get_async_gen_state();
if (state->value_numfree >= 0 && state->value_numfree < _PyAsyncGen_MAXFREELIST) {
assert(_PyAsyncGenWrappedValue_CheckExact(o));
state->value_freelist[state->value_numfree++] = o;
Expand Down Expand Up @@ -2004,7 +2004,7 @@ _PyAsyncGenValueWrapperNew(PyThreadState *tstate, PyObject *val)
assert(val);

#ifdef WITH_FREELISTS
struct _Py_async_gen_state *state = get_async_gen_state();
struct _Py_async_gen_freelist *state = get_async_gen_state();
if (state->value_numfree > 0) {
state->value_numfree--;
o = state->value_freelist[state->value_numfree];
Expand Down
12 changes: 6 additions & 6 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_ceval.h" // _PyEval_GetBuiltin()
#include "pycore_interp.h" // PyInterpreterState.list
#include "pycore_list.h" // struct _Py_list_state, _PyListIterObject
#include "pycore_list.h" // struct _Py_list_freelist, _PyListIterObject
#include "pycore_long.h" // _PyLong_DigitCount
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
#include "pycore_object.h" // _PyObject_GC_TRACK(), _PyDebugAllocatorStats()
Expand All @@ -21,7 +21,7 @@ class list "PyListObject *" "&PyList_Type"
_Py_DECLARE_STR(list_err, "list index out of range");

#ifdef WITH_FREELISTS
static struct _Py_list_state *
static struct _Py_list_freelist *
get_list_state(void)
{
_PyFreeListState *state = _PyFreeListState_GET();
Expand Down Expand Up @@ -123,7 +123,7 @@ void
_PyList_ClearFreeList(_PyFreeListState *freelist_state, int is_finalization)
{
#ifdef WITH_FREELISTS
struct _Py_list_state *state = &freelist_state->lists;
struct _Py_list_freelist *state = &freelist_state->lists;
while (state->numfree > 0) {
PyListObject *op = state->free_list[--state->numfree];
assert(PyList_CheckExact(op));
Expand All @@ -140,7 +140,7 @@ void
_PyList_DebugMallocStats(FILE *out)
{
#ifdef WITH_FREELISTS
struct _Py_list_state *state = get_list_state();
struct _Py_list_freelist *state = get_list_state();
_PyDebugAllocatorStats(out,
"free PyListObject",
state->numfree, sizeof(PyListObject));
Expand All @@ -158,7 +158,7 @@ PyList_New(Py_ssize_t size)
}

#ifdef WITH_FREELISTS
struct _Py_list_state *state = get_list_state();
struct _Py_list_freelist *state = get_list_state();
if (PyList_MAXFREELIST && state->numfree > 0) {
state->numfree--;
op = state->free_list[state->numfree];
Expand Down Expand Up @@ -391,7 +391,7 @@ list_dealloc(PyObject *self)
PyMem_Free(op->ob_item);
}
#ifdef WITH_FREELISTS
struct _Py_list_state *state = get_list_state();
struct _Py_list_freelist *state = get_list_state();
if (state->numfree < PyList_MAXFREELIST && state->numfree >= 0 && PyList_CheckExact(op)) {
state->free_list[state->numfree++] = op;
OBJECT_STAT_INC(to_freelist);
Expand Down
8 changes: 4 additions & 4 deletions Python/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ contextvar_del(PyContextVar *var);


#ifdef WITH_FREELISTS
static struct _Py_context_state *
static struct _Py_context_freelist *
get_context_state(void)
{
_PyFreeListState *state = _PyFreeListState_GET();
Expand Down Expand Up @@ -341,7 +341,7 @@ _context_alloc(void)
{
PyContext *ctx;
#ifdef WITH_FREELISTS
struct _Py_context_state *state = get_context_state();
struct _Py_context_freelist *state = get_context_state();
if (state->numfree > 0) {
state->numfree--;
ctx = state->freelist;
Expand Down Expand Up @@ -468,7 +468,7 @@ context_tp_dealloc(PyContext *self)
(void)context_tp_clear(self);

#ifdef WITH_FREELISTS
struct _Py_context_state *state = get_context_state();
struct _Py_context_freelist *state = get_context_state();
if (state->numfree >= 0 && state->numfree < PyContext_MAXFREELIST) {
state->numfree++;
self->ctx_weakreflist = (PyObject *)state->freelist;
Expand Down Expand Up @@ -1270,7 +1270,7 @@ void
_PyContext_ClearFreeList(_PyFreeListState *freelist_state, int is_finalization)
{
#ifdef WITH_FREELISTS
struct _Py_context_state *state = &freelist_state->contexts;
struct _Py_context_freelist *state = &freelist_state->contexts;
for (; state->numfree > 0; state->numfree--) {
PyContext *ctx = state->freelist;
state->freelist = (PyContext *)ctx->ctx_weakreflist;
Expand Down
2 changes: 1 addition & 1 deletion Python/gc_gil.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
void
_PyGC_ClearAllFreeLists(PyInterpreterState *interp)
{
_PyObject_ClearFreeLists(&interp->freelist_state, 0);
_PyObject_ClearFreeLists(&interp->object_state.freelists, 0);
}

#endif
8 changes: 4 additions & 4 deletions Python/object_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
extern _PyObjectStackChunk *_PyObjectStackChunk_New(void);
extern void _PyObjectStackChunk_Free(_PyObjectStackChunk *);

static struct _Py_object_stack_state *
static struct _Py_object_stack_freelist *
get_state(void)
{
_PyFreeListState *state = _PyFreeListState_GET();
Expand All @@ -19,7 +19,7 @@ _PyObjectStackChunk *
_PyObjectStackChunk_New(void)
{
_PyObjectStackChunk *buf;
struct _Py_object_stack_state *state = get_state();
struct _Py_object_stack_freelist *state = get_state();
if (state->numfree > 0) {
buf = state->free_list;
state->free_list = buf->prev;
Expand All @@ -43,7 +43,7 @@ void
_PyObjectStackChunk_Free(_PyObjectStackChunk *buf)
{
assert(buf->n == 0);
struct _Py_object_stack_state *state = get_state();
struct _Py_object_stack_freelist *state = get_state();
if (state->numfree >= 0 &&
state->numfree < _PyObjectStackChunk_MAXFREELIST)
{
Expand Down Expand Up @@ -97,7 +97,7 @@ _PyObjectStackChunk_ClearFreeList(_PyFreeListState *free_lists, int is_finalizat
return;
}

struct _Py_object_stack_state *state = &free_lists->object_stacks;
struct _Py_object_stack_freelist *state = &free_lists->object_stacks;
while (state->numfree > 0) {
_PyObjectStackChunk *buf = state->free_list;
state->free_list = buf->prev;
Expand Down