GeezerVison is showing github.com — Use per-thread freelists in `--disable-gil` builds · Issue #111968 · python/cpython
Use per-thread freelists in --disable-gil builds #111968DescriptionFeature 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
ActivityYaro1 commented on Dec 10, 2023 Hi!
I'm new here, but if you don't mind I would like to take that issue colesbury commented on Dec 10, 2023 ContributorAuthor More actions 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. Yaro1 commented on Dec 10, 2023 Got it, thanks.
Can I take please in that case some of the issues from #108219 ? colesbury commented on Dec 10, 2023 ContributorAuthor More actions I don't think there are any good issues right now that are scoped out and not blocked on other work. Yaro1 commented on Dec 10, 2023 corona10 commented on Dec 27, 2023 @colesbury I would like to take a look at this issue, Is there any pending issues that are related to this issue? colesbury commented on Dec 27, 2023 ContributorAuthor More actions @corona10, thanks! I think the issues were fixed (problems with PyThreadState_Clear() being called by the wrong interpreter) corona10 commented on Jan 2, 2024 #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
Reacted by Erlend E. Aasland Reacted by Erlend E. Aasland and Łukasz Langa added a commit that references this issue on Jul 15, 2025gh-111968: remove redundant fetching of interpreter state in `dict` i…
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|