Skip to content

Commit 9a812cb

Browse files
committed
Issue #13389: Full garbage collection passes now clear the freelists for
list and dict objects. They already cleared other freelists in the interpreter.
1 parent d8b9ae6 commit 9a812cb

File tree

8 files changed

+44
-6
lines changed

8 files changed

+44
-6
lines changed

Doc/c-api/dict.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,10 @@ Dictionary Objects
209209
for key, value in seq2:
210210
if override or key not in a:
211211
a[key] = value
212+
213+
214+
.. c:function:: int PyDict_ClearFreeList()
215+
216+
Clear the free list. Return the total number of freed items.
217+
218+
.. versionadded:: 3.3

Doc/c-api/list.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,10 @@ List Objects
142142
143143
Return a new tuple object containing the contents of *list*; equivalent to
144144
``tuple(list)``.
145+
146+
147+
.. c:function:: int PyList_ClearFreeList()
148+
149+
Clear the free list. Return the total number of freed items.
150+
151+
.. versionadded:: 3.3

Include/dictobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ PyAPI_FUNC(int) _PyDict_Contains(PyObject *mp, PyObject *key, Py_hash_t hash);
129129
PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused);
130130
PyAPI_FUNC(void) _PyDict_MaybeUntrack(PyObject *mp);
131131
PyAPI_FUNC(int) _PyDict_HasOnlyStringKeys(PyObject *mp);
132+
133+
PyAPI_FUNC(int) PyDict_ClearFreeList(void);
132134
#endif
133135

134136
/* PyDict_Update(mp, other) is equivalent to PyDict_Merge(mp, other, 1). */

Include/listobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ PyAPI_FUNC(int) PyList_Reverse(PyObject *);
6262
PyAPI_FUNC(PyObject *) PyList_AsTuple(PyObject *);
6363
#ifndef Py_LIMITED_API
6464
PyAPI_FUNC(PyObject *) _PyList_Extend(PyListObject *, PyObject *);
65+
66+
PyAPI_FUNC(int) PyList_ClearFreeList(void);
6567
#endif
6668

6769
/* Macro, trading safety for speed */

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.3 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #13389: Full garbage collection passes now clear the freelists for
14+
list and dict objects. They already cleared other freelists in the
15+
interpreter.
16+
1317
- Issue #13327: Remove the need for an explicit None as the second argument
1418
to os.utime, os.lutimes, os.futimes, os.futimens, os.futimesat, in
1519
order to update to the current time. Also added keyword argument

Modules/gcmodule.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,8 @@ clear_freelists(void)
762762
(void)PyTuple_ClearFreeList();
763763
(void)PyUnicode_ClearFreeList();
764764
(void)PyFloat_ClearFreeList();
765+
(void)PyList_ClearFreeList();
766+
(void)PyDict_ClearFreeList();
765767
}
766768

767769
static double

Objects/dictobject.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,23 @@ show_track(void)
217217
static PyDictObject *free_list[PyDict_MAXFREELIST];
218218
static int numfree = 0;
219219

220-
void
221-
PyDict_Fini(void)
220+
int
221+
PyDict_ClearFreeList(void)
222222
{
223223
PyDictObject *op;
224-
224+
int ret = numfree;
225225
while (numfree) {
226226
op = free_list[--numfree];
227227
assert(PyDict_CheckExact(op));
228228
PyObject_GC_Del(op);
229229
}
230+
return ret;
231+
}
232+
233+
void
234+
PyDict_Fini(void)
235+
{
236+
PyDict_ClearFreeList();
230237
}
231238

232239
PyObject *

Objects/listobject.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,23 @@ show_alloc(void)
9797
static PyListObject *free_list[PyList_MAXFREELIST];
9898
static int numfree = 0;
9999

100-
void
101-
PyList_Fini(void)
100+
int
101+
PyList_ClearFreeList(void)
102102
{
103103
PyListObject *op;
104-
104+
int ret = numfree;
105105
while (numfree) {
106106
op = free_list[--numfree];
107107
assert(PyList_CheckExact(op));
108108
PyObject_GC_Del(op);
109109
}
110+
return ret;
111+
}
112+
113+
void
114+
PyList_Fini(void)
115+
{
116+
PyList_ClearFreeList();
110117
}
111118

112119
PyObject *

0 commit comments

Comments
 (0)