Performance Impact: Constant `Py_INCREF`/`Py_DECREF` Operations

Every Python object interaction, such as assignment, function argument passing, and attribute access, triggers `Py_INCREF` or `Py_DECREF` operations to update reference counts. This constant overhead can significantly impact CPU performance, especially in highly dynamic code, making it less efficient than languages employing generational or tracing garbage collectors.

The top 3

  1. Atomic Overhead: 60% Slowdown without GIL: If `Py_INCREF`/`Py_DECREF` were made atomic for thread safety without the Global Interpreter Lock, CPython could face an estimated 60% performance slowdown on benchmarks, as reference counting inherently turns object reads into memory writes.
  2. RC vs. Tracing GC: Throughput vs. Latency: Python's reference counting offers predictable object deallocation but incurs constant CPU overhead, leading to typically lower throughput compared to tracing garbage collectors (like those in Java or Go), which achieve higher throughput with occasional, unpredictable 'stop-the-world' pauses.
  3. Optimizing `INCREF`/`DECREF`: Borrowing & Immortals: Recent Python versions have introduced optimizations to mitigate `Py_INCREF`/`Py_DECREF` overhead: Python 3.14 added `LOAD_FAST_BORROW` bytecode to avoid incrementing reference counts for local variable loads in hot loops, and Python 3.12 introduced 'immortal objects' to bypass reference count checks for frequently used, never-deallocated objects.

Sources

Open the full topic