]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40428: Remove PyTuple_ClearFreeList() function (GH-19769)
authorVictor Stinner <vstinner@python.org>
Wed, 29 Apr 2020 00:29:20 +0000 (02:29 +0200)
committerGitHub <noreply@github.com>
Wed, 29 Apr 2020 00:29:20 +0000 (02:29 +0200)
Remove the following function from the C API:

* PyAsyncGen_ClearFreeLists()
* PyContext_ClearFreeList()
* PyDict_ClearFreeList()
* PyFloat_ClearFreeList()
* PyFrame_ClearFreeList()
* PyList_ClearFreeList()
* PySet_ClearFreeList()
* PyTuple_ClearFreeList()

Make these functions private, move them to the internal C API and
change their return type to void.

Call explicitly PyGC_Collect() to free all free lists.

Note: PySet_ClearFreeList() did nothing.

21 files changed:
Doc/whatsnew/3.9.rst
Include/context.h
Include/cpython/dictobject.h
Include/cpython/frameobject.h
Include/cpython/listobject.h
Include/floatobject.h
Include/genobject.h
Include/internal/pycore_gc.h
Include/setobject.h
Include/tupleobject.h
Misc/NEWS.d/next/C API/2020-04-28-23-17-27.bpo-40428.rmtpru.rst [new file with mode: 0644]
Modules/gcmodule.c
Objects/dictobject.c
Objects/floatobject.c
Objects/frameobject.c
Objects/genobject.c
Objects/listobject.c
Objects/setobject.c
Objects/tupleobject.c
PC/python3.def
Python/context.c

index cb3afd593571a9536696e48a9bb6ba8022b03020..e26bd473e6189dd291f30cedf0a42d88b4c45fb8 100644 (file)
@@ -672,6 +672,19 @@ Build and C API Changes
   the garbage collector respectively. (Contributed by Pablo Galindo in
   :issue:`40241`.)
 
+* Remove the following functions from the C API. Call :c:func:`PyGC_Collect`
+  explicitly to free all free lists.
+  (Contributed by Victor Stinner in :issue:`40428`.)
+
+  * ``PyAsyncGen_ClearFreeLists()``
+  * ``PyContext_ClearFreeList()``
+  * ``PyDict_ClearFreeList()``
+  * ``PyFloat_ClearFreeList()``
+  * ``PyFrame_ClearFreeList()``
+  * ``PyList_ClearFreeList()``
+  * ``PySet_ClearFreeList()``
+  * ``PyTuple_ClearFreeList()``
+
 
 Deprecated
 ==========
index 619746d501efdc541cf30aa5ceeeddb3a45d49d5..4e5007089dd94bc1a50ca07340674deace6188bf 100644 (file)
@@ -73,9 +73,6 @@ PyAPI_FUNC(int) PyContextVar_Reset(PyObject *var, PyObject *token);
 PyAPI_FUNC(PyObject *) _PyContext_NewHamtForTests(void);
 
 
-PyAPI_FUNC(int) PyContext_ClearFreeList(void);
-
-
 #endif /* !Py_LIMITED_API */
 
 #ifdef __cplusplus
index 64c012a012b793bf5f5954ab67f880ae81cda138..e33a0d156fead2a90773025699fad9d0ab992558 100644 (file)
@@ -62,8 +62,6 @@ PyObject *_PyDict_Pop_KnownHash(PyObject *, PyObject *, Py_hash_t, PyObject *);
 PyObject *_PyDict_FromKeys(PyObject *, PyObject *, PyObject *);
 #define _PyDict_HasSplitTable(d) ((d)->ma_values != NULL)
 
