Low-Level Memory Layout: PyObject Header & Immortal Objects
Every CPython value is a C struct starting with the `PyObject` header: a 64-bit reference counter (`ob_refcnt`) and a type pointer (`ob_type`). Python 3.12+ introduces immortal objects (like `None`, small integers) with permanent sentinel refcounts that bypass atomic increments 🧠🔍.
import sys
# Dynamic heap object:
data = ["alpha", "beta"]
initial_refs = sys.getrefcount(data)
alias = data
after_alias = sys.getrefcount(data)
# Python 3.12+ Immortal Object (PEP 683):
immortal_ref = sys.getrefcount(None)
print(f"Dynamic refs: {initial_refs} -> {after_alias} 🚀")
print(f"Immortal Object Refcount (None): {immortal_ref} (Permanent!) 🛡️")
# 78. Low-Level Memory Layout: PyObject Header & Immortal Objects In CPython, an integer like `42` is not just 4 bytes of hardware memory. It is a heap-allocated `PyLongObject` struct containing a header, digit pointers, and metadata consuming 28 bytes of RAM 🛑. ```python # The C PyObject Structure (Include/object.h): # typedef struct _object { # _PyObject_HEAD_EXTRA // Double-linked list pointers in debug builds # Py_ssize_t ob_refcnt; // Reference count (64-bit uint) # PyTypeObject *ob_type; // Pointer to type descriptor (e.g. &PyList_Type) # } PyObject; ``` ### Reference Counting Mechanics 1. **Direct Ownership**: When you create an object, CPython initializes `ob_refcnt = 1`. Passing it to a function or assigning it to a new variable executes `Py_INCREF()`, incrementing the counter. 2. **Deallocation**: When a variable is rebound or goes out of scope, CPython executes `Py_DECREF()`. When `ob_refcnt == 0`, the memory is immediately deallocated or recycled into a free-list. 3. **PEP 683 Immortal Objects**: Constants like `None`, `True`, `False`, and small ints (-5 to 256) used to incur constant cache invalidations when read by multiple threads. In 3.12+, their refcount is locked at a sentinel value ($2^{32}-1$), completely bypassing refcount writes 🛡️. --- ### Interactive Pop-Quiz 🧠 #### What is the primary performance benefit of PEP 683 Immortal Objects in modern CPython runtimes? - [ ] They eliminate atomic reference count cache-line bouncing and page dirties across multiple threads and subinterpreters 🧠 - [ ] They allow Python to execute without an operating system kernel - [ ] They compress the bytecode of functions stored in disk caches - [ ] They convert dynamic lists into static C arrays --- ### Practical Challenge 🎯 Create a list `target = ['x', 'y']`. Record its `sys.getrefcount`. Append `target` to a new list `holder = [target]`. Verify that `sys.getrefcount(target)` incremented by 1. Clear `holder`, verify refcount drops back by 1, and print `'REFCOUNT_TRACKED'`. <details> <summary>Solution</summary> ```python import sys target = ['x', 'y'] c1 = sys.getrefcount(target) holder = [target] c2 = sys.getrefcount(target) assert c2 == c1 + 1 holder.clear() c3 = sys.getrefcount(target) assert c3 == c1 print("REFCOUNT_TRACKED") ``` </details>