Skip to content

Commit 24c3da6

Browse files
committed
gh-129210: Avoid unnecessary list init & merge
Avoid an unnecessary list initialisation and merge: this is a very marginal optimisation which gains a couple of percent speed increase for a few benchmarks. The marginal performance improvement may not be worth the marginal additional code complexity.
1 parent dd4384d commit 24c3da6

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

Python/gc.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,26 +1750,28 @@ gc_collect_region(PyThreadState *tstate,
17501750
int check_resurrected = finalize_garbage(tstate, &unreachable);
17511751

17521752
/* If no finalizers have run, no objects can have been resurrected: in that
1753-
* case skip the resurrection check. Otherwise we need to check and handle
1754-
* any objects that may have resurrected after the call to
1755-
* 'finalize_garbage' and continue the collection with the objects that are
1756-
* still unreachable */
1753+
* case skip the resurrection check and just use the existing unreachable
1754+
* list. Otherwise we need to check and handle any objects that may have
1755+
* resurrected after the call to 'finalize_garbage' and continue the
1756+
* collection with the objects that are still unreachable. */
17571757
PyGC_Head final_unreachable;
1758-
gc_list_init(&final_unreachable);
1758+
PyGC_Head *to_delete;
17591759
if (check_resurrected) {
1760+
gc_list_init(&final_unreachable);
17601761
handle_resurrected_objects(&unreachable, &final_unreachable, to);
17611762
unreachable_count = gc_list_size(&final_unreachable);
1763+
to_delete = &final_unreachable;
17621764
} else {
1763-
gc_list_merge(&unreachable, &final_unreachable);
1764-
assert(unreachable_count == gc_list_size(&final_unreachable));
1765+
to_delete = &unreachable;
1766+
assert(unreachable_count == gc_list_size(to_delete));
17651767
}
17661768

17671769
/* Call tp_clear on objects in the final_unreachable set. This will cause
17681770
* the reference cycles to be broken. It may also cause some objects
17691771
* in finalizers to be freed.
17701772
*/
17711773
stats->collected += unreachable_count;
1772-
delete_garbage(tstate, gcstate, &final_unreachable, to);
1774+
delete_garbage(tstate, gcstate, to_delete, to);
17731775

17741776
/* Collect statistics on uncollectable objects found and print
17751777
* debugging information. */

0 commit comments

Comments
 (0)