-PyAPI_FUNC(int) PyDict_ClearFreeList(void);
-
 /* Like PyDict_Merge, but override can be 0, 1 or 2.  If override is 0,
    the first occurrence of a key wins, if override is 1, the last occurrence
    of a key wins, if override is 2, a KeyError with conflicting key as
index e819cefd13cbeb18972f6df595cef4deea21347d..e32efac594718160c399f1c1497ceffd7358d330 100644 (file)
@@ -75,8 +75,6 @@ PyAPI_FUNC(void) PyFrame_LocalsToFast(PyFrameObject *, int);
 PyAPI_FUNC(int) PyFrame_FastToLocalsWithError(PyFrameObject *f);
 PyAPI_FUNC(void) PyFrame_FastToLocals(PyFrameObject *);
 
-PyAPI_FUNC(int) PyFrame_ClearFreeList(void);
-
 PyAPI_FUNC(void) _PyFrame_DebugMallocStats(FILE *out);
 
 #ifdef __cplusplus
index 4b6f2f7741c1a7dadb52336e78ed1ec432b21d07..74fe3301a7ab719faedba246790415cb1305b164 100644 (file)
@@ -26,7 +26,6 @@ typedef struct {
 } PyListObject;
 
 PyAPI_FUNC(PyObject *) _PyList_Extend(PyListObject *, PyObject *);
-PyAPI_FUNC(int) PyList_ClearFreeList(void);
 PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out);
 
 /* Macro, trading safety for speed */
index 917dfcc26445cebc31bdabcf42e9b463d02311d2..e994aa8f29da484a1e4660c28d782d2324b93145 100644 (file)
@@ -100,9 +100,6 @@ PyAPI_FUNC(double) _PyFloat_Unpack2(const unsigned char *p, int le);
 PyAPI_FUNC(double) _PyFloat_Unpack4(const unsigned char *p, int le);
 PyAPI_FUNC(double) _PyFloat_Unpack8(const unsigned char *p, int le);
 
-/* free list api */
-PyAPI_FUNC(int) PyFloat_ClearFreeList(void);
-
 PyAPI_FUNC(void) _PyFloat_DebugMallocStats(FILE* out);
 
 /* Format the object based on the format_spec, as defined in PEP 3101
index a7393a9a8359232a76883232946f3dedc03276b0..8ffd15646f084e2aae375e2638d5abe7325e92f3 100644 (file)
@@ -91,8 +91,6 @@ PyAPI_FUNC(PyObject *) PyAsyncGen_New(PyFrameObject *,
 
 PyObject *_PyAsyncGenValueWrapperNew(PyObject *);
 
-int PyAsyncGen_ClearFreeLists(void);
-
 #endif
 
 #undef _PyGenObject_HEAD
index 62b8800e24998447f57f689d7fd1bdf07cae4e63..0511eea779a7e7dfa78d7676de3a3d1671a0fef7 100644 (file)
@@ -163,6 +163,16 @@ struct _gc_runtime_state {
 
 PyAPI_FUNC(void) _PyGC_InitState(struct _gc_runtime_state *);
 
+
+// Functions to clear types free lists
+extern void _PyFrame_ClearFreeList(void);
+extern void _PyTuple_ClearFreeList(void);
+extern void _PyFloat_ClearFreeList(void);
+extern void _PyList_ClearFreeList(void);
+extern void _PyDict_ClearFreeList(void);
+extern void _PyAsyncGen_ClearFreeLists(void);
+extern void _PyContext_ClearFreeList(void);
+
 #ifdef __cplusplus
 }
 #endif
index 05a097eba7f7da05c9370eb28519d9d527965437..119619ebe72994be28bc58d92fb3df81e849e0b9 100644 (file)
@@ -70,7 +70,6 @@ PyAPI_DATA(PyObject *) _PySet_Dummy;
 
 PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash);
 PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable);
-PyAPI_FUNC(int) PySet_ClearFreeList(void);
 
 #endif /* Section excluded by Py_LIMITED_API */
 
index d3504b0501f9ef5e1cb1c3d2919fc25e7e31cd1d..e796a320192c204466ffca35d27a32fd3d064f5c 100644 (file)
@@ -34,8 +34,6 @@ PyAPI_FUNC(int) PyTuple_SetItem(PyObject *, Py_ssize_t, PyObject *);
 PyAPI_FUNC(PyObject *) PyTuple_GetSlice(PyObject *, Py_ssize_t, Py_ssize_t);
 PyAPI_FUNC(PyObject *) PyTuple_Pack(Py_ssize_t, ...);
 
