GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Statically allocate interpreter states as much as possible. · Issue #90111 · python/cpython

Statically allocate interpreter states as much as possible. #90111

New issue
Copy link
New issue
Copy link
Closed
Closed
Statically allocate interpreter states as much as possible.#90111
Copy link
Assignees
ericsnowcurrently
Labels
3.11only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)

Description

@ericsnowcurrently
ericsnowcurrently
opened on Dec 1, 2021
Issue body actions
BPO 45953
Nosy @gvanrossum, @vstinner, @markshannon, @ericsnowcurrently, @JulienPalard, @miss-islington, @brandtbucher
PRs
  • bpo-45953: Statically allocate the main interpreter (and initial thread state). #29883
  • bpo-45953: Statically initialize the small ints. #30092
  • bpo-45953: Statically allocate and initialize global bytes objects. #30096
  • bpo-45953: Statically initialize all the _PyRuntimeState fields we can. #30588
  • bpo-45953: Statically initialize all the non-object PyInterpreterState fields we can. #30589
  • bpo-45953: Statically initialize all the PyThreadState fields we can. #30590
  • bpo-45953: Preserve backward compatibility on some PyThreadState field names. #31038
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ericsnowcurrently'
    closed_at = None
    created_at = <Date 2021-12-01.18:50:39.120>
    labels = ['interpreter-core', '3.11']
    title = 'Statically allocate interpreter states as much as possible.'
    updated_at = <Date 2022-03-29.07:04:09.685>
    user = 'https://github.com/ericsnowcurrently'

    bugs.python.org fields:

    activity = <Date 2022-03-29.07:04:09.685>
    actor = 'mdk'
    assignee = 'eric.snow'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2021-12-01.18:50:39.120>
    creator = 'eric.snow'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45953
    keywords = ['patch']
    message_count = 15.0
    messages = ['407476', '408507', '410315', '410441', '410536', '410537', '412046', '412213', '412291', '416159', '416161', '416170', '416208', '416210', '416245']
    nosy_count = 7.0
    nosy_names = ['gvanrossum', 'vstinner', 'Mark.Shannon', 'eric.snow', 'mdk', 'miss-islington', 'brandtbucher']
    pr_nums = ['29883', '30092', '30096', '30588', '30589', '30590', '31038']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue45953'
    versions = ['Python 3.11']

    Linked PRs

    • gh-90111: Minor Cleanup for Runtime-Global Objects #100254

    Activity

    1. ericsnowcurrently commented on Dec 1, 2021

      @ericsnowcurrently
      ericsnowcurrently
      on Dec 1, 2021
      MemberAuthor
      More actions

      Currently we allocate each new PyInterpreterState in PyInterpreterState_New(). Furthermore, PyInterpreterState is full of pointers which are almost all allocated on the heap during runtime init. We can statically allocate (and initialize?) most of what goes into PyInterpreterState, as well as the main interpreter itself (as part of _PyRuntimeState). This includes each interpreter's initial PyThreadState.

      TODO:

      • add PyInterpreterState main; to _PyRuntimeState.interpreters and use it
      • change references from the pointer to the value
      • add PyThreadState _main; to PyInterpreterState.threads and use it
      • change references from the pointer to the value
      • change PyInterpreterState pointer fields to values (as much as possible)
      • change PyThreadState pointer fields to values (as much as possible)

      benefits:

      • fewer possible failures (no memory) during runtime/interpreter/thread init
      • improved memory locality for pointers looked up relative to interpreter/thread state

      There is one non-trivial bit: embedding the various PyObject values in PyInterpreterState and PyThreadState means hard-coding the various pieces of the object there (e.g. for dict, its keys/values; for list, its array), as well as adding necessary init code to PyInterpreterState_New() and PyThreadState_New(). The resulting added complexity can be mitigated somewhat with macros or even code generation. (In fact, there is probably significant overlap with Guido's deepfreeze tool.) Regardless, we'll probably need to factor out init funcs for a number of object types, where currently there are only "Py*_New()" funcs that combine allocation and init.

    2. ericsnowcurrently
      added
      3.11only security fixesonly security fixes
      on Dec 1, 2021
    3. ericsnowcurrently
      self-assigned this
      on Dec 1, 2021
    4. ericsnowcurrently
      added
      interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
      3.11only security fixesonly security fixes
      on Dec 1, 2021
    5. ericsnowcurrently
      self-assigned this
      on Dec 1, 2021
    6. ericsnowcurrently
      added
      interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
      on Dec 1, 2021
    7. ericsnowcurrently commented on Dec 14, 2021

      @ericsnowcurrently
      ericsnowcurrently
      on Dec 14, 2021
      MemberAuthor
      More actions

      New changeset 121f1f8 by Eric Snow in branch 'main':
      bpo-45953: Statically initialize the small ints. (gh-30092)
      121f1f8

    8. ericsnowcurrently commented on Jan 11, 2022

      @ericsnowcurrently
      ericsnowcurrently
      on Jan 11, 2022
      MemberAuthor
      More actions

      New changeset cf496d6 by Eric Snow in branch 'main':
      bpo-45953: Statically allocate and initialize global bytes objects. (gh-30096)
      cf496d6

    9. ericsnowcurrently commented on Jan 12, 2022

      @ericsnowcurrently
      ericsnowcurrently
      on Jan 12, 2022
      MemberAuthor
      More actions

      New changeset ed57b36 by Eric Snow in branch 'main':
      bpo-45953: Statically allocate the main interpreter (and initial thread state). (gh-29883)
      ed57b36

    10. ericsnowcurrently commented on Jan 14, 2022

      @ericsnowcurrently
      ericsnowcurrently
      on Jan 14, 2022
      MemberAuthor
      More actions

      New changeset 324908b by Eric Snow in branch 'main':
      bpo-45953: Statically initialize all the PyThreadState fields we can. (gh-30590)
      324908b

    11. ericsnowcurrently commented on Jan 14, 2022

      @ericsnowcurrently
      ericsnowcurrently
      on Jan 14, 2022
      MemberAuthor
      More actions

      New changeset 322f962 by Eric Snow in branch 'main':
      bpo-45953: Statically initialize all the non-object PyInterpreterState fields we can. (gh-30589)
      322f962

    12. brandtbucher commented on Jan 29, 2022

      @brandtbucher
      brandtbucher
      on Jan 29, 2022
      Member
      More actions

      Any chance we could revert the recent renaming of tstate.exc_state and tstate.root_cframe in #30590? It broke Greenlet again:

      python-greenlet/greenlet#288

      If it's only a name change (and the members themselves are the same), I think reverting it is preferable to burying Greenlet in more compatibility macros and bugging them to put out another new release.

    13. ericsnowcurrently commented on Jan 31, 2022

      @ericsnowcurrently
      ericsnowcurrently
      on Jan 31, 2022
      MemberAuthor
      More actions

      Any chance we could revert the recent renaming of tstate.exc_state and tstate.root_cframe

      Yeah, I'll sort this out. Sorry for that.

    14. miss-islington commented on Feb 1, 2022

      @miss-islington
      miss-islington
      on Feb 1, 2022
      Contributor
      More actions

      New changeset f78be59 by Eric Snow in branch 'main':
      bpo-45953: Preserve backward compatibility on some PyThreadState field names. (GH-31038)
      f78be59

    15. JulienPalard commented on Mar 28, 2022

      @JulienPalard
      JulienPalard
      on Mar 28, 2022
      Member
      More actions

      Since 121f1f8, sys.getrefcount(1) is surprising:

          >>> __import__("sys").getrefcount(1)
          1000000210

      Should sys.getrefcount try to "fix" the value like by returning PyREFCNT(object) % 999999999? (But using a define to avoid the "magic, copy/pasted value").

    16. vstinner commented on Mar 28, 2022

      @vstinner
      vstinner
      on Mar 28, 2022
      Member
      More actions

      Should sys.getrefcount try to "fix" the value (...)

      https://peps.python.org/pep-0683/ would make it possible. Right now, I don't think that it's possible.

      Right now, a refcount of 1000000210 can be a real value, or it can be an immortal object.

    17. gvanrossum commented on Mar 28, 2022

      @gvanrossum
      gvanrossum
      on Mar 28, 2022
      Member
      More actions

      Please don’t try to “fix” anything. The value is only useful if you
      understand the implementation. It should map straightforwardly to what’s in
      memory.

      On Mon, Mar 28, 2022 at 05:16 STINNER Victor <[email protected]> wrote:

      STINNER Victor <[email protected]> added the comment:

      > Should sys.getrefcount try to "fix" the value (...)

      https://peps.python.org/pep-0683/ would make it possible. Right now, I
      don't think that it's possible.

      Right now, a refcount of 1000000210 can be a real value, or it can be an
      immortal object.

      ----------


      Python tracker <[email protected]>
      <https://bugs.python.org/issue45953\>


      --
      --Guido (mobile)

    18. JulienPalard commented on Mar 28, 2022

      @JulienPalard
      JulienPalard
      on Mar 28, 2022
      Member
      More actions

      Hum, and why 999999999? I am probably missing something obvious but 1 should be enough to ensure the value never hits 0. Except for refcount bugs obviously, but I don't think this is the right reason?

    19. gvanrossum commented on Mar 28, 2022

      @gvanrossum
      gvanrossum
      on Mar 28, 2022
      Member
      More actions

      I used 999999999 in deepfreeze.py to signify "immortal object". It has been copied by others (small integers are essentially immortal too). I wasn't too sure that the refcount wouldn't go below zero if the interpreter is repeatedly finalized and reinitialized. Once we have official immortal objects (see PEP-683) we should switch to that.

      Since you seem to be challenging the value of 999999999, my question to you is, why do you care what the refcount of 1 is?

    20. JulienPalard commented on Mar 29, 2022

      @JulienPalard
      JulienPalard
      on Mar 29, 2022
      Member
      More actions

      Since you seem to be challenging the value of 999999999, my question to you is, why do you care what the refcount of 1 is?

      Yesterday I was teaching Python, and we were speaking of integer immutability, names being "labels to objects" and so on, and I was showing the memory layout of all of this by hand on a whiteboard while "prooving" my drawings using an interpreter.

      While doing so came a question like "So, many modules can use the object int(1)?" So I answered yes, told that I expected many reuse of 1, and went importing sys.getrefcount to show them.

      And boom, it printed 1000000209 so I bugged for a few seconds, the value was obviously not the real refcount, and was also obviously bumped by a constant like 100000000, so I went inspecting why and found this commit.

      I have nothing against keeping 999999999, but in the other hand it could surprise other people, maybe we should at least document it near sys.getrefcount.

    21. ezio-melotti
      transferred this issue fromon Apr 10, 2022
    22. kumaraditya303 commented on Nov 8, 2022

      @kumaraditya303
      kumaraditya303
      on Nov 8, 2022
      Contributor
      More actions

      This looks completed, @ericsnowcurrently can this be closed now?

    23. ericsnowcurrently commented on Nov 8, 2022

      @ericsnowcurrently
      ericsnowcurrently
      on Nov 8, 2022
      MemberAuthor
      More actions

      We should add a note in the docs, as @JulienPalard recommended. Arguably, that should be part of #98154.

      Otherwise, I'll take a look soon to see if there's anything else left to do.

    24. ericsnowcurrently
      added a commit that references this issue on Dec 14, 2022

      gh-90111: Minor Cleanup for Runtime-Global Objects (gh-100254)

      aa8591e
    25. carljm
      added a commit that references this issue on Dec 16, 2022

      Merge branch 'main' into fastmock

      2260a54
    26. arnaudlegout commented on May 24, 2023

      @arnaudlegout
      arnaudlegout
      on May 24, 2023
      More actions

      Following @JulienPalard comments, I also faced the same issue during a Python course. Whatever is the meaning of refcount, the documentation of sys.getrefcount should be fixed. The doc currently says that

      Return the reference count of object.
      
      The count returned is generally one higher than you might expect,
      because it includes the (temporary) reference as an argument to
      getrefcount().
      

      For immortal objects, it is no more the case, the returned number is not the reference count.

      Reacted by Eric Snow
    27. ericsnowcurrently commented on May 24, 2023

      @ericsnowcurrently
      ericsnowcurrently
      on May 24, 2023
      MemberAuthor
      More actions

      See gh-98154.

    28. kumaraditya303 commented on Nov 15, 2025

      @kumaraditya303
      kumaraditya303
      on Nov 15, 2025
      Contributor
      More actions

      Closing as the docs were updated for immortal objects https://docs.python.org/3.14/library/sys.html#sys.getrefcount

    29. kumaraditya303
      closed this as completedon Nov 15, 2025
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    • @ericsnowcurrently
      ericsnowcurrently

    Labels

    3.11only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.