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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
9 changes: 9 additions & 0 deletions Lib/test/test_dict.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,15 @@ class MyFrozenDict(frozendict):
d = MyFrozenDict(x=1, y=2)
self.assertEqual(repr(d), "MyFrozenDict({'x': 1, 'y': 2})")

def test_hash(self):
# hash() doesn't rely on the items order
self.assertEqual(hash(frozendict(x=1, y=2)),
hash(frozendict(y=2, x=1)))

fd = frozendict(x=[1], y=[2])
with self.assertRaisesRegex(TypeError, "unhashable type: 'list'"):
hash(fd)


if __name__ == "__main__":
unittest.main()
56 changes: 39 additions & 17 deletions Objects/dictobject.c
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
Original file line number Diff line number Diff line change
Expand Up @@ -7869,33 +7869,55 @@ frozendict_repr(PyObject *self)
return res;
}

static Py_uhash_t
_shuffle_bits(Py_uhash_t h)
{
return ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
}

// Code copied from frozenset_hash()
static Py_hash_t
frozendict_hash(PyObject *op)
{
PyFrozenDictObject *self = _PyFrozenDictObject_CAST(op);
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.

if (shash != -1) {
return shash;
}

PyObject *items = _PyDictView_New(op, &PyDictItems_Type);
if (items == NULL) {
return -1;
}
PyObject *frozenset = PyFrozenSet_New(items);
Py_DECREF(items);
if (frozenset == NULL) {
return -1;
PyDictObject *mp = _PyAnyDict_CAST(op);
Py_uhash_t hash = 0;

PyObject *key, *value; // borrowed refs
Py_ssize_t pos = 0;
while (PyDict_Next(op, &pos, &key, &value)) {
Py_hash_t key_hash = PyObject_Hash(key);
if (key_hash == -1) {
return -1;
}
hash ^= _shuffle_bits(key_hash);

Py_hash_t value_hash = PyObject_Hash(value);
if (value_hash == -1) {
return -1;
}
hash ^= _shuffle_bits(value_hash);
}

hash = PyObject_Hash(frozenset);
Py_DECREF(frozenset);
if (hash == -1) {
return -1;
/* Factor in the number of active entries */
hash ^= ((Py_uhash_t)mp->ma_used + 1) * 1927868237UL;

/* Disperse patterns arising in nested frozendicts */
hash ^= (hash >> 11) ^ (hash >> 25);
hash = hash * 69069U + 907133923UL;

/* -1 is reserved as an error code */
if (hash == (Py_uhash_t)-1) {
hash = 590923713UL;
}

FT_ATOMIC_STORE_SSIZE_RELAXED(self->ma_hash, hash);
return hash;
FT_ATOMIC_STORE_SSIZE_RELAXED(self->ma_hash, (Py_hash_t)hash);
Comment thread
corona10 marked this conversation as resolved.
return (Py_hash_t)hash;
}


Expand Down
5 changes: 4 additions & 1 deletion Objects/setobject.c
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
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,10 @@ _shuffle_bits(Py_uhash_t h)

This hash algorithm can be used on either a frozenset or a set.
When it is used on a set, it computes the hash value of the equivalent
frozenset without creating a new frozenset object. */
frozenset without creating a new frozenset object.

If you update this code, update also frozendict_hash() which copied this
code. */

static Py_hash_t
frozenset_hash_impl(PyObject *self)
Expand Down
Loading

Back | FazBrowse Home | New Git URL