-PyAPI_FUNC(int) PyTuple_ClearFreeList(void);
-
 #ifndef Py_LIMITED_API
 #  define Py_CPYTHON_TUPLEOBJECT_H
 #  include  "cpython/tupleobject.h"
diff --git a/Misc/NEWS.d/next/C API/2020-04-28-23-17-27.bpo-40428.rmtpru.rst b/Misc/NEWS.d/next/C API/2020-04-28-23-17-27.bpo-40428.rmtpru.rst
new file mode 100644 (file)
index 0000000..f8710ef
--- /dev/null
@@ -0,0 +1,11 @@
+Remove the following functions from the C API. Call :c:func:`PyGC_Collect`
+explicitly to free all free lists.
+
+* ``PyAsyncGen_ClearFreeLists()``
+* ``PyContext_ClearFreeList()``
+* ``PyDict_ClearFreeList()``
+* ``PyFloat_ClearFreeList()``
+* ``PyFrame_ClearFreeList()``
+* ``PyList_ClearFreeList()``
+* ``PySet_ClearFreeList()``
+* ``PyTuple_ClearFreeList()``
index 5727820f09bbbcf2882e333b5a35ee0e3ac9b6f6..56dcb101e0005e55b0f1ca132b218d1c9f8c2cc8 100644 (file)
@@ -30,7 +30,6 @@
 #include "pycore_object.h"
 #include "pycore_pyerrors.h"
 #include "pycore_pystate.h"     // _PyThreadState_GET()
-#include "frameobject.h"        // PyFrame_ClearFreeList
 #include "pydtrace.h"
 #include "pytime.h"             // _PyTime_GetMonotonicClock()
 
@@ -1026,14 +1025,13 @@ delete_garbage(PyThreadState *tstate, GCState *gcstate,
 static void
 clear_freelists(void)
 {
-    (void)PyFrame_ClearFreeList();
-    (void)PyTuple_ClearFreeList();
-    (void)PyFloat_ClearFreeList();
-    (void)PyList_ClearFreeList();
-    (void)PyDict_ClearFreeList();
-    (void)PySet_ClearFreeList();
-    (void)PyAsyncGen_ClearFreeLists();
-    (void)PyContext_ClearFreeList();
+    _PyFrame_ClearFreeList();
+    _PyTuple_ClearFreeList();
+    _PyFloat_ClearFreeList();
+    _PyList_ClearFreeList();
+    _PyDict_ClearFreeList();
+    _PyAsyncGen_ClearFreeLists();
+    _PyContext_ClearFreeList();
 }
 
 // Show stats for objects in each generations
index 8f9d4e7b7314042e20ae14d10f9d2588372c71f3..9c35f3c3f14d01f09f0256356712d20335a88a0e 100644 (file)
@@ -257,20 +257,17 @@ static int numfreekeys = 0;
 
 #include "clinic/dictobject.c.h"
 
-int
-PyDict_ClearFreeList(void)
+void
+_PyDict_ClearFreeList(void)
 {
-    PyDictObject *op;
-    int ret = numfree + numfreekeys;
     while (numfree) {
-        op = free_list[--numfree];
+        PyDictObject *op = free_list[--numfree];
         assert(PyDict_CheckExact(op));
         PyObject_GC_Del(op);
     }
     while (numfreekeys) {
         PyObject_FREE(keys_free_list[--numfreekeys]);
     }
-    return ret;
 }
 
 /* Print summary info about the state of the optimized allocator */
@@ -285,7 +282,7 @@ _PyDict_DebugMallocStats(FILE *out)
 void
 _PyDict_Fini(void)
 {
-    PyDict_ClearFreeList();
+    _PyDict_ClearFreeList();
 }
 
 #define DK_SIZE(dk) ((dk)->dk_size)
index 04f968e56b142761cd02f6ddfc72f791576b8b41..faa02f2f05795c1b06e6cc32f3eb89d40cb9fb69 100644 (file)
@@ -1998,25 +1998,22 @@ _PyFloat_Init(void)
     return 1;
 }
 
