You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Performance regression in PySet_Add() with 3.14t #140476
In the fbthrift-pythonbenchmarks, we observed a performance regression when comparing Python 3.14t to 3.14, particularly during Thrift struct initialization and deserialization. In our tests, we populated sets with 1000 elements. The regression was linked to the use of the PySet_Add() API for building set and frozenset objects, where a critical section is always applied—even when the set is uniquely referenced during initialization.
I am not sure that it is correct to remove critical section from such public API.
Considering a case of a wrapper object which stores data in an underlying set, while the set will always have refcount of 1 but the wrapper can be shared among multiple threads. If the wrapper object tries to modify the set from multiple threads then it can lead to data races with the owning thread which would avoids critical section and non-owning threads which would use critical section.
I am not sure that it is correct to remove critical section from such public API.
Considering a case of a wrapper object which stores data in an underlying set, while the set will always have refcount of 1 but the wrapper can be shared among multiple threads. If the wrapper object tries to modify the set from multiple threads then it can lead to data races with the owning thread which would avoids critical section and non-owning threads which would use critical section.
I was considering/testing a similar scenario for set, but I’m wondering if it also applies to frozenset. The API documentation mentions a special condition
... like PyTuple_SetItem() it can be used to fill in the values of brand new frozensets before they are exposed to other code ...
Yes, PySet_Add can be used to add elements to frozenset initially, it is user's responsibility that the frozen set is not exposed and is local so optimizing that is safe.
Activity
kumaraditya303 commentedon Oct 23, 2025
I am not sure that it is correct to remove critical section from such public API.
Considering a case of a wrapper object which stores data in an underlying set, while the set will always have refcount of 1 but the wrapper can be shared among multiple threads. If the wrapper object tries to modify the set from multiple threads then it can lead to data races with the owning thread which would avoids critical section and non-owning threads which would use critical section.
yoney commentedon Oct 23, 2025
I was considering/testing a similar scenario for
set, but I’m wondering if it also applies tofrozenset. The API documentation mentions a special conditionkumaraditya303 commentedon Oct 24, 2025
Yes,
PySet_Addcan be used to add elements tofrozensetinitially, it is user's responsibility that the frozen set is not exposed and is local so optimizing that is safe.gh-140476: optimize `PySet_Add` for `frozenset` in free-threading (#1…
pythongh-140476: optimize `PySet_Add` for `frozenset` in free-threadi…