FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-141510: Optimize hash(frozendict) by vstinner · Pull Request #144919 · python/cpython · GitHub

/ cpython Public

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 commented Feb 17, 2026
edited by bedevere-app Bot
Loading

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 thread Objects/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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Why atomic operation is needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Comment thread Objects/dictobject.c

corona10 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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

lgtm!

Comment thread Objects/dictobject.c Outdated
vstinner merged commit 8e211b1 into python:main Feb 17, 2026
45 checks passed
vstinner deleted the frozendict_hash branch February 17, 2026 17:39

Copy link
Copy Markdown
Member Author

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().
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL