gh-111968: Split _Py_dictkeys_freelist out of _Py_dict_freelist (gh-115505)

This commit is contained in:
Donghee Na 2024-02-16 10:01:36 +09:00 • committed by GitHub
parent 454d7963e3
commit 321d13fd2b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 77 additions and 59 deletions

View file

@ -276,6 +276,13 @@ get_dict_freelist(void)
struct _Py_object_freelists *freelists = _Py_object_freelists_GET();
return &freelists->dicts;
}
static struct _Py_dictkeys_freelist *
get_dictkeys_freelist(void)
{
struct _Py_object_freelists *freelists = _Py_object_freelists_GET();
return &freelists->dictkeys;
}
#endif
@ -283,18 +290,19 @@ void
_PyDict_ClearFreeList(struct _Py_object_freelists *freelists, int is_finalization)
{
#ifdef WITH_FREELISTS
struct _Py_dict_freelist *state = &freelists->dicts;
while (state->numfree > 0) {
PyDictObject *op = state->free_list[--state->numfree];
struct _Py_dict_freelist *freelist = &freelists->dicts;
while (freelist->numfree > 0) {
PyDictObject *op = freelist->items[--freelist->numfree];
assert(PyDict_CheckExact(op));
PyObject_GC_Del(op);
}
while (state->keys_numfree > 0) {
PyMem_Free(state->keys_free_list[--state->keys_numfree]);
struct _Py_dictkeys_freelist *keys_freelist = &freelists->dictkeys;
while (keys_freelist->numfree > 0) {
PyMem_Free(keys_freelist->items[--keys_freelist->numfree]);
}
if (is_finalization) {
state->numfree = -1;
state->keys_numfree = -1;
freelist->numfree = -1;
keys_freelist->numfree = -1;
}
#endif
}
@ -314,6 +322,9 @@ _PyDict_DebugMallocStats(FILE *out)
struct _Py_dict_freelist *dict_freelist = get_dict_freelist();
_PyDebugAllocatorStats(out, "free PyDictObject",
dict_freelist->numfree, sizeof(PyDictObject));
struct _Py_dictkeys_freelist *dictkeys_freelist = get_dictkeys_freelist();
_PyDebugAllocatorStats(out, "free PyDictKeysObject",
dictkeys_freelist->numfree, sizeof(PyDictKeysObject));
#endif
}
@ -663,9 +674,9 @@ new_keys_object(PyInterpreterState *interp, uint8_t log2_size, bool unicode)
}
#ifdef WITH_FREELISTS
struct _Py_dict_freelist *dict_freelist = get_dict_freelist();
if (log2_size == PyDict_LOG_MINSIZE && unicode && dict_freelist->keys_numfree > 0) {
dk = dict_freelist->keys_free_list[--dict_freelist->keys_numfree];
struct _Py_dictkeys_freelist *freelist = get_dictkeys_freelist();
if (log2_size == PyDict_LOG_MINSIZE && unicode && freelist->numfree > 0) {
dk = freelist->items[--freelist->numfree];
OBJECT_STAT_INC(from_freelist);
}
else
@ -698,12 +709,12 @@ static void
free_keys_object(PyDictKeysObject *keys)
{
#ifdef WITH_FREELISTS
struct _Py_dict_freelist *dict_freelist = get_dict_freelist();
struct _Py_dictkeys_freelist *freelist = get_dictkeys_freelist();
if (DK_LOG_SIZE(keys) == PyDict_LOG_MINSIZE
&& dict_freelist->keys_numfree < PyDict_MAXFREELIST
&& dict_freelist->keys_numfree >= 0
&& freelist->numfree < PyDict_MAXFREELIST
&& freelist->numfree >= 0
&& DK_IS_UNICODE(keys)) {
dict_freelist->keys_free_list[dict_freelist->keys_numfree++] = keys;
freelist->items[freelist->numfree++] = keys;
OBJECT_STAT_INC(to_freelist);
return;
}
@ -743,9 +754,9 @@ new_dict(PyInterpreterState *interp,
PyDictObject *mp;
assert(keys != NULL);
#ifdef WITH_FREELISTS
struct _Py_dict_freelist *dict_freelist = get_dict_freelist();
if (dict_freelist->numfree > 0) {
mp = dict_freelist->free_list[--dict_freelist->numfree];
struct _Py_dict_freelist *freelist = get_dict_freelist();
if (freelist->numfree > 0) {
mp = freelist->items[--freelist->numfree];
assert (mp != NULL);
assert (Py_IS_TYPE(mp, &PyDict_Type));
OBJECT_STAT_INC(from_freelist);
@ -2593,10 +2604,10 @@ dict_dealloc(PyObject *self)
dictkeys_decref(interp, keys);
}
#ifdef WITH_FREELISTS
struct _Py_dict_freelist *dict_freelist = get_dict_freelist();
if (dict_freelist->numfree < PyDict_MAXFREELIST && dict_freelist->numfree >=0 &&
struct _Py_dict_freelist *freelist = get_dict_freelist();
if (freelist->numfree < PyDict_MAXFREELIST && freelist->numfree >=0 &&
Py_IS_TYPE(mp, &PyDict_Type)) {
dict_freelist->free_list[dict_freelist->numfree++] = mp;
freelist->items[freelist->numfree++] = mp;
OBJECT_STAT_INC(to_freelist);
}
else

View file

@ -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);
}
@ -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);
@ -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;
}

View file

@ -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);
}
@ -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);
}
@ -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

View file

@ -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
@ -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;
@ -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;