summaryrefslogtreecommitdiffstats
path: root/Objects/dictobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'Objects/dictobject.c')
-rw-r--r--Objects/dictobject.c103
1 files changed, 19 insertions, 84 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index ca91da7..7310c3c 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -395,44 +395,6 @@ static int _PyObject_InlineValuesConsistencyCheck(PyObject *obj);
#include "clinic/dictobject.c.h"
-#ifdef WITH_FREELISTS
-static struct _Py_dict_freelist *
-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
-
-
-void
-_PyDict_ClearFreeList(struct _Py_object_freelists *freelists, int is_finalization)
-{
-#ifdef WITH_FREELISTS
- 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);
- }
- struct _Py_dictkeys_freelist *keys_freelist = &freelists->dictkeys;
- while (keys_freelist->numfree > 0) {
- PyMem_Free(keys_freelist->items[--keys_freelist->numfree]);
- }
- if (is_finalization) {
- freelist->numfree = -1;
- keys_freelist->numfree = -1;
- }
-#endif
-}
-
static inline Py_hash_t
unicode_get_hash(PyObject *o)
{
@@ -445,12 +407,12 @@ void
_PyDict_DebugMallocStats(FILE *out)
{
#ifdef WITH_FREELISTS
- 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();
+ _Py_FREELIST_SIZE(dicts),
+ sizeof(PyDictObject));
_PyDebugAllocatorStats(out, "free PyDictKeysObject",
- dictkeys_freelist->numfree, sizeof(PyDictKeysObject));
+ _Py_FREELIST_SIZE(dictkeys),
+ sizeof(PyDictKeysObject));
#endif
}
@@ -785,7 +747,6 @@ _PyDict_CheckConsistency(PyObject *op, int check_content)
static PyDictKeysObject*
new_keys_object(PyInterpreterState *interp, uint8_t log2_size, bool unicode)
{
- PyDictKeysObject *dk;
Py_ssize_t usable;
int log2_bytes;
size_t entry_size = unicode ? sizeof(PyDictUnicodeEntry) : sizeof(PyDictKeyEntry);
@@ -808,15 +769,11 @@ new_keys_object(PyInterpreterState *interp, uint8_t log2_size, bool unicode)
log2_bytes = log2_size + 2;
}
-#ifdef WITH_FREELISTS
- 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);
+ PyDictKeysObject *dk = NULL;
+ if (log2_size == PyDict_LOG_MINSIZE && unicode) {
+ dk = _Py_FREELIST_POP_MEM(dictkeys);
}
- else
-#endif
- {
+ if (dk == NULL) {
dk = PyMem_Malloc(sizeof(PyDictKeysObject)
+ ((size_t)1 << log2_bytes)
+ entry_size * usable);
@@ -852,18 +809,12 @@ free_keys_object(PyDictKeysObject *keys, bool use_qsbr)
return;
}
#endif
-#ifdef WITH_FREELISTS
- struct _Py_dictkeys_freelist *freelist = get_dictkeys_freelist();
- if (DK_LOG_SIZE(keys) == PyDict_LOG_MINSIZE
- && freelist->numfree < PyDict_MAXFREELIST
- && freelist->numfree >= 0
- && DK_IS_UNICODE(keys)) {
- freelist->items[freelist->numfree++] = keys;
- OBJECT_STAT_INC(to_freelist);
- return;
+ if (DK_LOG_SIZE(keys) == PyDict_LOG_MINSIZE && keys->dk_kind == DICT_KEYS_UNICODE) {
+ _Py_FREELIST_FREE(dictkeys, keys, PyMem_Free);
+ }
+ else {
+ PyMem_Free(keys);
}
-#endif
- PyMem_Free(keys);
}
static size_t
@@ -912,20 +863,9 @@ new_dict(PyInterpreterState *interp,
PyDictKeysObject *keys, PyDictValues *values,
Py_ssize_t used, int free_values_on_failure)
{
- PyDictObject *mp;
assert(keys != NULL);
-#ifdef WITH_FREELISTS
- 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);
- _Py_NewReference((PyObject *)mp);
- }
- else
-#endif
- {
+ PyDictObject *mp = _Py_FREELIST_POP(PyDictObject, dicts);
+ if (mp == NULL) {
mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
if (mp == NULL) {
dictkeys_decref(interp, keys, false);
@@ -935,6 +875,7 @@ new_dict(PyInterpreterState *interp,
return NULL;
}
}
+ assert(Py_IS_TYPE(mp, &PyDict_Type));
mp->ma_keys = keys;
mp->ma_values = values;
mp->ma_used = used;
@@ -3153,16 +3094,10 @@ dict_dealloc(PyObject *self)
assert(keys->dk_refcnt == 1 || keys == Py_EMPTY_KEYS);
dictkeys_decref(interp, keys, false);
}
-#ifdef WITH_FREELISTS
- struct _Py_dict_freelist *freelist = get_dict_freelist();
- if (freelist->numfree < PyDict_MAXFREELIST && freelist->numfree >=0 &&
- Py_IS_TYPE(mp, &PyDict_Type)) {
- freelist->items[freelist->numfree++] = mp;
- OBJECT_STAT_INC(to_freelist);
+ if (Py_IS_TYPE(mp, &PyDict_Type)) {
+ _Py_FREELIST_FREE(dicts, mp, Py_TYPE(mp)->tp_free);
}
- else
-#endif
- {
+ else {
Py_TYPE(mp)->tp_free((PyObject *)mp);
}
Py_TRASHCAN_END