gh-132213: use relaxed atomics for set hash (#132215)

This commit is contained in:
Kumar Aditya 2025-04-07 21:59:24 +05:30 committed by GitHub
parent fd1b98dd1d
commit ed99e28d5b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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: