Skip to content

Conversation

@efimov-mikhail
Copy link
Member

@efimov-mikhail efimov-mikhail commented Dec 20, 2025

This is another attempt to provide the same StackRef flagging scheme for all builds (GIL, FT, STACKREF_DEBUG).
Instead of adding _Py_IsImmortal check to PyStackRef_FromPyObjectSteal for FT builds as in #141675, we make no checks at all in this function.
As a result, immortal objects will be DECREF'ed with no effect at reference closing.

@efimov-mikhail
Copy link
Member Author

@efimov-mikhail
Copy link
Member Author

If this PR will be merged we can remove PyStackRef_FromPyObjectStealMortal at all.
OTOH, I'd like to provide this change in a separate PR.

@Fidget-Spinner
Copy link
Member

This is another attempt to provide the same StackRef flagging scheme for all builds (GIL, FT, STACKREF_DEBUG). Instead of adding _Py_IsImmortal check to PyStackRef_FromPyObjectSteal for FR builds as in #141675, we make no checks at all in this function. As a result, immortal objects will be DECREF'ed with no effect at reference closing.

We can't rely on Py_DECREF to do the immortal check, as that is more expensive than simply checking a bit on a pointer. You have to do an atomic read of memory now.

@efimov-mikhail
Copy link
Member Author

efimov-mikhail commented Dec 20, 2025

We can't rely on Py_DECREF to do the immortal check, as that is more expensive than simply checking a bit on a pointer. You have to do an atomic read of memory now.

It seems that I'm not quite understand, when we will do additional atomic reads.
Do you mean FT builds? For those nothing changes since Py_DECREF_MORTAL is Py_DECREF on them.
And there's no atomic reads at Py_DECREF on GIL builds.

@Fidget-Spinner
Copy link
Member

Sorry I think I'm misunderstanding. This PR moves the immortal check from the stackref to the Py_DECREF right?

@efimov-mikhail
Copy link
Member Author

Sorry I think I'm misunderstanding. This PR moves the immortal check from the stackref to the Py_DECREF right?

Yes. If we steal stack reference from the immortal object then there will be no actual decrefing at Py_DECREF.

@kumaraditya303
Copy link
Contributor

We can't rely on Py_DECREF to do the immortal check, as that is more expensive than simply checking a bit on a pointer. You have to do an atomic read of memory now.

Actually for immortal check, only ob_ref_local is read with relaxed order so it will get compiled to plain load. That will still be more instructions executed that checking a single bit though.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants