Skip to content

Simplify PyStackRef_FromPyObjectSteal #127022

Closed
@colesbury

Description

@colesbury

Feature or enhancement

Currently, PyStackRef_FromPyObjectSteal checks if the object is immortal in order to set the Py_TAG_DEFERRED bit.

static inline _PyStackRef
_PyStackRef_FromPyObjectSteal(PyObject *obj)
{
assert(obj != NULL);
// Make sure we don't take an already tagged value.
assert(((uintptr_t)obj & Py_TAG_BITS) == 0);
unsigned int tag = _Py_IsImmortal(obj) ? (Py_TAG_DEFERRED) : Py_TAG_PTR;
return ((_PyStackRef){.bits = ((uintptr_t)(obj)) | tag});
}
# define PyStackRef_FromPyObjectSteal(obj) _PyStackRef_FromPyObjectSteal(_PyObject_CAST(obj))

This check isn't necessary and has a performance cost that's not made up for by the slightly faster PyStackRef_CLOSE() calls or PyStackRef_Is() checks.

We should simplify PyStackRef_FromPyObjectSteal so that it creates _PyStackRef directly from the PyObject * without setting any tag bits.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions