Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Optimize :c:func:`PySet_Add` for uniquely referenced sets in :term:`free
threaded <free threading>` build.
24 changes: 15 additions & 9 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2773,17 +2773,23 @@ PySet_Discard(PyObject *set, PyObject *key)
int
PySet_Add(PyObject *anyset, PyObject *key)
{
if (!PySet_Check(anyset) &&
(!PyFrozenSet_Check(anyset) || !_PyObject_IsUniquelyReferenced(anyset))) {
PyErr_BadInternalCall();
return -1;
if (_PyObject_IsUniquelyReferenced(anyset) && PyAnySet_Check(anyset)) {
// We can only change frozensets if they are uniquely referenced,
// and we can avoid locking sets even in free-threaded build if they
// are uniquely referenced.
return set_add_key((PySetObject *)anyset, key);
}

int rv;
Py_BEGIN_CRITICAL_SECTION(anyset);
rv = set_add_key((PySetObject *)anyset, key);
Py_END_CRITICAL_SECTION();
return rv;
if (PySet_Check(anyset)) {
int rv;
Py_BEGIN_CRITICAL_SECTION(anyset);
rv = set_add_key((PySetObject *)anyset, key);
Py_END_CRITICAL_SECTION();
return rv;
}

PyErr_BadInternalCall();
return -1;
}

int
Expand Down
Loading