]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-142048: Fix quadratically increasing GC delays (gh-142051) (gh-142166)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 2 Dec 2025 00:30:11 +0000 (01:30 +0100)
committerGitHub <noreply@github.com>
Tue, 2 Dec 2025 00:30:11 +0000 (00:30 +0000)
The GC for the free threaded build would get slower with each collection due
to effectively double counting objects freed by the GC.
(cherry picked from commit eb892868b31322d7cf271bc25923e14b1f67ae38)

Co-authored-by: Kevin Wang <kevmo314@gmail.com>
Misc/NEWS.d/next/Core_and_Builtins/2025-12-01-20-41-26.gh-issue-142048.c2YosX.rst [new file with mode: 0644]
Python/gc_free_threading.c

diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-12-01-20-41-26.gh-issue-142048.c2YosX.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-12-01-20-41-26.gh-issue-142048.c2YosX.rst
new file mode 100644 (file)
index 0000000..1400dae
--- /dev/null
@@ -0,0 +1,2 @@
+Fix quadratically increasing garbage collection delays in free-threaded
+build.
index 94c77991f5f45b84464af98531f0a1960ea0794b..d096accb4371c1e49a98d7af9bf688c6083b4538 100644 (file)
@@ -2134,7 +2134,19 @@ record_deallocation(PyThreadState *tstate)
     gc->alloc_count--;
     if (gc->alloc_count <= -LOCAL_ALLOC_COUNT_THRESHOLD) {
         GCState *gcstate = &tstate->interp->gc;
-        _Py_atomic_add_int(&gcstate->young.count, (int)gc->alloc_count);
+        int count = _Py_atomic_load_int_relaxed(&gcstate->young.count);
+        int new_count;
+        do {
+            if (count == 0) {
+                break;
+            }
+            new_count = count + (int)gc->alloc_count;
+            if (new_count < 0) {
+                new_count = 0;
+            }
+        } while (!_Py_atomic_compare_exchange_int(&gcstate->young.count,
+                                                  &count,
+                                                  new_count));
         gc->alloc_count = 0;
     }
 }