Skip to content

Commit 5c5670e

Browse files
[3.14] gh-142048: Fix quadratically increasing GC delays (gh-142051) (gh-142166)
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 eb89286) Co-authored-by: Kevin Wang <[email protected]>
1 parent 7642070 commit 5c5670e

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
@@ -2134,7 +2134,19 @@ record_deallocation(PyThreadState *tstate)
21342134
gc->alloc_count--;
21352135
if (gc->alloc_count <= -LOCAL_ALLOC_COUNT_THRESHOLD) {
21362136
GCState *gcstate = &tstate->interp->gc;
2137-
_Py_atomic_add_int(&gcstate->young.count, (int)gc->alloc_count);
2137+
int count = _Py_atomic_load_int_relaxed(&gcstate->young.count);
2138+
int new_count;
2139+
do {
2140+
if (count == 0) {
2141+
break;
2142+
}
2143+
new_count = count + (int)gc->alloc_count;
2144+
if (new_count < 0) {
2145+
new_count = 0;
2146+
}
2147+
} while (!_Py_atomic_compare_exchange_int(&gcstate->young.count,
2148+
&count,
2149+
new_count));
21382150
gc->alloc_count = 0;
21392151
}
21402152
}

0 commit comments

Comments
 (0)