Skip to content

Commit a9a2252

Browse files
naschemesergey-miryanov
authored andcommitted
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 57e0d17 commit a9a2252

2 files changed

Lines changed: 10 additions & 13 deletions

File tree

‎Lib/test/test_gc.py‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,11 +1417,10 @@ def callback(ignored):
14171417
# The free-threaded build doesn't have multiple generations, so
14181418
# just trigger a GC manually.
14191419
gc.collect()
1420-
assert not detector.gc_happened
14211420
while not detector.gc_happened:
14221421
i += 1
1423-
if i > 100000:
1424-
self.fail("gc didn't happen after 100000 iterations")
1422+
if i > 10000:
1423+
self.fail("gc didn't happen after 10000 iterations")
14251424
self.assertEqual(len(ouch), 0)
14261425
junk.append([]) # this will eventually trigger gc
14271426

@@ -1492,8 +1491,8 @@ def __del__(self):
14921491
gc.collect()
14931492
while not detector.gc_happened:
14941493
i += 1
1495-
if i > 50000:
1496-
self.fail("gc didn't happen after 50000 iterations")
1494+
if i > 10000:
1495+
self.fail("gc didn't happen after 10000 iterations")
14971496
self.assertEqual(len(ouch), 0)
14981497
junk.append([]) # this will eventually trigger gc
14991498

@@ -1510,8 +1509,8 @@ def test_indirect_calls_with_gc_disabled(self):
15101509
detector = GC_Detector()
15111510
while not detector.gc_happened:
15121511
i += 1
1513-
if i > 100000:
1514-
self.fail("gc didn't happen after 100000 iterations")
1512+
if i > 10000:
1513+
self.fail("gc didn't happen after 10000 iterations")
15151514
junk.append([]) # this will eventually trigger gc
15161515

15171516
try:
@@ -1521,11 +1520,11 @@ def test_indirect_calls_with_gc_disabled(self):
15211520
detector = GC_Detector()
15221521
while not detector.gc_happened:
15231522
i += 1
1524-
if i > 100000:
1523+
if i > 10000:
15251524
break
15261525
junk.append([]) # this may eventually trigger gc (if it is enabled)
15271526

1528-
self.assertEqual(i, 100001)
1527+
self.assertEqual(i, 10001)
15291528
finally:
15301529
gc.enable()
15311530

‎Python/gc.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,7 @@ assess_work_to_do(GCState *gcstate)
15901590
scale_factor = 2;
15911591
}
15921592
intptr_t new_objects = gcstate->young.count;
1593-
intptr_t max_heap_fraction = new_objects*2;
1593+
intptr_t max_heap_fraction = new_objects*3/2;
15941594
intptr_t heap_fraction = gcstate->heap_size / SCAN_RATE_DIVISOR / scale_factor;
15951595
if (heap_fraction > max_heap_fraction) {
15961596
heap_fraction = max_heap_fraction;
@@ -1605,9 +1605,6 @@ gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats)
16051605
GC_STAT_ADD(1, collections, 1);
16061606
GCState *gcstate = &tstate->interp->gc;
16071607
gcstate->work_to_do += assess_work_to_do(gcstate);
1608-
if (gcstate->work_to_do < 0) {
1609-
return;
1610-
}
16111608
untrack_tuples(&gcstate->young.head);
16121609
if (gcstate->phase == GC_PHASE_MARK) {
16131610
Py_ssize_t objects_marked = mark_at_start(tstate);
@@ -1650,6 +1647,7 @@ gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats)
16501647
gc_collect_region(tstate, &increment, &survivors, stats);
16511648
gc_list_merge(&survivors, visited);
16521649
assert(gc_list_is_empty(&increment));
1650+
gcstate->work_to_do += gcstate->heap_size / SCAN_RATE_DIVISOR / scale_factor;
16531651
gcstate->work_to_do -= increment_size;
16541652

16551653
add_stats(gcstate, 1, stats);

0 commit comments

Comments
 (0)