Description
Feature or enhancement
Proposal:
This issue mostly mirrors Mark's original one at faster-cpython/ideas#632.
As part of PEP 703, tagged pointers will help achieve deferred reference counting.
Tagged pointers will also help the default GIL builds in 3.14 and future with unboxed ints.
Here's the initial design (open for comments):
We change the evaluation stack from PyObject *
to PyTaggedObject
. PyTaggedObject
looks like this.
typedef union PyTaggedObject {
PyObject *obj;
uintptr_t bits;
} PyTaggedObject;
Note: I am limiting the tagged pointers to just the evaluation stack (this is up for debate as well). The main reason is to keep the change as minimally invasive as possible for 3.13 and not leak out any implementation details for now. For example, I don't want to support tagged pointers in C API functions. This may/will change in 3.14.
Thanks to the cases generator, we can then autogenerate all the proper accessors to said object. For example, a stack effect of PyObject *
will generate the code effect.obj
, while a stack effect of PyTaggedObject
(the new default) will generate the code (PyObject *)(effect.bits & ~TAGS)
or something similar.
Unboxed ints will also help both free-threaded and non-free-threaded builds by reducing refcounting even more for simple arithmetic operations (and making integer operations native). However, I am aiming for deferred tags in 3.13, and anything else is a plus.
Further note: due to wanting to respect immediate finalizers, we are not deferring that many objects. So this may not be a win on default builds. On free-threaded builds, I see a slight (10%) speedup on fibonacci microbenchmarks. Will benchmark on the default bulid as well and see the results.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
- gh-117139: Set up the tagged evaluation stack #117186
- gh-117139: Add header for tagged pointers #118330
- gh-117139: Convert the evaluation stack to stack refs #118450
- gh-117139: Fix missing semicolon #118573
- gh-117139: Fix a few wrong steals in bytecodes.c #121127
- gh-117139: A StackRef Debugging Mode #121134
- gh-117139: Add _PyTuple_FromStackRefSteal and use it #121244
- gh-117139: Fix an incorrect borrow in bytecodes.c #122318
- gh-117139: Replace _PyList_FromArraySteal with stack ref variant #122830
- gh-117139: Fix a few
_PyStackRef
related bugs #122831 - gh-117139: Garbage collector support for deferred refcounting #122956
### Tasks
- [ ] https://github.com/python/cpython/issues/118926
- [ ] https://github.com/python/cpython/issues/118930