Skip to content

Commit fef3eed

Browse files
committed
Revert GC changes to "work_to_do" logic.
This reverts parts of the pythonGH-140262 change. The changes that affect the tuple untracking are left unchanged. Revert the changes to the calculation of the increment size, based on the "work_to_do" variable. This causes cyclic garbage to be collected more quickly. Revert also the change to test_gc.py, which was done because the expected GC collection was taking longer to happen. With the tuple untrack change, the performance regression as reported by bug pythonGH-139951 is still resolved (work_to_do changes are not required).
1 parent bc9e63d commit fef3eed

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

Lib/test/test_gc.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,11 +1493,10 @@ def callback(ignored):
14931493
# The free-threaded build doesn't have multiple generations, so
14941494
# just trigger a GC manually.
14951495
gc.collect()
1496-
assert not detector.gc_happened
14971496
while not detector.gc_happened:
14981497
i += 1
1499-
if i > 100000:
1500-
self.fail("gc didn't happen after 100000 iterations")
1498+
if i > 10000:
1499+
self.fail("gc didn't happen after 10000 iterations")
15011500
self.assertEqual(len(ouch), 0)
15021501
junk.append([]) # this will eventually trigger gc
15031502

@@ -1569,8 +1568,8 @@ def __del__(self):
15691568
gc.collect()
15701569
while not detector.gc_happened:
15711570
i += 1
1572-
if i > 50000:
1573-
self.fail("gc didn't happen after 50000 iterations")
1571+
if i > 10000:
1572+
self.fail("gc didn't happen after 10000 iterations")
15741573
self.assertEqual(len(ouch), 0)
15751574
junk.append([]) # this will eventually trigger gc
15761575

@@ -1587,8 +1586,8 @@ def test_indirect_calls_with_gc_disabled(self):
15871586
detector = GC_Detector()
15881587
while not detector.gc_happened:
15891588
i += 1
1590-
if i > 100000:
1591-
self.fail("gc didn't happen after 100000 iterations")
1589+
if i > 10000:
1590+
self.fail("gc didn't happen after 10000 iterations")
15921591
junk.append([]) # this will eventually trigger gc
15931592

15941593
try:
@@ -1598,11 +1597,11 @@ def test_indirect_calls_with_gc_disabled(self):
15981597
detector = GC_Detector()
15991598
while not detector.gc_happened:
16001599
i += 1
1601-
if i > 100000:
1600+
if i > 10000:
16021601
break
16031602
junk.append([]) # this may eventually trigger gc (if it is enabled)
16041603

1605-
self.assertEqual(i, 100001)
1604+
self.assertEqual(i, 10001)
16061605
finally:
16071606
gc.enable()
16081607

Python/gc.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,7 @@ assess_work_to_do(GCState *gcstate)
16441644
scale_factor = 2;
16451645
}
16461646
intptr_t new_objects = gcstate->young.count;
1647-
intptr_t max_heap_fraction = new_objects*2;
1647+
intptr_t max_heap_fraction = new_objects*3/2;
16481648
intptr_t heap_fraction = gcstate->heap_size / SCAN_RATE_DIVISOR / scale_factor;
16491649
if (heap_fraction > max_heap_fraction) {
16501650
heap_fraction = max_heap_fraction;
@@ -1659,9 +1659,6 @@ gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats)
16591659
GC_STAT_ADD(1, collections, 1);
16601660
GCState *gcstate = &tstate->interp->gc;
16611661
gcstate->work_to_do += assess_work_to_do(gcstate);
1662-
if (gcstate->work_to_do < 0) {
1663-
return;
1664-
}
16651662
untrack_tuples(&gcstate->young.head);
16661663
if (gcstate->phase == GC_PHASE_MARK) {
16671664
Py_ssize_t objects_marked = mark_at_start(tstate);
@@ -1705,6 +1702,7 @@ gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats)
17051702
gc_collect_region(tstate, &increment, &survivors, stats);
17061703
gc_list_merge(&survivors, visited);
17071704
assert(gc_list_is_empty(&increment));
1705+
gcstate->work_to_do += gcstate->heap_size / SCAN_RATE_DIVISOR / scale_factor;
17081706
gcstate->work_to_do -= increment_size;
17091707

17101708
if (gc_list_is_empty(not_visited)) {

0 commit comments

Comments
 (0)