Skip to content

Commit eb89286

Browse files
authored
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.
1 parent e32c975 commit eb89286

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix quadratically increasing garbage collection delays in free-threaded
2+
build.

‎Python/gc_free_threading.c‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2210,7 +2210,19 @@ record_deallocation(PyThreadState *tstate)
22102210
gc->alloc_count--;
22112211
if (gc->alloc_count <= -LOCAL_ALLOC_COUNT_THRESHOLD) {
22122212
GCState *gcstate = &tstate->interp->gc;
2213-
_Py_atomic_add_int(&gcstate->young.count, (int)gc->alloc_count);
2213+
int count = _Py_atomic_load_int_relaxed(&gcstate->young.count);
2214+
int new_count;
2215+
do {
2216+
if (count == 0) {
2217+
break;
2218+
}
2219+
new_count = count + (int)gc->alloc_count;
2220+
if (new_count < 0) {
2221+
new_count = 0;
2222+
}
2223+
} while (!_Py_atomic_compare_exchange_int(&gcstate->young.count,
2224+
&count,
2225+
new_count));
22142226
gc->alloc_count = 0;
22152227
}
22162228
}

0 commit comments

Comments
 (0)