Uh oh!
There was an error while loading. Please reload this page.
gh-127119: Remove check on accidental deallocation of immortal objects for free-threaded build - #127120
gh-127119: Remove check on accidental deallocation of immortal objects for free-threaded build#127120eendebakpt wants to merge 17 commits into
Conversation
skirpichev
commented
Nov 22, 2024
Apparently, assertion is broken in tests. |
eendebakpt
commented
Nov 22, 2024
Yes, my mistake. Should be fixed now |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
…e-127119.p9Yv4U.rst Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
bedevere-bot
commented
Nov 22, 2024
🤖 New build scheduled with the buildbot fleet by @ZeroIntensity for commit 4dc97c9 🤖 If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again. |
CI is failing, because apparently you can build with assertions but not with debug enabled (that's what I get for prematurely approving, ha).
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
this changes from silently re-immortalizing the small ints to actually letting them be deallocated in non-debug builds. in theory this is supposed to be fine, but how confident are we of (buggy) reference counting code not relying in the former behavior?
(I'm not blocking this PR, I think the change is acceptable, just want that pondered...)
ZeroIntensity
commented
Nov 24, 2024
I thought about that too, but I think issues around immortal objects being buggy was fixed by GH-125251 (at least, I thought that was implied by the removal of |
eendebakpt
commented
Nov 24, 2024
The possibility of de-allocating immortal objects is described in [PEP 683, section Accidental De-Immortalizing](https://peps.python.org/pep-0683/#accidental-de-immortalizing). Also see But if I understand the correctly the accidental-de-immortalizing cannot happen on: 64-bit systems, or with the free-threading build, or when And yes, this should be double checked! |
eendebakpt
commented
Nov 24, 2024
Results on a microbenchmark: I expect no overall improvement on the pyperformance benchmark. Benchmark script |
| #ifdef Py_LIMITED_API | ||
| #ifndef Py_GIL_DISABLED |
There was a problem hiding this comment.
I'm not sure it's possible for us to know when we're using the limited API. Py_LIMITED_API is something that affects Python.h at the compile-time of an extension module, but this happens during the compilation of CPython, so it will never be true.
markshannon
commented
Dec 4, 2024
As an alternative to removing the check altogether we could make it cheaper by using the reserved bit https://github.com/python/cpython/blob/main/Include/cpython/longintrepr.h#L79 as a marker for immortal/small ints. |
eendebakpt
commented
Dec 4, 2024
@markshannon Thanks for the idea! I think it works and would not be hard to implement, but I am a bit hesitant to use a reserved bit for a small optimization like this. I created an alternative PR with a faster check based on your idea though |
markshannon
commented
Dec 5, 2024
That bit is reserved for exactly this use case, so it is fine to use it. |
eendebakpt
commented
Jan 28, 2025
Closing in favor of #127620 |
(Description was updated) The python small ints are immortal with a fixed reference count (also some other types). However, old extension modules can still modify the reference count and end up deallocating these objects.
This should not be an issue on 64-bit builds (even when accidentically changing the reference count it will take a very long time for an object to reach refcount zero) or the free-threaded build (which used a different reference counts, so has no legacy extensions modules)
In this PR we remove the check on accidental deallocation for the free-threading build. This has performance impact for the long type (where the
tp_deallocis actually called often). For the other types we remove it to be consistent, but it should have no performance impact.Also see: