Core Changes: Biased Reference Counting & Immortal Objects

PEP 703, proposing an optional GIL-free CPython, relies on 'Biased Reference Counting' to manage thread-safe memory with reduced overhead, and 'Immortal Objects' for core Python objects to bypass reference counting. This shift increases memory usage and requires existing C extensions to be updated for thread safety.

The top 3

  1. No-GIL Python's Single-Threaded Performance Cost: The `--disable-gil` build, leveraging Biased Reference Counting, introduces a 10-15% slowdown in single-threaded workloads compared to GIL-enabled Python, due to necessary thread-safe reference counting operations. Immortal Objects alone initially showed a ~2% performance regression, which was mitigated to near performance-neutral.
  2. How Biased Reference Counting Boosts Speed: Biased Reference Counting (BRC) splits each object's reference count into a 'local' count for the owning (creating) thread and a 'shared' count for all other threads. The owning thread uses faster non-atomic operations for its local count, while other threads use slower, thread-safe atomic operations on the shared count, optimizing for the common single-threaded access pattern.
  3. Immortal Objects: Key Types & Memory Gains: Python 3.12 introduced Immortal Objects (PEP 683) for high-use singletons like `None`, `True`, and `False`. These objects now have a fixed reference count, bypassing frequent checks and living for the interpreter's lifetime. This change reduces CPU cache invalidation and significantly decreases private memory usage in multi-process deployments by avoiding copy-on-write issues.

Sources

Open the full topic