Skip to content

Commit 0464fb7

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 d34a745 commit 0464fb7

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

Python/gc.c

+10-8
Original file line numberDiff line numberDiff line change
@@ -1742,24 +1742,26 @@ gc_collect_region(PyThreadState *tstate,
17421742
int check_resurrected = finalize_garbage(tstate, &unreachable);
17431743

17441744
/* If no finalizers have run, no objects can have been resurrected: in that
1745-
* case skip the resurrection check. Otherwise we need to check and handle
1746-
* any objects that may have resurrected after the call to
1747-
* 'finalize_garbage' and continue the collection with the objects that are
1748-
* still unreachable */
1745+
* case skip the resurrection check and just use the existing unreachable
1746+
* list. Otherwise we need to check and handle any objects that may have
1747+
* resurrected after the call to 'finalize_garbage' and continue the
1748+
* collection with the objects that are still unreachable. */
17491749
PyGC_Head final_unreachable;
1750-
gc_list_init(&final_unreachable);
1750+
PyGC_Head *to_delete;
17511751
if (check_resurrected) {
1752+
gc_list_init(&final_unreachable);
17521753
handle_resurrected_objects(&unreachable, &final_unreachable, to);
1754+
to_delete = &final_unreachable;
17531755
} else {
1754-
gc_list_merge(&unreachable, &final_unreachable);
1756+
to_delete = &unreachable;
17551757
}
17561758

17571759
/* Call tp_clear on objects in the final_unreachable set. This will cause
17581760
* the reference cycles to be broken. It may also cause some objects
17591761
* in finalizers to be freed.
17601762
*/
1761-
stats->collected += gc_list_size(&final_unreachable);
1762-
delete_garbage(tstate, gcstate, &final_unreachable, to);
1763+
stats->collected += gc_list_size(to_delete);
1764+
delete_garbage(tstate, gcstate, to_delete, to);
17631765

17641766
/* Collect statistics on uncollectable objects found and print
17651767
* debugging information. */

0 commit comments

Comments
 (0)