-int
-PyFloat_ClearFreeList(void)
+void
+_PyFloat_ClearFreeList(void)
 {
     PyFloatObject *f = free_list, *next;
-    int i = numfree;
-    while (f) {
+    for (; f; f = next) {
         next = (PyFloatObject*) Py_TYPE(f);
         PyObject_FREE(f);
-        f = next;
     }
     free_list = NULL;
     numfree = 0;
-    return i;
 }
 
 void
 _PyFloat_Fini(void)
 {
-    (void)PyFloat_ClearFreeList();
+    _PyFloat_ClearFreeList();
 }
 
 /* Print summary info about the state of the optimized allocator */
index 533186bc046f0f748053c0da7f413a25f5dcfe4d..6d288b5b059d7ae500fe9362c411aeed93f94933 100644 (file)
@@ -1200,11 +1200,9 @@ PyFrame_LocalsToFast(PyFrameObject *f, int clear)
 }
 
 /* Clear out the free list */
-int
-PyFrame_ClearFreeList(void)
+void
+_PyFrame_ClearFreeList(void)
 {
-    int freelist_size = numfree;
-
     while (free_list != NULL) {
         PyFrameObject *f = free_list;
         free_list = free_list->f_back;
@@ -1212,13 +1210,12 @@ PyFrame_ClearFreeList(void)
         --numfree;
     }
     assert(numfree == 0);
-    return freelist_size;
 }
 
 void
 _PyFrame_Fini(void)
 {
-    (void)PyFrame_ClearFreeList();
+    _PyFrame_ClearFreeList();
 }
 
 /* Print summary info about the state of the optimized allocator */
index 071def8a25aae9924151079e9c9c8438bc98ff9c..6e36690b65148ab1f31a9b6d8d9a70a805bae0bb 100644 (file)
@@ -1429,11 +1429,9 @@ PyAsyncGen_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
 }
 
 
-int
-PyAsyncGen_ClearFreeLists(void)
+void
+_PyAsyncGen_ClearFreeLists(void)
 {
-    int ret = ag_value_freelist_free + ag_asend_freelist_free;
-
     while (ag_value_freelist_free) {
         _PyAsyncGenWrappedValue *o;
         o = ag_value_freelist[--ag_value_freelist_free];
@@ -1447,14 +1445,12 @@ PyAsyncGen_ClearFreeLists(void)
         assert(Py_IS_TYPE(o, &_PyAsyncGenASend_Type));
         PyObject_GC_Del(o);
     }
-
-    return ret;
 }
 
 void
 _PyAsyncGen_Fini(void)
 {
-    PyAsyncGen_ClearFreeLists();
+    _PyAsyncGen_ClearFreeLists();
 }
 
 
index 7d2f006617ba50e165847544eb1b464e20c8c2ce..904bea317c9da8a44ffef5390b5f0cc3196bb0b5 100644 (file)
@@ -103,23 +103,20 @@ list_preallocate_exact(PyListObject *self, Py_ssize_t size)
 static PyListObject *free_list[PyList_MAXFREELIST];
 static int numfree = 0;
 
-int
-PyList_ClearFreeList(void)
+void
+_PyList_ClearFreeList(void)
 {
-    PyListObject *op;
-    int ret = numfree;
     while (numfree) {
-        op = free_list[--numfree];
+        PyListObject *op = free_list[--numfree];
         assert(PyList_CheckExact(op));
         PyObject_GC_Del(op);
     }
-    return ret;
 }
 
 void
 _PyList_Fini(void)
 {
-    PyList_ClearFreeList();
+    _PyList_ClearFreeList();
 }
 
 /* Print summary info about the state of the optimized allocator */
index 8452546008bd1eafd67d89102e8e582f5079d8da..bbe013bcfac748a5a9efb160243cff87a6d03995 100644 (file)
@@ -2384,12 +2384,6 @@ PySet_Add(PyObject *anyset, PyObject *key)
     return set_add_key((PySetObject *)anyset, key);
 }
 
