Skip to content

gh-141510: Optimize hash(frozendict) - #144919

Merged
vstinner merged 2 commits into
python:mainfrom
vstinner:frozendict_hash
Feb 17, 2026
Merged

gh-141510: Optimize hash(frozendict)#144919
vstinner merged 2 commits into
python:mainfrom
vstinner:frozendict_hash

Conversation

@vstinner

@vstinnervstinner commented Feb 17, 2026

Copy link
Copy Markdown
Member

hash(frozendict) no longer creates a temporary items view and a temporary frozenset object.

Copy frozenset_hash() code to frozendict_hash().

hash(frozendict) no longer creates a temporary items view and a
temporary frozenset object.
Copy frozenset_hash() code to frozendict_hash().
Comment threadObjects/dictobject.c
Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(self->ma_hash);
if (hash != -1) {
return hash;
Py_hash_t shash = FT_ATOMIC_LOAD_SSIZE_RELAXED(self->ma_hash);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why atomic operation is needed?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

PyFrozenDictObject.ma_hash is mutable and so it needs a lock to handle properly concurrent access.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

It's the same for the Unicode strings hash: the string is immutable, but the hash member is mutable. So unicode_hash() uses atomic operations to get and set the hash member.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah got it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, the hash is "lazily" computed so we need to use relaxed atomics here.

Comment threadObjects/dictobject.c

@corona10corona10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm!

Comment threadObjects/dictobject.c Outdated
@vstinner
vstinner merged commit 8e211b1 into python:mainFeb 17, 2026
45 checks passed
@vstinner
vstinner deleted the frozendict_hash branch February 17, 2026 17:39
@vstinner

Copy link
Copy Markdown
MemberAuthor

Merged, thanks for reviews!

brijkapadia pushed a commit to brijkapadia/cpython that referenced this pull request Feb 28, 2026
hash(frozendict) no longer creates a temporary items view and a
temporary frozenset object.
Copy frozenset_hash() code to frozendict_hash().
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
hash(frozendict) no longer creates a temporary items view and a
temporary frozenset object.
Copy frozenset_hash() code to frozendict_hash().
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@vstinner@corona10@kumaraditya303