Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Address code review
  • Loading branch information
corona10 committed Feb 16, 2024
commit 1688fa71650d5ad69c30b0d3678345fc7d74379d
14 changes: 7 additions & 7 deletions Include/internal/pycore_freelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extern "C" {

struct _Py_list_freelist {
#ifdef WITH_FREELISTS
PyListObject *free_list[PyList_MAXFREELIST];
PyListObject *items[PyList_MAXFREELIST];
int numfree;
#endif
};
Expand All @@ -50,7 +50,7 @@ struct _Py_tuple_freelist {
object is used as the linked list node, with its first item
(ob_item[0]) pointing to the next node (i.e. the previous head).
Each linked list is initially NULL. */
PyTupleObject *free_list[PyTuple_NFREELISTS];
PyTupleObject *items[PyTuple_NFREELISTS];
int numfree[PyTuple_NFREELISTS];
#else
char _unused; // Empty structs are not allowed.
Expand All @@ -63,22 +63,22 @@ struct _Py_float_freelist {
free_list is a singly-linked list of available PyFloatObjects,
linked via abuse of their ob_type members. */
int numfree;
PyFloatObject *free_list;
PyFloatObject *items;
#endif
};

struct _Py_dict_freelist {
#ifdef WITH_FREELISTS
/* Dictionary reuse scheme to save calls to malloc and free */
PyDictObject *free_list[PyDict_MAXFREELIST];
PyDictObject *items[PyDict_MAXFREELIST];
int numfree;
#endif
};

struct _Py_dictkeys_freelist {
#ifdef WITH_FREELISTS
/* Dictionary keys reuse scheme to save calls to malloc and free */
PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
PyDictKeysObject *items[PyDict_MAXFREELIST];
int numfree;
#endif
};
Expand All @@ -94,7 +94,7 @@ struct _Py_slice_freelist {
struct _Py_context_freelist {
#ifdef WITH_FREELISTS
// List of free PyContext objects
PyContext *freelist;
PyContext *items;
int numfree;
#endif
};
Expand All @@ -116,7 +116,7 @@ struct _Py_async_gen_freelist {
struct _PyObjectStackChunk;

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

Expand Down
12 changes: 6 additions & 6 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,13 @@ _PyDict_ClearFreeList(struct _Py_object_freelists *freelists, int is_finalizatio
#ifdef WITH_FREELISTS
struct _Py_dict_freelist *freelist = &freelists->dicts;
while (freelist->numfree > 0) {
PyDictObject *op = freelist->free_list[--freelist->numfree];
PyDictObject *op = freelist->items[--freelist->numfree];
assert(PyDict_CheckExact(op));
PyObject_GC_Del(op);
}
struct _Py_dictkeys_freelist *keys_freelist = &freelists->dictkeys;
while (keys_freelist->numfree > 0) {
PyMem_Free(keys_freelist->keys_free_list[--keys_freelist->numfree]);
PyMem_Free(keys_freelist->items[--keys_freelist->numfree]);
}
if (is_finalization) {
freelist->numfree = -1;
Expand Down Expand Up @@ -676,7 +676,7 @@ new_keys_object(PyInterpreterState *interp, uint8_t log2_size, bool unicode)
#ifdef WITH_FREELISTS
struct _Py_dictkeys_freelist *freelist = get_dictkeys_freelist();
if (log2_size == PyDict_LOG_MINSIZE && unicode && freelist->numfree > 0) {
dk = freelist->keys_free_list[--freelist->numfree];
dk = freelist->items[--freelist->numfree];
OBJECT_STAT_INC(from_freelist);
}
else
Expand Down Expand Up @@ -714,7 +714,7 @@ free_keys_object(PyDictKeysObject *keys)
&& freelist->numfree < PyDict_MAXFREELIST
&& freelist->numfree >= 0
&& DK_IS_UNICODE(keys)) {
freelist->keys_free_list[freelist->numfree++] = keys;
freelist->items[freelist->numfree++] = keys;
OBJECT_STAT_INC(to_freelist);
return;
}
Expand Down Expand Up @@ -756,7 +756,7 @@ new_dict(PyInterpreterState *interp,
#ifdef WITH_FREELISTS
struct _Py_dict_freelist *freelist = get_dict_freelist();
if (freelist->numfree > 0) {
mp = freelist->free_list[--freelist->numfree];
mp = freelist->items[--freelist->numfree];
assert (mp != NULL);
assert (Py_IS_TYPE(mp, &PyDict_Type));
OBJECT_STAT_INC(from_freelist);
Expand Down Expand Up @@ -2607,7 +2607,7 @@ dict_dealloc(PyObject *self)
struct _Py_dict_freelist *freelist = get_dict_freelist();
if (freelist->numfree < PyDict_MAXFREELIST && freelist->numfree >=0 &&
Py_IS_TYPE(mp, &PyDict_Type)) {
freelist->free_list[freelist->numfree++] = mp;
freelist->items[freelist->numfree++] = mp;
OBJECT_STAT_INC(to_freelist);
}
else
Expand Down
12 changes: 6 additions & 6 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ PyFloat_FromDouble(double fval)
PyFloatObject *op;
#ifdef WITH_FREELISTS
struct _Py_float_freelist *float_freelist = get_float_freelist();
op = float_freelist->free_list;
op = float_freelist->items;
if (op != NULL) {
float_freelist->free_list = (PyFloatObject *) Py_TYPE(op);
float_freelist->items = (PyFloatObject *) Py_TYPE(op);
float_freelist->numfree--;
OBJECT_STAT_INC(from_freelist);
}
Expand Down Expand Up @@ -251,8 +251,8 @@ _PyFloat_ExactDealloc(PyObject *obj)
return;
}
float_freelist->numfree++;
Py_SET_TYPE(op, (PyTypeObject *)float_freelist->free_list);
float_freelist->free_list = op;
Py_SET_TYPE(op, (PyTypeObject *)float_freelist->items);
float_freelist->items = op;
OBJECT_STAT_INC(to_freelist);
#else
PyObject_Free(op);
Expand Down Expand Up @@ -1994,13 +1994,13 @@ _PyFloat_ClearFreeList(struct _Py_object_freelists *freelists, int is_finalizati
{
#ifdef WITH_FREELISTS
struct _Py_float_freelist *state = &freelists->floats;
PyFloatObject *f = state->free_list;
PyFloatObject *f = state->items;
while (f != NULL) {
PyFloatObject *next = (PyFloatObject*) Py_TYPE(f);
PyObject_Free(f);
f = next;
}
state->free_list = NULL;
state->items = NULL;
if (is_finalization) {
state->numfree = -1;
}
Expand Down
6 changes: 3 additions & 3 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ _PyList_ClearFreeList(struct _Py_object_freelists *freelists, int is_finalizatio
#ifdef WITH_FREELISTS
struct _Py_list_freelist *state = &freelists->lists;
while (state->numfree > 0) {
PyListObject *op = state->free_list[--state->numfree];
PyListObject *op = state->items[--state->numfree];
assert(PyList_CheckExact(op));
PyObject_GC_Del(op);
}
Expand Down Expand Up @@ -169,7 +169,7 @@ PyList_New(Py_ssize_t size)
struct _Py_list_freelist *list_freelist = get_list_freelist();
if (PyList_MAXFREELIST && list_freelist->numfree > 0) {
list_freelist->numfree--;
op = list_freelist->free_list[list_freelist->numfree];
op = list_freelist->items[list_freelist->numfree];
OBJECT_STAT_INC(from_freelist);
_Py_NewReference((PyObject *)op);
}
Expand Down Expand Up @@ -401,7 +401,7 @@ list_dealloc(PyObject *self)
#ifdef WITH_FREELISTS
struct _Py_list_freelist *list_freelist = get_list_freelist();
if (list_freelist->numfree < PyList_MAXFREELIST && list_freelist->numfree >= 0 && PyList_CheckExact(op)) {
list_freelist->free_list[list_freelist->numfree++] = op;
list_freelist->items[list_freelist->numfree++] = op;
OBJECT_STAT_INC(to_freelist);
}
else
Expand Down
12 changes: 6 additions & 6 deletions Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1134,11 +1134,11 @@ maybe_freelist_pop(Py_ssize_t size)
assert(size > 0);
if (size < PyTuple_MAXSAVESIZE) {
Py_ssize_t index = size - 1;
PyTupleObject *op = TUPLE_FREELIST.free_list[index];
PyTupleObject *op = TUPLE_FREELIST.items[index];
if (op != NULL) {
/* op is the head of a linked list, with the first item
pointing to the next node. Here we pop off the old head. */
TUPLE_FREELIST.free_list[index] = (PyTupleObject *) op->ob_item[0];
TUPLE_FREELIST.items[index] = (PyTupleObject *) op->ob_item[0];
TUPLE_FREELIST.numfree[index]--;
/* Inlined _PyObject_InitVar() without _PyType_HasFeature() test */
#ifdef Py_TRACE_REFS
Expand Down Expand Up @@ -1173,8 +1173,8 @@ maybe_freelist_push(PyTupleObject *op)
{
/* op is the head of a linked list, with the first item
pointing to the next node. Here we set op as the new head. */
op->ob_item[0] = (PyObject *) TUPLE_FREELIST.free_list[index];
TUPLE_FREELIST.free_list[index] = op;
op->ob_item[0] = (PyObject *) TUPLE_FREELIST.items[index];
TUPLE_FREELIST.items[index] = op;
TUPLE_FREELIST.numfree[index]++;
OBJECT_STAT_INC(to_freelist);
return 1;
Expand All @@ -1188,8 +1188,8 @@ maybe_freelist_clear(struct _Py_object_freelists *freelists, int fini)
{
#ifdef WITH_FREELISTS
for (Py_ssize_t i = 0; i < PyTuple_NFREELISTS; i++) {
PyTupleObject *p = TUPLE_FREELIST.free_list[i];
TUPLE_FREELIST.free_list[i] = NULL;
PyTupleObject *p = TUPLE_FREELIST.items[i];
TUPLE_FREELIST.items[i] = NULL;
TUPLE_FREELIST.numfree[i] = fini ? -1 : 0;
while (p) {
PyTupleObject *q = p;
Expand Down
12 changes: 6 additions & 6 deletions Python/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,8 @@ _context_alloc(void)
struct _Py_context_freelist *context_freelist = get_context_freelist();
if (context_freelist->numfree > 0) {
context_freelist->numfree--;
ctx = context_freelist->freelist;
context_freelist->freelist = (PyContext *)ctx->ctx_weakreflist;
ctx = context_freelist->items;
context_freelist->items = (PyContext *)ctx->ctx_weakreflist;
OBJECT_STAT_INC(from_freelist);
ctx->ctx_weakreflist = NULL;
_Py_NewReference((PyObject *)ctx);
Expand Down Expand Up @@ -471,8 +471,8 @@ context_tp_dealloc(PyContext *self)
struct _Py_context_freelist *context_freelist = get_context_freelist();
if (context_freelist->numfree >= 0 && context_freelist->numfree < PyContext_MAXFREELIST) {
context_freelist->numfree++;
self->ctx_weakreflist = (PyObject *)context_freelist->freelist;
context_freelist->freelist = self;
self->ctx_weakreflist = (PyObject *)context_freelist->items;
context_freelist->items = self;
OBJECT_STAT_INC(to_freelist);
}
else
Expand Down Expand Up @@ -1272,8 +1272,8 @@ _PyContext_ClearFreeList(struct _Py_object_freelists *freelists, int is_finaliza
#ifdef WITH_FREELISTS
struct _Py_context_freelist *state = &freelists->contexts;
for (; state->numfree > 0; state->numfree--) {
PyContext *ctx = state->freelist;
state->freelist = (PyContext *)ctx->ctx_weakreflist;
PyContext *ctx = state->items;
state->items = (PyContext *)ctx->ctx_weakreflist;
ctx->ctx_weakreflist = NULL;
PyObject_GC_Del(ctx);
}
Expand Down
20 changes: 10 additions & 10 deletions Python/object_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ _PyObjectStackChunk_New(void)
_PyObjectStackChunk *buf;
struct _Py_object_stack_freelist *obj_stack_freelist = get_object_stack_freelist();
if (obj_stack_freelist->numfree > 0) {
buf = obj_stack_freelist->free_list;
obj_stack_freelist->free_list = buf->prev;
buf = obj_stack_freelist->items;
obj_stack_freelist->items = buf->prev;
obj_stack_freelist->numfree--;
}
else {
Expand All @@ -47,8 +47,8 @@ _PyObjectStackChunk_Free(_PyObjectStackChunk *buf)
if (obj_stack_freelist->numfree >= 0 &&
obj_stack_freelist->numfree < _PyObjectStackChunk_MAXFREELIST)
{
buf->prev = obj_stack_freelist->free_list;
obj_stack_freelist->free_list = buf;
buf->prev = obj_stack_freelist->items;
obj_stack_freelist->items = buf;
obj_stack_freelist->numfree++;
}
else {
Expand Down Expand Up @@ -97,12 +97,12 @@ _PyObjectStackChunk_ClearFreeList(struct _Py_object_freelists *freelists, int is
return;
}

struct _Py_object_stack_freelist *state = &freelists->object_stacks;
while (state->numfree > 0) {
_PyObjectStackChunk *buf = state->free_list;
state->free_list = buf->prev;
state->numfree--;
struct _Py_object_stack_freelist *freelist = &freelists->object_stacks;
while (freelist->numfree > 0) {
_PyObjectStackChunk *buf = freelist->items;
freelist->items = buf->prev;
freelist->numfree--;
PyMem_RawFree(buf);
}
state->numfree = -1;
freelist->numfree = -1;
}