Repository navigation
Conversation
Replace slow (but safe) PyMem_Malloc()+set_zero_table() with faster PyMem_Calloc(). On Free Threading, set_zero_table() uses _Py_atomic_store_ptr_release() and _Py_atomic_store_ssize_relaxed(). Reorder also operations in set_zero_table() to write tables in the same order than the structure: first set the key, then set the hash.
|
On Free Threading, I identified a performance bottleneck in set_table_resize(), called by set_table_resize(), when running many benchmarks on my PySetBuilder implementation. When creating a new set, most time was spent only in set_zero_table()! Benchmark on bench.py: import pyperf
runner = pyperf.Runner()
for size in (10, 100, 1000, 10_000, 100_000):
items = tuple(range(size))
runner.bench_func(f'set(range({size:,}))', set, items)Results:
The largest speedup is for 100 items: 1.20x faster! I ran the benchmark on Fedora 45, Python built with This change also makes comparison between my |
|
With my change,
static int
set_clear_internal(PyObject *self)
{
// Do not check if the set is locked since this function is called by
// the garbage collector (via tp_clear) and by _PySet_ClearInternal()
// which don't lock the set.
...
}cc @nascheme who made commit c98182b last year which adds |
Replace slow (but safe) PyMem_Malloc()+set_zero_table() with faster PyMem_Calloc(). On Free Threading, set_zero_table() uses _Py_atomic_store_ptr_release() and _Py_atomic_store_ssize_relaxed().
Reorder also operations in set_zero_table() to write tables in the same order than the structure: first set the key, then set the hash.
PyFrozenWriterC API to create afrozensetobject #160147