Skip to content

Commit 01393ff

Browse files
colesburykevmo314
andauthored
[3.13] gh-142048: Fix quadratically increasing GC delays (gh-142051) (#142167)
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 <kevmo314@gmail.com>
1 parent 8d920ac commit 01393ff

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
@@ -1100,7 +1100,19 @@ record_deallocation(PyThreadState *tstate)
11001100
gc->alloc_count--;
11011101
if (gc->alloc_count <= -LOCAL_ALLOC_COUNT_THRESHOLD) {
11021102
GCState *gcstate = &tstate->interp->gc;
1103-
_Py_atomic_add_int(&gcstate->generations[0].count, (int)gc->alloc_count);
1103+
int count = _Py_atomic_load_int_relaxed(&gcstate->generations[0].count);
1104+
int new_count;
1105+
do {
1106+
if (count == 0) {
1107+
break;
1108+
}
1109+
new_count = count + (int)gc->alloc_count;
1110+
if (new_count < 0) {
1111+
new_count = 0;
1112+
}
1113+
} while (!_Py_atomic_compare_exchange_int(&gcstate->generations[0].count,
1114+
&count,
1115+
new_count));
11041116
gc->alloc_count = 0;
11051117
}
11061118
}

0 commit comments

Comments
 (0)