-int
-PySet_ClearFreeList(void)
-{
-    return 0;
-}
-
 void
 _PySet_Fini(void)
 {
index b65b8abc2806df0384e96c2ac79a35df66ad7c45..f8648d24f1c876d5a6dba4ffcc6ccee4742d80d0 100644 (file)
@@ -955,26 +955,22 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
     return 0;
 }
 
-int
-PyTuple_ClearFreeList(void)
+void
+_PyTuple_ClearFreeList(void)
 {
-    int freelist_size = 0;
 #if PyTuple_MAXSAVESIZE > 0
-    int i;
-    for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
-        PyTupleObject *p, *q;
-        p = free_list[i];
-        freelist_size += numfree[i];
+    for (Py_ssize_t i = 1; i < PyTuple_MAXSAVESIZE; i++) {
+        PyTupleObject *p = free_list[i];
         free_list[i] = NULL;
         numfree[i] = 0;
         while (p) {
-            q = p;
+            PyTupleObject *q = p;
             p = (PyTupleObject *)(p->ob_item[0]);
             PyObject_GC_Del(q);
         }
     }
+    // the empty tuple singleton is only cleared by _PyTuple_Fini()
 #endif
-    return freelist_size;
 }
 
 void
@@ -985,7 +981,7 @@ _PyTuple_Fini(void)
      * rely on the fact that an empty tuple is a singleton. */
     Py_CLEAR(free_list[0]);
 
-    (void)PyTuple_ClearFreeList();
+    _PyTuple_ClearFreeList();
 #endif
 }
 
index 083384e30f648ff8fbf5d6684647222e43fa9ac6..1521ac738c0b33a7824562ec95b9deef697b1044 100644 (file)
@@ -35,7 +35,6 @@ EXPORTS
   PyBytes_Size=python39.PyBytes_Size
   PyBytes_Type=python39.PyBytes_Type DATA
   PyCFunction_Call=python39.PyCFunction_Call
-  PyCFunction_ClearFreeList=python39.PyCFunction_ClearFreeList
   PyCFunction_GetFlags=python39.PyCFunction_GetFlags
   PyCFunction_GetFunction=python39.PyCFunction_GetFunction
   PyCFunction_GetSelf=python39.PyCFunction_GetSelf
@@ -584,7 +583,6 @@ EXPORTS
   PyTraceBack_Print=python39.PyTraceBack_Print
   PyTraceBack_Type=python39.PyTraceBack_Type DATA
   PyTupleIter_Type=python39.PyTupleIter_Type DATA
-  PyTuple_ClearFreeList=python39.PyTuple_ClearFreeList
   PyTuple_GetItem=python39.PyTuple_GetItem
   PyTuple_GetSlice=python39.PyTuple_GetSlice
   PyTuple_New=python39.PyTuple_New
index f0217f280180a1e480d83040ce4ec6cd2b50c4d4..bacc7010c458e1f927c24bbb467feaeb53eea390 100644 (file)
@@ -1270,18 +1270,15 @@ get_token_missing(void)
 ///////////////////////////
 
 
-int
-PyContext_ClearFreeList(void)
+void
+_PyContext_ClearFreeList(void)
 {
-    int size = ctx_freelist_len;
-    while (ctx_freelist_len) {
+    for (; ctx_freelist_len; ctx_freelist_len--) {
         PyContext *ctx = ctx_freelist;
         ctx_freelist = (PyContext *)ctx->ctx_weakreflist;
         ctx->ctx_weakreflist = NULL;
         PyObject_GC_Del(ctx);
-        ctx_freelist_len--;
     }
-    return size;
 }
 
 
@@ -1289,8 +1286,8 @@ void
 _PyContext_Fini(void)
 {
     Py_CLEAR(_token_missing);
-    (void)PyContext_ClearFreeList();
-    (void)_PyHamt_Fini();
+    _PyContext_ClearFreeList();
+    _PyHamt_Fini();
 }