Text of peps.python.org › pep-0683 as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

PEP 683 – Immortal Objects, Using a Fixed Refcount | peps.python.org

PEP 683 – Immortal Objects, Using a Fixed Refcount PEP 683 – Immortal Objects, Using a Fixed Refcount Author : Eric Snow <ericsnowcurrently at gmail.com>, Eddie Elizondo <eduardo.elizondorueda at gmail.com> Discussions-To : Discourse thread Status : Final Type : Standards Track Created : 10-Feb-2022 Python-Version : 3.12 Post-History : 16-Feb-2022 , 19-Feb-2022 , 28-Feb-2022 , 12-Aug-2022 Resolution : Discourse message Table of Contents PEP Acceptance Conditions Abstract Scope Implementation Summary Motivation Reducing CPU Cache Invalidation Avoiding Data Races Avoiding Copy-on-Write Rationale Impact Benefits Performance Backward Compatibility Accidental Immortality Stable ABI Accidental De-Immortalizing Alternate Python Implementations Security Implications Maintainability Specification Public Refcount Details Constraints Immortal Mutable Objects Implicitly Immortal Objects Un-Immortalizing Objects _Py_IMMORTAL_REFCNT Affected API Immortal Global Objects Object Cleanup Performance Regression Mitigations at the end of runtime init, mark all objects as immortal drop unnecessary hard-coded refcount operations specialize for immortal objects in the eval loop other possibilities Solutions for Accidental De-Immortalization Documentation Reference Implementation Open Issues References Prior Art Discussions Runtime Object State Reference Counting, with Cyclic Garbage Collection Copyright Important This PEP is a historical document.

The up-to-date, canonical documentation can now be found at reference count . × See PEP 1 for how to propose changes. PEP Acceptance Conditions The PEP was accepted with conditions: the primary proposal in Solutions for Accidental De-Immortalization (reset the immortal refcount in tp_dealloc() ) was applied types without this were not immortalized (in CPython’s code) the PEP was updated with final benchmark results once the implementation is finalized (confirming the change is worthwhile) Abstract Currently the CPython runtime maintains a small amount of mutable state in the allocated memory of each object.

Because of this, otherwise immutable objects are actually mutable. This can have a large negative impact on CPU and memory performance, especially for approaches to increasing Python’s scalability. This proposal mandates that, internally, CPython will support marking an object as one for which that runtime state will no longer change. Consequently, such an object’s refcount will never reach 0, and thus the object will never be cleaned up (except when the runtime knows it’s safe to do so, like during runtime finalization).

We call these objects “immortal”. (Normally, only a relatively small number of internal objects will ever be immortal.) The fundamental improvement here is that now an object can be truly immutable. Scope Object immortality is meant to be an internal-only feature, so this proposal does not include any changes to public API or behavior (with one exception). As usual, we may still add some private (yet publicly accessible) API to do things like immortalize an object or tell if one is immortal.

Any effort to expose this feature to users would need to be proposed separately. There is one exception to “no change in behavior”: refcounting semantics for immortal objects will differ in some cases from user expectations. This exception, and the solution, are discussed below. Most of this PEP focuses on an internal implementation that satisfies the above mandate. However, those implementation details are not meant to be strictly proscriptive.

Instead, at the least they are included to help illustrate the technical considerations required by the mandate. The actual implementation may deviate somewhat as long as it satisfies the constraints outlined below. Furthermore, the acceptability of any specific implementation detail described below does not depend on the status of this PEP, unless explicitly specified. For example, the particular details of: how to mark something as immortal how to recognize something as immortal which subset of functionally immortal objects are marked as immortal which memory-management activities are skipped or modified for immortal objects are not only CPython-specific but are also private implementation details that are expected to change in subsequent versions.

Implementation Summary Here’s a high-level look at the implementation: If an object’s refcount matches a very specific value (defined below) then that object is treated as immortal. The CPython C-API and runtime will not modify the refcount (or other runtime state) of an immortal object. The runtime will now be explicitly responsible for deallocating all immortal objects during finalization, unless statically allocated. (See Object Cleanup below.) Aside from the change to refcounting semantics, there is one other possible negative impact to consider.

The threshold for an “acceptable” performance penalty for immortal objects is 2% (the consensus at the 2022 Language Summit). A naive implementation of the approach described below makes CPython roughly 4% slower. However, the implementation is ~performance-neutral~ once known mitigations are applied. TODO: Update the performance impact for the latest branch (both for GCC and for clang). Motivation As noted above, currently all objects are effectively mutable.

That includes “immutable” objects like str instances. This is because every object’s refcount is frequently modified as the object is used during execution. This is especially significant for a number of commonly used global (builtin) objects, e.g. None . Such objects are used a lot, both in Python code and internally. That adds up to a consistent high volume of refcount changes. The effective mutability of all Python objects has a concrete impact on parts of the Python community, e.g. projects that aim for scalability like Instagram or the effort to make the GIL per-interpreter.

Below we describe several ways in which refcount modification has a real negative effect on such projects. None of that would happen for objects that are truly immutable. Reducing CPU Cache Invalidation Every modification of a refcount causes the corresponding CPU cache line to be invalidated. This has a number of effects. For one, the write must be propagated to other cache levels and to main memory. This has small effect on all Python programs.

Immortal objects would provide a slight relief in that regard. On top of that, multi-core applications pay a price. If two threads (running simultaneously on distinct cores) are interacting with the same object (e.g. None ) then they will end up invalidating each other’s caches with each incref and decref. This is true even for otherwise immutable objects like True , 0 , and str instances. CPython’s GIL helps reduce this effect, since only one thread runs at a time, but it doesn’t completely eliminate the penalty.

Avoiding Data Races Speaking of multi-core, we are considering making the GIL a per-interpreter lock, which would enable true multi-core parallelism. Among other things, the GIL currently protects against races between multiple concurrent threads that may incref or decref the same object. Without a shared GIL, two running interpreters could not safely share any objects, even otherwise immutable ones like None . This means that, to have a per-interpreter GIL, each interpreter must have its own copy of every object.

That includes the singletons and static types. We have a viable strategy for that but it will require a meaningful amount of extra effort and extra complexity. The alternative is to ensure that all shared objects are truly immutable. There would be no races because there would be no modification. This is something that the immortality proposed here would enable for otherwise immutable objects. With immortal objects, support for a per-interpreter GIL becomes much simpler.

Avoiding Copy-on-Write For some applications it makes sense to get the application

The rest of this page is on the live site.