Repository navigation
gh-159437: Skip unnecessary legacy finalizer scans - #159593
Draft
mfalco-oai wants to merge 5 commits into
Draft
mfalco-oai wants to merge 5 commits into
mfalco-oai wants to merge 5 commits into
Conversation
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.
pablogsal
reviewed
Oct 17, 2026
| PyObject *op = FROM_GC(gc); | ||
| traverse = Py_TYPE(op)->tp_traverse; | ||
| PyTypeObject *type = Py_TYPE(op); | ||
| legacy_finalizer_count += type->tp_del != NULL; |
Member
There was a problem hiding this comment.
This checks tp_del on every mortal candidate to avoid scanning the objects that end up as garbage. Even a live object with tp_del prevents the skip.
The reachability walk seems a better place to gather this. In a related change I made in an earlier PR, survivor counting was folded into that walk. We could use the same placement here and set a boolean when an object enters the provisional garbage list. If it gets rescued later, we just do the scan unnecessarily.
That removes this bookkeeping from the reference walkers. The exact count is unneeded anyway since we only need to know whether any legacy finalizers are present. This alternative still needs building and measuring.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With this optimization the GIL collector counts mortal candidates whose type defines
tp_delduring the existing reference passes. A zero count rules out legacy finalizers in the garbage, so the collector skips their classification and reachability scans. On that path, it also clears unreachable tags during weakref preparation. WithDEBUG_COLLECTABLE, the tags are cleared earlier, before debug output can run Python.