https://github.com/python/cpython/commit/ed99e28d5b5613f7c13691fd854195719d7bff1f
commit: ed99e28d5b5613f7c13691fd854195719d7bff1f
branch: main
author: Kumar Aditya <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2025-04-07T21:59:24+05:30
summary:
gh-132213: use relaxed atomics for set hash (#132215)
files:
M Objects/setobject.c
diff --git a/Objects/setobject.c b/Objects/setobject.c
index f65d458f7924c3..acbb53aafc0a26 100644
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -417,7 +417,7 @@ set_empty_to_minsize(PySetObject *so)
FT_ATOMIC_STORE_SSIZE_RELAXED(so->used, 0);
so->mask = PySet_MINSIZE - 1;
so->table = so->smalltable;
- so->hash = -1;
+ FT_ATOMIC_STORE_SSIZE_RELAXED(so->hash, -1);
}
static int
@@ -1243,10 +1243,12 @@ set_swap_bodies(PySetObject *a, PySetObject *b)
if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
- h = a->hash; a->hash = b->hash; b->hash = h;
+ h = FT_ATOMIC_LOAD_SSIZE_RELAXED(a->hash);
+ FT_ATOMIC_STORE_SSIZE_RELAXED(a->hash,
FT_ATOMIC_LOAD_SSIZE_RELAXED(b->hash));
+ FT_ATOMIC_STORE_SSIZE_RELAXED(b->hash, h);
} else {
- a->hash = -1;
- b->hash = -1;
+ FT_ATOMIC_STORE_SSIZE_RELAXED(a->hash, -1);
+ FT_ATOMIC_STORE_SSIZE_RELAXED(b->hash, -1);
}
}
@@ -2141,9 +2143,9 @@ set_richcompare(PyObject *self, PyObject *w, int op)
case Py_EQ:
if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Py_RETURN_FALSE;
- if (v->hash != -1 &&
- ((PySetObject *)w)->hash != -1 &&
- v->hash != ((PySetObject *)w)->hash)
+ Py_hash_t v_hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(v->hash);
+ Py_hash_t w_hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(((PySetObject
*)w)->hash);
+ if (v_hash != -1 && w_hash != -1 && v_hash != w_hash)
Py_RETURN_FALSE;
return set_issubset((PyObject*)v, w);
case Py_NE:
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]