Skip to content

gh-160147: Optimize set_table_resize() on Free Threading - #163222

Open
vstinner wants to merge 1 commit into
python:mainfrom
vstinner:set_zero_table
Open

vstinner wants to merge 1 commit into
python:mainfrom
vstinner:set_zero_table

Conversation

@vstinner

@vstinner vstinner commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

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.

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.
@vstinner
vstinner requested a review from rhettinger as a code owner December 5, 2026 23:02
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Dec 5, 2026
@vstinner vstinner added topic-free-threading performance Performance or resource usage and removed type-feature A feature request or enhancement labels Dec 5, 2026
@vstinner

vstinner commented Dec 5, 2026

Copy link
Copy Markdown
Member Author

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 set(tuple) which is currently implemented as: create an empty tuple, iterate on the tuple using the iterator protocol (PyIter_Next()), and call PySet_Add() (set_add_key() to be exact) on each item.

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:

Benchmark ref calloc
set(range(10)) 293 ns 280 ns: 1.05x faster
set(range(100)) 2.01 us 1.69 us: 1.20x faster
set(range(1,000)) 15.2 us 13.5 us: 1.12x faster
set(range(10,000)) 179 us 159 us: 1.13x faster
set(range(100,000)) 2.00 ms 1.87 ms: 1.07x faster
Geometric mean (ref) 1.11x faster

The largest speedup is for 100 items: 1.20x faster!

I ran the benchmark on Fedora 45, Python built with ./configure --disable-gil (gcc -O3), with CPU isolation.

This change also makes comparison between my PySetBuilder API and the main branch more stable.

@vstinner

vstinner commented Dec 5, 2026 •

Copy link
Copy Markdown
Member Author

With my change, set_zero_table() is still called in 2 cases:

  • set_table_resize() if newsize == PySet_MINSIZE: the table is quite small in this case (8 items), so it shouldn't matter. I'm not sure that using memset() would be safe here. Also, shrinking a set() is a less common operation than growing a set.
  • set_empty_to_minsize() which is called by set_clear_internal(). set_clear_internal() is called without locking the set, so we should use the slower but safer set_zero_table(): memset()/calloc() cannot be used in this case.
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 set_zero_table() function.

commit c98182be8d47a69b9a43c08f58bac7a70c109cc9
Author: Neil Schemenauer <nas-github@arctrix.com>
Date:   Sat Dec 13 01:50:23 2025 -0800

    gh-132657: Add lock-free set contains implementation (#132290)
    
    This roughly follows what was done for dictobject to make a lock-free
    lookup operation. With this change, the set contains operation scales much
    better when used from multiple-threads. The frozenset contains performance
    seems unchanged (as already lock-free).

    (...)

cc @kumaraditya303 @rhettinger

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant