Skip to content

gh-148871: make LOAD_COMMON_CONSTANT use immortal stackref borrows#149625

Merged
markshannon merged 6 commits into
python:mainfrom
NekoAsakura:gh-148871-immortal-stackref
May 28, 2026
Merged

gh-148871: make LOAD_COMMON_CONSTANT use immortal stackref borrows#149625
markshannon merged 6 commits into
python:mainfrom
NekoAsakura:gh-148871-immortal-stackref

Conversation

@NekoAsakura
Copy link
Copy Markdown
Member

@NekoAsakura NekoAsakura commented May 9, 2026

gc.is_tracked(all/any) True -> False

Comment thread Python/bytecodes.c Outdated
// Keep in sync with _common_constants in opcode.py
assert(oparg < NUM_COMMON_CONSTANTS);
value = PyStackRef_FromPyObjectNew(tstate->interp->common_consts[oparg]);
value = PyStackRef_DUP(tstate->interp->common_consts[oparg]);
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.

This needs to check the tag.

Could you add PyStackRef_DupImmortal(ref) which would return its argument unchanged and assert that the referenced object is immortal, otherwise it defeats the point of changing PyObject * to _PyStackRef here

Comment thread Python/flowgraph.c Outdated
if (opcode == LOAD_COMMON_CONSTANT) {
assert(oparg < NUM_COMMON_CONSTANTS);
return Py_NewRef(_PyInterpreterState_GET()->common_consts[oparg]);
return Py_NewRef(PyStackRef_AsPyObjectBorrow(
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.

Suggested change
return Py_NewRef(PyStackRef_AsPyObjectBorrow(
return PyStackRef_AsPyObjectBorrow(

These objects are immortal, you can borrow references to them.

@NekoAsakura
Copy link
Copy Markdown
Member Author

Thanks for reviewing :')
Unfortunately I cannot get on my remote server right now due to some network issues, but I’ll sort the requested changes tomorrow.

@markshannon markshannon self-requested a review May 28, 2026 10:16
Copy link
Copy Markdown
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

Looks good. Thanks for doing this

@markshannon markshannon merged commit 39bd44f into python:main May 28, 2026
105 of 106 checks passed
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.

2 participants