Skip to content
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

PEP 683: Add a PEP for immortal objects #2320

Merged
merged 34 commits into from
Feb 15, 2022

Conversation

ericsnowcurrently
Copy link
Member

I plan on merging/posting this as soon as I get a review from my co-author.

@ericsnowcurrently
Copy link
Member Author

@eduardo-elizondo

@AA-Turner
Copy link
Member

You can take 683
A

@ericsnowcurrently ericsnowcurrently changed the title Add a PEP for immortal objects. [PEP 683[ Add a PEP for immortal objects. Feb 11, 2022
@ericsnowcurrently ericsnowcurrently changed the title [PEP 683[ Add a PEP for immortal objects. [PEP 683] Add a PEP for immortal objects. Feb 11, 2022
pep-0683.rst Outdated Show resolved Hide resolved
pep-0683.rst Outdated Show resolved Hide resolved
pep-0683.rst Outdated Show resolved Hide resolved
Comment on lines +96 to +97
memory usage. Several enterprise Python users (e.g. Instagram,
YouTube) have taken advantage of this. However, the above
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be useful have a citation/link for this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really have any. However, it's fairly common knowledge on the core team.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I don't think we've made a public facing post about this, but I added this to Instagram and can confirm that we are still using this 🙂

pep-0683.rst Outdated Show resolved Hide resolved
pep-0683.rst Outdated Show resolved Hide resolved
pep-0683.rst Outdated Show resolved Hide resolved
pep-0683.rst Outdated Show resolved Hide resolved
pep-0683.rst Outdated Show resolved Hide resolved
pep-0683.rst Outdated Show resolved Hide resolved
pep-0683.rst Outdated Show resolved Hide resolved
pep-0683.rst Outdated Show resolved Hide resolved
@brettcannon
Copy link
Member

@ericsnowcurrently please update CODEOWNERS so you get asked to review any PRs for the PEP.

@ericsnowcurrently
Copy link
Member Author

@eduardo-elizondo, unless you have any objections, I'm going to merge this first thing tomorrow morning and post to python-dev.

@AA-Turner AA-Turner changed the title [PEP 683] Add a PEP for immortal objects. PEP 683: Add a PEP for immortal objects. Feb 15, 2022
pep-0683.rst Outdated Show resolved Hide resolved
pep-0683.rst Outdated Show resolved Hide resolved
Comment on lines +146 to +147
Several promising mitigation strategies will be pursued in the effort
to bring it closer to performance-neutral.
Copy link

@eduardo-elizondo eduardo-elizondo Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not needed to include here but these are four different strategies that I'll be pursuing:

  • Immortalized interned strings (including Py_IDENTIFIERs)
  • Immortalizing heap after startup
  • Immortalizing module-level globals at import time

Also the following but it probably won't impact current benchmarks:

  • GC improvements for large applications
    Mental note to expand the benchmark suite to include something that overflows the last GC generation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Immortalized interned strings (including Py_IDENTIFIERs)

Note that I've replaced all core uses of _Py_IDENTIFIER() with direct access of statically allocated/initialized objects under _PyRuntimeState.global_objects.

pep-0683.rst Outdated

* (public) ``Py_REFCNT()``
* (public) ``sys.getrefcount()``
* (public) ``sys.gettotalrefcount()``
Copy link

@eduardo-elizondo eduardo-elizondo Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't change! Or rather, it will change but not in the expected way.

This is something that I really looked into to make sure that the refleak tooling wouldn't break from this change. gettotalrefcount relies on _Py_RefTotal which is updated independently from the object's refcount field. Thus, an object's immortality will not make the value of gettotalrefcount be something really large.

Currently, I've placed the _Py_RefTotal update after the immortalization check. Thus, this value now reflects all the increfs/decrefs that did take place. If we move the check to before the immortalization check, it will now reflect all the attempts to incref and decrefs that took place (but they could have been a no-op). I chose the former since it's a bit unintuitive to have a returned value that's larger than the actual effective operations. This is enough to make the refcount leak tooling to work as is. However, if we choose the latter, it will work even in cases where we can upgrade a once mortal object to be immortal (which is beyond of the scope of any of the current changes in the PR). In this case, _Py_RefTotal will always be updated regardless of the the immortalization status of the instance, but the returned value of gettotalrefcount might make less sense as a whole.

The final consideration is that we need to make sure that _PySet_Dummy is not set to be immortal, otherwise, it will affect the implementation of _Py_GetRefTotal. Perhaps we should just remove the use of _PySet_Dummy and fix the gdb plugin in another way?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've mentioned that sys.gettotalrefcount() is unaffected but didn't include any of your explanation. If you think any of it would be helpful to PEP readers then we can add it in a follow-up.

pep-0683.rst Outdated Show resolved Hide resolved
pep-0683.rst Outdated Show resolved Hide resolved
Copy link

@eduardo-elizondo eduardo-elizondo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ericsnowcurrently I've added a bunch of comments. Up to you if you think we need to update the PEP to reflect what I mention there. Otherwise, looks good to me!

@ericsnowcurrently
Copy link
Member Author

@eduardo-elizondo Great! I'll make some updates and merge it. Thanks for the help.

@ericsnowcurrently ericsnowcurrently merged commit 4ba5757 into python:main Feb 15, 2022
@ericsnowcurrently ericsnowcurrently deleted the immortal-objects branch February 15, 2022 23:52
@CAM-Gerlach CAM-Gerlach changed the title PEP 683: Add a PEP for immortal objects. PEP 683: Add a PEP for immortal objects Feb 15, 2022
@CAM-Gerlach
Copy link
Member

Oops, too late on that change—glad I didn't start a full review. Just remember for the future, though—titles don't have periods in English, and neither do commit message summary lines (which is what the PR title maps to).

PEP: 683
Title: Immortal Objects, Using a Fixed Refcount
Author: Eric Snow <ericsnowcurrently@gmail.com>, Eddie Elizondo <eduardo.elizondorueda@gmail.com>
Discussions-To: python-dev@python.org
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the PEP with a link to the actual thread once you create it, so it is easy for others to find without a lot of guessing and Googling.

Also, there are a smattering of relatively non-critical editing issues (aside from the title containing a comma, which is a bit irregular), but as this was merged just as I got to it, I'm a bit late to the party now so if needed, I can make a separate PR fixing those (though they aren't terribly critical).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants