Skip to content

gh-159437: Remove extra resurrection bookkeeping walks - #159595

Draft
mfalco-oai wants to merge 6 commits into
python:mainfrom
mfalco-oai:mfalco-oai/gh-159437-gc-resurrection-bookkeeping
Draft

mfalco-oai wants to merge 6 commits into
python:mainfrom
mfalco-oai:mfalco-oai/gh-159437-gc-resurrection-bookkeeping

Conversation

@mfalco-oai

@mfalco-oai mfalco-oai commented Oct 16, 2026 •

Copy link
Copy Markdown

With this optimization the GIL collector removes two bookkeeping walks around a resurrection recheck. update_refs() already replaces the temporary state, so the collecting bits do not need a separate clearing pass before the recheck. clear_weakrefs() clears the unreachable tags during its required walk over the remaining garbage, leaving the list normalized before deletion.

Count the stable unreachable set while preparing callbacks and retain a conservative cleanup flag. Reuse the count only when late weakref clearing has no work, and force fresh cleanup and counting after resurrection analysis.

Add a regression for a callback-free weakref observed when clearing a young cycle releases an older finalizer.
Count tp_del candidates during the existing reference traversals so zero can skip legacy classification. Reuse callback preparation to clear initial unreachable tags, while preserving normalized links before DEBUG_COLLECTABLE output can invoke Python.

Cover debug visibility of weakrefs with callbacks and legacy classification during a generation 0 collection.
Comment thread Python/gc.c
if (resurrection_checked) {
collectable = &final_unreachable;
// Python code may have changed the garbage set or created weakrefs.
// Refresh cleanup and counting, and normalize the recheck's tags.

@pablogsal pablogsal Oct 17, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small naming thing: needs_weakref_cleanup now also means the list links need their tags cleared, even when there are no weakrefs. The comment explains that, but the condition below still reads as though it only controls weakrefs. Maybe a name that covers this extra cleanup work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants