Skip to content

Use per-thread freelists in --disable-gil builds #111968

Description

@colesbury

Feature or enhancement

CPython uses freelists for frequently allocated Python objects, like dict, list, and slice. There freelists are generally stored in the per-interpreter state, which is not thread-safe without the GIL. In --disable-gil builds, the freelists should be stored in the per-thread state (i.e., PyThreadState). I think we probably want to keep the freelists in the interpreter state for the default build. This will probably require some refactoring.

Freelists:

  • float
  • slice (slice_cache)
  • tuple
  • list
  • dict (PyDictKeysObject and PyDictObject)
  • generator (value_freelist and asend_freelist)
  • PyContext

For context, here are similar changes in the nogil-3.12 fork, but I expect the changes in CPython 3.13 to be a bit different:

Linked PRs

Activity

  1. Yaro1 commented on Dec 10, 2023

    @Yaro1

    Hi!
    I'm new here, but if you don't mind I would like to take that issue

  2. colesbury commented on Dec 10, 2023

    @colesbury
    ContributorAuthor

    Hi @Yaro1, thanks for volunteering. You are welcome to take this on, but I don't think it's a great first issue. There are some subtleties and it also depends on #112776.

  3. Yaro1 commented on Dec 10, 2023

    @Yaro1

    Got it, thanks.
    Can I take please in that case some of the issues from #108219 ?

  4. colesbury commented on Dec 10, 2023

    @colesbury
    ContributorAuthor

    I don't think there are any good issues right now that are scoped out and not blocked on other work.

  5. Yaro1 commented on Dec 10, 2023

    @Yaro1

    Okay, got it. Thank you

  6. corona10 commented on Dec 27, 2023

    @corona10
    Member

    @colesbury I would like to take a look at this issue, Is there any pending issues that are related to this issue?

  7. colesbury commented on Dec 27, 2023

    @colesbury
    ContributorAuthor

    @corona10, thanks! I think the issues were fixed (problems with PyThreadState_Clear() being called by the wrong interpreter)

  8. self-assigned this
    on Dec 27, 2023
  9. added 2 commits that reference this issue on Dec 30, 2023
  10. corona10 commented on Jan 2, 2024

    @corona10
    Member

    #113584 will introduce seamless management of freelists that runtimes do not need to care about the implementation detail :)

    graph TD;
        PerThread --> FreeList;
        PerInterpreter --> FreeList;
        GC -- if free-threaded --> gc_freethreading;
        GC -- if default build --> gc_gil;
        gc_freethreading --> PerThread;
        gc_gil --> PerInterpreter;
        _PyFreeListState -- if free-threaded --> PerThread;
        _PyFreeListState -- if default build --> PerInterpreter;
        Runtime -- _PyFreeListState_GET --> _PyFreeListState;
        list --> Runtime;
        tuple --> Runtime;
        slice --> Runtime;
        float --> Runtime;
        dict --> Runtime;
        generator --> Runtime;
        PyContext --> Runtime;
    
    Loading
  11. 73 remaining items

  12. added 10 commits that reference this issue on Sep 2, 2024
  13. added 2 commits that reference this issue on Jan 22, 2025
  14. added a commit that references this issue on Jul 15, 2025
  15. added a commit that references this issue on Aug 4, 2025
  16. added a commit that references this issue on Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions