Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Lib/test/test_dict.py
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
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
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
Member Author

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
Member Author

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

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);
return (Py_hash_t)hash;
}


Expand Down
5 changes: 4 additions & 1 deletion Objects/setobject.c
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