gh-153060: Add an empty frozendict singleton#153061
Open
vstinner wants to merge 6 commits into
Open
Conversation
The singleton is created immortal to avoid refcount contention in Free Threading. * Add PyInterpreterState.dict_state.empty_frozendict. * Add _PyDict_Init() and _PyDict_Fini() to create and destroy the empty frozendict singleton.
Member
Author
|
Examples: >>> empty = frozendict()
>>> frozendict() is empty
True
>>> frozendict([]) is empty
True
>>> (empty | empty) is empty
True
>>> (empty | {}) is empty
True
>>> frozendict.fromkeys('') is empty
True |
Member
Author
|
cc @corona10 |
corona10
reviewed
Jul 5, 2026
corona10
left a comment
Member
There was a problem hiding this comment.
Overall looks good to me, but give me enough time to take a look at see the detail.
Member
Author
|
I ran a microbenchmark on Linux with CPU isolation on this change:
I'm surprised by the slowdown on these operations:
Details |
ZeroIntensity
left a comment
Member
There was a problem hiding this comment.
I think it would make sense to also add this to Py_GetConstantBorrowed.
| PyMutex watcher_mutex; // Protects the watchers array (free-threaded builds) | ||
| _PyOnceFlag watcher_setup_once; // One-time optimizer watcher setup | ||
| PyDict_WatchCallback watchers[DICT_MAX_WATCHERS]; | ||
| PyObject *empty_frozendict; |
Member
There was a problem hiding this comment.
Singletons are usually supposed to be stored on _PyRuntime.static_objects.singletons, not on the interpreter state. Is there a reason we're deviating from that convention here?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The singleton is created immortal to avoid refcount contention in Free Threading.