Skip to content

Commit 0932e16

Browse files
committed
Only for frozenset
1 parent 48a4017 commit 0932e16

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Optimize :c:func:`PySet_Add` for uniquely referenced sets in :term:`free
2-
threaded <free threading>` build.
1+
Optimize :c:func:`PySet_Add` for :class:`frozenset` in :term:`free threaded
2+
<free threading>` build.

Objects/setobject.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,13 +2773,6 @@ PySet_Discard(PyObject *set, PyObject *key)
27732773
int
27742774
PySet_Add(PyObject *anyset, PyObject *key)
27752775
{
2776-
if (_PyObject_IsUniquelyReferenced(anyset) && PyAnySet_Check(anyset)) {
2777-
// We can only change frozensets if they are uniquely referenced,
2778-
// and we can avoid locking sets even in free-threaded build if they
2779-
// are uniquely referenced.
2780-
return set_add_key((PySetObject *)anyset, key);
2781-
}
2782-
27832776
if (PySet_Check(anyset)) {
27842777
int rv;
27852778
Py_BEGIN_CRITICAL_SECTION(anyset);
@@ -2788,6 +2781,14 @@ PySet_Add(PyObject *anyset, PyObject *key)
27882781
return rv;
27892782
}
27902783

2784+
if (PyFrozenSet_Check(anyset) || _PyObject_IsUniquelyReferenced(anyset)) {
2785+
// We can only change frozensets if they are uniquely referenced. The
2786+
// API limits the usage of `PySet_Add` to "fill in the values of brand
2787+
// new frozensets before they are exposed to other code". In this case,
2788+
// this can be done without a lock.
2789+
return set_add_key((PySetObject *)anyset, key);
2790+
}
2791+
27912792
PyErr_BadInternalCall();
27922793
return -1;
27932794
}

0 commit comments

Comments
 (0)