BRC vs. Atomic RC: Non-Atomic vs. Constant Overhead

Biased Reference Counting optimizes by using fast, non-atomic operations for objects primarily accessed by their 'owning' thread, falling back to slower atomic operations only when other threads intervene. This contrasts with traditional atomic reference counting, which incurs the constant, higher overhead of atomic operations for every reference count modification.

The top 3

  1. BRC's Single-Thread Speed: Avoiding 60% Atomic Slowdown: Naive replacement of non-atomic reference counting with atomic operations, a common approach to GIL removal, resulted in a 60% average slowdown on the `pyperformance` benchmark suite. Biased Reference Counting (BRC) avoids this by enabling the object's owner thread to perform fast, non-atomic reference count updates.
  2. BRC's Dual Counter Design: Local vs. Shared Ref Counts: Biased Reference Counting (BRC) fundamentally reworks an object's reference count by splitting it into two distinct fields: `ob_ref_local` for the creating (owning) thread, updated non-atomically, and `ob_ref_shared` for all other threads, updated atomically. This strategy, detailed in PEP 703, optimizes for the common case where objects are primarily accessed by their owner.
  3. BRC's Safe Object Deallocation: Merging Counts for Release: To ensure correct object deallocation, BRC periodically merges the `ob_ref_local` and `ob_ref_shared` counts. When the owning thread decrements its local count to zero, but the shared count is still active, a special bit is set. Subsequent decrements of the shared count will then trigger the object's final freeing once it also reaches zero.

Sources

Open the full topic