https://github.com/python/cpython/commit/eb892868b31322d7cf271bc25923e14b1f67ae38
commit: eb892868b31322d7cf271bc25923e14b1f67ae38
branch: main
author: Kevin Wang <[email protected]>
committer: colesbury <[email protected]>
date: 2025-12-01T19:04:47-05:00
summary:

gh-142048: Fix quadratically increasing GC delays (gh-142051)

The GC for the free threaded build would get slower with each collection due
to effectively double counting objects freed by the GC.

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-12-01-20-41-26.gh-issue-142048.c2YosX.rst
M 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
index 00000000000000..1400dae13ffe32
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-12-01-20-41-26.gh-issue-142048.c2YosX.rst
@@ -0,0 +1,2 @@
+Fix quadratically increasing garbage collection delays in free-threaded
+build.
diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c
index 1717603b947f90..e672e870db2f27 100644
--- a/Python/gc_free_threading.c
+++ b/Python/gc_free_threading.c
@@ -2210,7 +2210,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;
     }
 }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to