Skip to content

Commit 73dbc1c

Browse files
committed
Move gc_should_collect_mem_usage() call.
It's probably better to call this inside of gc_collect_main(). That way, we are not doing the STW from inside _PyObject_GC_Link() function. This should have no significant performance impact since we hit this only after the young object count hits the threshold.
1 parent 609ed97 commit 73dbc1c

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

Python/gc_free_threading.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,11 @@ gc_should_collect_mem_usage(PyThreadState *tstate)
20482048
GCState *gcstate = &interp->gc;
20492049
int threshold = gcstate->young.threshold;
20502050

2051+
if (gcstate->old[0].threshold == 0) {
2052+
// A few tests rely on immediate scheduling of the GC so we ignore the
2053+
// extra conditions if generations[1].threshold is set to zero.
2054+
return true;
2055+
}
20512056
if (gcstate->deferred_count > threshold * 40) {
20522057
// Too many new container objects since last GC, even though memory
20532058
// use might not have increased much. This avoids resource
@@ -2096,7 +2101,7 @@ gc_should_collect(PyThreadState *tstate)
20962101
// objects.
20972102
return false;
20982103
}
2099-
return gc_should_collect_mem_usage(tstate);
2104+
return true;
21002105
}
21012106

21022107
static void
@@ -2311,6 +2316,10 @@ gc_collect_main(PyThreadState *tstate, int generation, _PyGC_Reason reason)
23112316
_Py_atomic_store_int(&gcstate->collecting, 0);
23122317
return 0;
23132318
}
2319+
if (reason == _Py_GC_REASON_HEAP && !gc_should_collect_mem_usage(tstate)) {
2320+
_Py_atomic_store_int(&gcstate->collecting, 0);
2321+
return 0;
2322+
}
23142323
gcstate->frame = tstate->current_frame;
23152324

23162325
assert(generation >= 0 && generation < NUM_GENERATIONS);

0 commit comments

Comments
 (0)