Description
Feature or enhancement
CPython's current reference counting implementation would not be thread-safe without the GIL. In --disable-gil
builds, we should implement biased reference counting, which is thread-safe and has lower execution overhead compared to plain atomic reference counting. The design is described in the "Biased Reference Counting" section of PEP 703.
This will require changing the PyObject
struct in --disable-gil
builds. I expect the --disable-gil
struct to look like:
struct _object {
uintptr_t ob_tid;
uint16_t _padding;
uint8_t ob_mutex;
uint8_t ob_gc_bits;
uint32_t ob_ref_local;
Py_ssize_t ob_ref_shared;
PyTypeObject *ob_type;
};
Not all the fields will be used immediately, but it will be easier to include them as part of implementing this change.
I intend to split the implementation across (at least) two PRs to make it easier to review.
- The first PR will add the new
PyObject
fields and implement the core biased reference counting operations, but not the inter-thread queue to reduce the initial complexity. (done) - A later PR will implement the inter-thread queue. (not yet implemented)
The inter-thread queue is needed eventually (before actually disabling the GIL), but since the --disable-gil
builds still currently require the GIL, it doesn't have to be in the initial PR.
Linked PRs
- gh-110481: Implement biased reference counting #110764
- gh-110481: Fix _Py_ThreadId for non-free-threaded mode #111503
- gh-110481: Non-limited Py_INCREF/DECREF() in cpython/object.h #111505
- gh-110481: Implement _Py_DECREF_NO_DEALLOC for free-threaded build #111560
- gh-110481: fix 'unused function' warning for
is_shared_refcnt_dead
. #111974 - gh-110481: Fix Py_SET_REFCNT() integer overflow #112174
- gh-110481, doc: Add "immortal" term to the glossary #112180
- gh-110481: Fix typo in Py_SET_REFCNT() #112595
- gh-110481: Implement inter-thread queue for biased reference counting #114824
- gh-110481: Fix biased reference counting queue initialization. #117271