Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 7, 2025
Merged
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
16 changes: 9 additions & 7 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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:
Expand Down
Loading