]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-111968: Use per-thread freelists for float in free-threading (gh-113886)
authorDonghee Na <donghee.na@python.org>
Wed, 10 Jan 2024 15:47:13 +0000 (00:47 +0900)
committerGitHub <noreply@github.com>
Wed, 10 Jan 2024 15:47:13 +0000 (15:47 +0000)
Include/internal/pycore_floatobject.h
Include/internal/pycore_freelist.h
Include/internal/pycore_gc.h
Include/internal/pycore_interp.h
Objects/floatobject.c
Python/gc_free_threading.c
Python/gc_gil.c
Python/pylifecycle.c
Python/pystate.c

index 4e5474841bc25da8b78215093f04609157d80181..038578e1f9680a65089d76af5c4613476d3561ea 100644 (file)
@@ -8,14 +8,14 @@ extern "C" {
 #  error "this header requires Py_BUILD_CORE define"
 #endif
 
-
+#include "pycore_freelist.h"      // _PyFreeListState
 #include "pycore_unicodeobject.h" // _PyUnicodeWriter
 
 /* runtime lifecycle */
 
 extern void _PyFloat_InitState(PyInterpreterState *);
 extern PyStatus _PyFloat_InitTypes(PyInterpreterState *);
-extern void _PyFloat_Fini(PyInterpreterState *);
+extern void _PyFloat_Fini(_PyFreeListState *);
 extern void _PyFloat_FiniType(PyInterpreterState *);
 
 
@@ -33,24 +33,7 @@ struct _Py_float_runtime_state {
 };
 
 
-#ifndef WITH_FREELISTS
-// without freelists
-#  define PyFloat_MAXFREELIST 0
-#endif
-
-#ifndef PyFloat_MAXFREELIST
-#  define PyFloat_MAXFREELIST   100
-#endif
 
-struct _Py_float_state {
-#if PyFloat_MAXFREELIST > 0
-    /* Special free list
-       free_list is a singly-linked list of available PyFloatObjects,
-       linked via abuse of their ob_type members. */
-    int numfree;
-    PyFloatObject *free_list;
-#endif
-};
 
 void _PyFloat_ExactDealloc(PyObject *op);
 
index b725986528d864dbe7c606ba39e1885d1f1affcb..d9619218b8dada5fb5431f0853952cf9aead0260 100644 (file)
@@ -8,24 +8,34 @@ extern "C" {
 #  error "this header requires Py_BUILD_CORE define"
 #endif
 
-#ifndef WITH_FREELISTS
-// without freelists
-#  define PyList_MAXFREELIST 0
-#endif
-
-/* Empty list reuse scheme to save calls to malloc and free */
-#ifndef PyList_MAXFREELIST
+#ifdef WITH_FREELISTS
+// with freelists
 #  define PyList_MAXFREELIST 80
+#  define PyFloat_MAXFREELIST 100
+#else
+#  define PyList_MAXFREELIST 0
+#  define PyFloat_MAXFREELIST 0
 #endif
 
 struct _Py_list_state {
-#if PyList_MAXFREELIST > 0
+#ifdef WITH_FREELISTS
     PyListObject *free_list[PyList_MAXFREELIST];
     int numfree;
 #endif
 };
 
+struct _Py_float_state {
+#ifdef WITH_FREELISTS
+    /* Special free list
+       free_list is a singly-linked list of available PyFloatObjects,
+       linked via abuse of their ob_type members. */
+    int numfree;
+    PyFloatObject *free_list;
+#endif
+};
+
 typedef struct _Py_freelist_state {
+    struct _Py_float_state float_state;
     struct _Py_list_state list;
 } _PyFreeListState;
 
index 5d90d3a7f865da9073414782100e7e223e583ad9..2a0730eebb8db75eab51c879fae01c3ff7f0993c 100644 (file)
@@ -243,7 +243,7 @@ extern PyObject *_PyGC_GetReferrers(PyInterpreterState *interp, PyObject *objs);
 extern void _PyGC_ClearAllFreeLists(PyInterpreterState *interp);
 extern void _Py_ClearFreeLists(_PyFreeListState *state, int is_finalization);
 extern void _PyTuple_ClearFreeList(PyInterpreterState *interp);
-extern void _PyFloat_ClearFreeList(PyInterpreterState *interp);
+extern void _PyFloat_ClearFreeList(_PyFreeListState *state, int is_finalization);
 extern void _PyList_ClearFreeList(_PyFreeListState *state, int is_finalization);
 extern void _PyDict_ClearFreeList(PyInterpreterState *interp);
 extern void _PyAsyncGen_ClearFreeLists(PyInterpreterState *interp);
index 4d49fa2a51b88cb2a47aefef91a8e5de723ee867..dadc8e3b91a75dce5fb47d875ec78a31bfa817a6 100644 (file)
@@ -184,7 +184,6 @@ struct _is {
 #endif
     struct _py_object_state object_state;
     struct _Py_unicode_state unicode;
-    struct _Py_float_state float_state;
     struct _Py_long_state long_state;
     struct _dtoa_state dtoa;
     struct _py_func_state func_state;
index 364cf1553bb5d47a7aea5702ba211c446577eb29..f1a09c0a94f4a616cdfb5116dbfb5febd76c4737 100644 (file)
@@ -26,17 +26,13 @@ class float "PyObject *" "&PyFloat_Type"
 
 #include "clinic/floatobject.c.h"
 
-#ifndef PyFloat_MAXFREELIST
-#  define PyFloat_MAXFREELIST   100
-#endif
-
-
-#if PyFloat_MAXFREELIST > 0
+#ifdef WITH_FREELISTS
 static struct _Py_float_state *
 get_float_state(void)
 {
-    PyInterpreterState *interp = _PyInterpreterState_GET();
-    return &interp->float_state;
+    _PyFreeListState *state = _PyFreeListState_GET();
+    assert(state != NULL);
+    return &state->float_state;
 }
 #endif
 
@@ -132,7 +128,7 @@ PyObject *
 PyFloat_FromDouble(double fval)
 {
     PyFloatObject *op;
-#if PyFloat_MAXFREELIST > 0
+#ifdef WITH_FREELISTS
     struct _Py_float_state *state = get_float_state();
     op = state->free_list;
     if (op != NULL) {
@@ -252,13 +248,9 @@ _PyFloat_ExactDealloc(PyObject *obj)
 {
     assert(PyFloat_CheckExact(obj));
     PyFloatObject *op = (PyFloatObject *)obj;
-#if PyFloat_MAXFREELIST > 0
+#ifdef WITH_FREELISTS
     struct _Py_float_state *state = get_float_state();
-#ifdef Py_DEBUG
-    // float_dealloc() must not be called after _PyFloat_Fini()
-    assert(state->numfree != -1);
-#endif
-    if (state->numfree >= PyFloat_MAXFREELIST)  {
+    if (state->numfree >= PyFloat_MAXFREELIST || state->numfree < 0) {
         PyObject_Free(op);
         return;
     }
@@ -275,7 +267,7 @@ static void
 float_dealloc(PyObject *op)
 {
     assert(PyFloat_Check(op));
-#if PyFloat_MAXFREELIST > 0
+#ifdef WITH_FREELISTS
     if (PyFloat_CheckExact(op)) {
         _PyFloat_ExactDealloc(op);
     }
@@ -2002,10 +1994,10 @@ _PyFloat_InitTypes(PyInterpreterState *interp)
 }
 
 void
-_PyFloat_ClearFreeList(PyInterpreterState *interp)
+_PyFloat_ClearFreeList(_PyFreeListState *freelist_state, int is_finalization)
 {
-#if PyFloat_MAXFREELIST > 0
-    struct _Py_float_state *state = &interp->float_state;
+#ifdef WITH_FREELISTS
+    struct _Py_float_state *state = &freelist_state->float_state;
     PyFloatObject *f = state->free_list;
     while (f != NULL) {
         PyFloatObject *next = (PyFloatObject*) Py_TYPE(f);
@@ -2013,18 +2005,19 @@ _PyFloat_ClearFreeList(PyInterpreterState *interp)
         f = next;
     }
     state->free_list = NULL;
-    state->numfree = 0;
+    if (is_finalization) {
+        state->numfree = -1;
+    }
+    else {
+        state->numfree = 0;
+    }
 #endif
 }
 
 void
-_PyFloat_Fini(PyInterpreterState *interp)
+_PyFloat_Fini(_PyFreeListState *state)
 {
-    _PyFloat_ClearFreeList(interp);
-#if defined(Py_DEBUG) && PyFloat_MAXFREELIST > 0
-    struct _Py_float_state *state = &interp->float_state;
-    state->numfree = -1;
-#endif
+    _PyFloat_ClearFreeList(state, 1);
 }
 
 void
@@ -2037,7 +2030,7 @@ _PyFloat_FiniType(PyInterpreterState *interp)
 void
 _PyFloat_DebugMallocStats(FILE *out)
 {
-#if PyFloat_MAXFREELIST > 0
+#ifdef WITH_FREELISTS
     struct _Py_float_state *state = get_float_state();
     _PyDebugAllocatorStats(out,
                            "free PyFloatObject",
index aea272840f950dbc00e368d9792086acd5070d6c..c19893a39c161396f199acd88b3adacb2e10abc8 100644 (file)
@@ -15,7 +15,6 @@ void
 _PyGC_ClearAllFreeLists(PyInterpreterState *interp)
 {
     _PyTuple_ClearFreeList(interp);
-    _PyFloat_ClearFreeList(interp);
     _PyDict_ClearFreeList(interp);
     _PyAsyncGen_ClearFreeLists(interp);
     _PyContext_ClearFreeList(interp);
index b0961cdbee904f9e12d7a366c936dc827401509d..c8ca397be7ae585bc0e5e474e07f4e8f35eafac5 100644 (file)
@@ -12,7 +12,6 @@ void
 _PyGC_ClearAllFreeLists(PyInterpreterState *interp)
 {
     _PyTuple_ClearFreeList(interp);
-    _PyFloat_ClearFreeList(interp);
     _PyDict_ClearFreeList(interp);
     _PyAsyncGen_ClearFreeLists(interp);
     _PyContext_ClearFreeList(interp);
index bd6475f99e464bbc4efbd9f9dd07628c4b62dade..6468e72eaad5640e27bd400d16cf00d6a43c476c 100644 (file)
@@ -1757,10 +1757,10 @@ finalize_interp_types(PyInterpreterState *interp)
     _PySlice_Fini(interp);
 
     _PyUnicode_Fini(interp);
-    _PyFloat_Fini(interp);
 
     _PyFreeListState *state = _PyFreeListState_GET();
     _PyList_Fini(state);
+    _PyFloat_Fini(state);
 
 #ifdef Py_DEBUG
     _PyStaticObjects_CheckRefcnt(interp);
index ddd57f75f3f3633c5797cebdcb6d82251ca45b3e..683e29277b4675b49ad2caa3f5e38d3fea9068a2 100644 (file)
@@ -1458,6 +1458,7 @@ clear_datastack(PyThreadState *tstate)
 void
 _Py_ClearFreeLists(_PyFreeListState *state, int is_finalization)
 {
+    _PyFloat_ClearFreeList(state, is_finalization);
     _PyList_ClearFreeList(state, is_finalization);
 }