Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Only for frozenset
  • Loading branch information
yoney committed Oct 23, 2025
commit 0932e1641181891ca904149183f8b4dc1508740f
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Optimize :c:func:`PySet_Add` for uniquely referenced sets in :term:`free
threaded <free threading>` build.
Optimize :c:func:`PySet_Add` for :class:`frozenset` in :term:`free threaded
<free threading>` build.
15 changes: 8 additions & 7 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2773,13 +2773,6 @@ PySet_Discard(PyObject *set, PyObject *key)
int
PySet_Add(PyObject *anyset, PyObject *key)
{
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);
}

if (PySet_Check(anyset)) {
int rv;
Py_BEGIN_CRITICAL_SECTION(anyset);
Expand All @@ -2788,6 +2781,14 @@ PySet_Add(PyObject *anyset, PyObject *key)
return rv;
}

if (PyFrozenSet_Check(anyset) || _PyObject_IsUniquelyReferenced(anyset)) {
// We can only change frozensets if they are uniquely referenced. The
// API limits the usage of `PySet_Add` to "fill in the values of brand
// new frozensets before they are exposed to other code". In this case,
// this can be done without a lock.
return set_add_key((PySetObject *)anyset, key);
}

PyErr_BadInternalCall();
return -1;
}
Expand Down
Loading