-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
gh-129210: GC optimisations for case where no objects being collected have finalizers #132488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
To handle objects potentially resurrected in finalizers it is necessary to repeat the reachability check on objects to be collected. If there are no finalizers run this can be skipped: in that case it is not possible for an object to be resurrected. In my environment and testing this gives a ~25-30% performance improvement for the pythongh-129210 micro-benchmark.
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Given this is just an optimisation for the incremental GC, which is being introduced in v3.14, I don't imagine it requires a blurb, but I'm happy to add one if it is felt useful. I've also had a quick look at the free-threading GC and it appears at first glance like similar optimisations are possible for it also. I'm investigating that now and if it pans out will add commits for it to this PR. |
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.
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any benchmarks? (Also, please don't force push and make the bot spam; we squash merge at the end, and you need a blurb entry anyway.)
The percentage improvements I mentioned in the issue discussion and commit logs were taken from the benchmark's output and I've been running Any tips on how to speed this up and/or make it more reliable would be welcome.
Sorry! A habit from other organisations, with other practices, that I'll try and resist in the future. |
I've just finished running Out of 102 tests, 20 are "significantly" faster and 7 slower
However, I still don't have much confidence in the reliability of these results. Many are unstable. |
…zers run As for the regular GIL garbage collector, skip the second reachability check if no objects to-be-collected have finalizers, and hence none can be resurrected. In (so far very limited) testing this gives a ~10% overall performance improvement to the motivating benchmark's runtime and a ~35% improvement to its reported time. The pyperformance gc_traversal and gc_collect benchmarks show no significant difference and a 6% improvement respectively.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense but I'm not familiar enough with the GC to approve.
Misc/NEWS.d/next/Core_and_Builtins/2025-04-16-12-02-26.gh-issue-129210.a5uLpE.rst
Outdated
Show resolved
Hide resolved
…e-129210.a5uLpE.rst Co-authored-by: Jelle Zijlstra <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
If no objects being collected have finalizers, they will not be able to be resurrected. This lets us avoid some repeated work we would otherwise have to do.