BRC's Strategy for C-API Compatibility

Biased Reference Counting, as part of PEP 703, aims to update the Python C API to support a GIL-optional mode while striving to maintain compatibility with existing C extensions, a significant challenge that previous GIL-removal attempts often faced due to required extensive changes.

The top 3

  1. BRC's Dual-Count System: C-API Compatibility Foundation: Biased Reference Counting (BRC) ensures C-API compatibility by replacing the single `ob_refcnt` with separate `ob_ref_local` (non-atomic for owner) and `ob_ref_shared` (atomic for other threads), plus an `ob_tid` to track the owning thread.
  2. How BRC Transforms Core C-API Reference Operations: BRC modifies the fundamental `Py_INCREF()` and `Py_DECREF()` C-API macros to dynamically use either non-atomic `ob_ref_local` or atomic `ob_ref_shared` based on the object's owning thread ID, with `Py_DECREF()` potentially queuing destruction for non-owning threads.
  3. BRC's Performance Edge: Avoiding Atomic C-API Bottlenecks: BRC's C-API compatibility strategy significantly boosts multi-threaded performance by minimizing expensive atomic read-modify-write operations on reference counts, a key bottleneck that hindered earlier GIL-removal projects like Gilectomy by causing lock contention.

Sources

Open the full topic