8000 PEP 683: Immortal Objects v3 by ericsnowcurrently · Pull Request #2372 · python/peps · GitHub
[go: up one dir, main page]

Skip to content

PEP 683: Immortal Objects v3 #2372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Mar 1, 2022
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3fe96eb
Update the post history.
ericsnowcurrently Feb 26, 2022
6771a73
Expand the abstract.
ericsnowcurrently Feb 26, 2022
dd3861d
Be a little less specific.
ericsnowcurrently Feb 26, 2022
3785039
Drop the old scope section.
ericsnowcurrently Feb 26, 2022
c84919d
Update the performance numbers.
ericsnowcurrently Feb 26, 2022
bf8257e
Elaborate on refcounts and accidental immortality.
ericsnowcurrently Feb 26, 2022
407efc0
Clarify how the PEP supports immutability rather than enforcing it.
ericsnowcurrently Feb 28, 2022
7631bdf
Clarify about the refcount 1.
ericsnowcurrently Feb 28, 2022
d171a3c
Clarify about the immortal bit.
ericsnowcurrently Feb 28, 2022
b27fbcb
Drop some explanation about immortal global objects.
ericsnowcurrently Feb 28, 2022
a0b9d04
Relate cleanup to performance.
ericsnowcurrently Feb 28, 2022
7666c66
Add a note about __del__ and weakrefs.
ericsnowcurrently Feb 28, 2022
365ef60
Drop an open question.
ericsnowcurrently Feb 28, 2022
fb490f9
Add a note about GC.
ericsnowcurrently Feb 28, 2022
34de9b5
Outline solutions to accidental de-immortalizing.
ericsnowcurrently Feb 28, 2022
7f5052b
Update the post history.
ericsnowcurrently Feb 28, 2022
4f1e4fb
Collapse the abstract.
ericsnowcurrently Feb 28, 2022
2772517
Tweak the scope section.
ericsnowcurrently Feb 28, 2022
baa1947
lint
ericsnowcurrently Feb 28, 2022
a6d07a4
Fix a typo.
ericsnowcurrently Feb 28, 2022
5b2fe45
Fix a typo.
ericsnowcurrently Feb 28, 2022
8188ae5
Fix a typo.
ericsnowcurrently Feb 28, 2022
db478a9
Fix the post history.
8000 ericsnowcurrently Feb 28, 2022
f6bab1d
Update the magic bit in examples.
ericsnowcurrently Feb 28, 2022
c8a2d41
Clarify an exmaple.
ericsnowcurrently Feb 28, 2022
4ed4d44
Drop an outdated note.
ericsnowcurrently Mar 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Expand the abstract.
  • Loading branch information
ericsnowcurrently committed Feb 26, 2022
commit 6771a73b146ae151e1bd04dd6cb5fc03951b714b
82 changes: 59 additions & 23 deletions pep-0683.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,65 @@ Currently the CPython runtime maintains a
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. The solution proposed here provides a way
to mark an object as one for which that per-object
runtime state should not change.

Specifically, if an object's refcount matches a very specific value
(defined below) then that object is treated as "immortal". If an object
is immortal then its refcount will never be modified by ``Py_INCREF()``,
etc. Consequently, the refcount will never reach 0, so that object will
never be cleaned up (unless explicitly done, e.g. during runtime
finalization). Additionally, all other per-object runtime state
for an immortal object will be considered immutable.

This approach has some possible negative impact, which is explained
below, along with mitigations. A critical requirement for this change
is that the performance regression be no more than 2-3%. Anything worse
than performance-neutral requires that the other benefits are proportionally
large. Aside from specific applications, the fundamental improvement
here is that now an object can be truly immutable.

(This proposal is meant to be CPython-specific and to affect only
internal implementation details. There are some slight exceptions
to that which are explained below. See `Backward Compatibility`_,
`Public Refcount Details`_, and `scope`_.)
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 so the
object will never be cleaned up. 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.
However, this does not prevent us from adding (publicly accessible)
private 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: 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. Furthermore, the
acceptability of any 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.

Aside from the change to refcounting semantics, there is one other
possible negative impact to consider. A naive implementation of the
approach described below makes CPython roughly 4% slower. However,
the implementation is performance-neutral once known mitigations
are applied.


Motivation
Expand Down
0