Skip to content

gh-160147: Add PySetBuilder C API - #162582

Open
vstinner wants to merge 10 commits into
python:mainfrom
vstinner:setbuilder
Open

vstinner wants to merge 10 commits into
python:mainfrom
vstinner:setbuilder

Conversation

@vstinner

@vstinner vstinner commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

This PR is based on PR gh-160154. It renames PyFrozenSetWriter to PyFrozenSetBuilder, and adds the ability to also create set objects, not only frozenset objects.

Use the new API in:

  • Modules/_abc.c
  • Modules/_asynciomodule.c
  • Modules/_hashopenssl.c
  • Modules/_ssl.c

When building a set, PySet_Add() suffers from the same performance issue than PyList_Add() on Free Threading (issue gh-158660): each call needs a critical section.

@read-the-docs-community

read-the-docs-community Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
📖 60 files modified

Preview · build overview · cpython-previews · built Dec. 6, 2026, 1:24 a.m. UTC

@vstinner

Copy link
Copy Markdown
Member Author

I ran a benchmark on Fedora 45 with CPU isolation and Python built with Free Threading: ./configure --disable-gil && make (gcc -O3).

The benchmark only create set objects. See #160154 (comment) for benchmarks on frozenset objects.

Benchmark files:

Results all together:

Benchmark set_new set_add writer_add writer_update writer_update_array
size=0 88.1 ns 45.6 ns: 1.93x faster 57.2 ns: 1.54x faster 101 ns: 1.15x slower 59.8 ns: 1.47x faster
size=4 175 ns 182 ns: 1.04x slower 128 ns: 1.37x faster 195 ns: 1.11x slower 121 ns: 1.44x faster
size=100 3.15 us 4.57 us: 1.45x slower 3.17 us: 1.01x slower 3.73 us: 1.18x slower 1.95 us: 1.62x faster
size=1,000 23.6 us 39.2 us: 1.66x slower 21.6 us: 1.09x faster 26.6 us: 1.13x slower 18.3 us: 1.29x faster
size=100,000 3.27 ms 4.81 ms: 1.47x slower not significant 4.71 ms: 1.44x slower 2.46 ms: 1.33x faster
Geometric mean (ref) 1.14x slower 1.18x faster 1.20x slower 1.43x faster

Columns:

  • set_new: Call PySet_New(tuple): call PyObject_GetIter() and then loop on PyIter_Next()
  • set_add: Call PySet_New(NULL) + PySet_Add() once per item
  • writer_add: Call PySetBuilder_CreateSet(0) + PySetBuilder_Add() once per item
  • writer_update: Call PySetBuilder_CreateSet(0) + PySetBuilder_Update(tuple): call PyObject_GetIter() and then loop on PyIter_Next()
  • writer_update_array: Call PySetBuilder_CreateSet(0) + PySetBuilder_UpdateArray()

Compare PySetBuilder_Add() to PySet_Add(): it's faster since it doesn't need a critical section.

Benchmark set_add writer_add
size=0 45.6 ns 57.2 ns: 1.25x slower
size=4 182 ns 128 ns: 1.42x faster
size=100 4.57 us 3.17 us: 1.44x faster
size=1,000 39.2 us 21.6 us: 1.81x faster
size=100,000 4.81 ms 3.29 ms: 1.46x faster
Geometric mean (ref) 1.34x faster

Compare PySetBuilder_Update(tuple) to PySet_New(tuple): it's slower since PySetBuilder is just an abstraction on top of the PySet API, it doesn't bring any optimization in this case, and PySetBuilder_Finish() has to scan again items to decide to track the set in the GC or not.

Benchmark set_new writer_update
size=0 88.1 ns 101 ns: 1.15x slower
size=4 175 ns 195 ns: 1.11x slower
size=100 3.15 us 3.73 us: 1.18x slower
size=1,000 23.6 us 26.6 us: 1.13x slower
size=100,000 3.27 ms 4.71 ms: 1.44x slower
Geometric mean (ref) 1.20x slower

Compare PySetBuilder_UpdateArray() to PySet_New(tuple) and PySet_Add(): it's faster since it can preallocate the set table at once, and then add all items at once. Also, it doesn't need a critical section.

Benchmark set_new set_add writer_update_array
size=0 88.1 ns 45.6 ns: 1.93x faster 59.8 ns: 1.47x faster
size=4 175 ns 182 ns: 1.04x slower 121 ns: 1.44x faster
size=100 3.15 us 4.57 us: 1.45x slower 1.95 us: 1.62x faster
size=1,000 23.6 us 39.2 us: 1.66x slower 18.3 us: 1.29x faster
size=100,000 3.27 ms 4.81 ms: 1.47x slower 2.46 ms: 1.33x faster
Geometric mean (ref) 1.14x slower 1.43x faster

Use the new API in:

* Modules/_abc.c
* Modules/_asynciomodule.c
* Modules/_hashopenssl.c
* Modules/_ssl.c
@vstinner vstinner changed the title gh-158660: Add PySetBuilder C API gh-160147: Add PySetBuilder C API Nov 24, 2026
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 24, 2026
@vstinner

Copy link
Copy Markdown
Member Author

Tests / iOS (free-threading) (pull_request): Failing after 32m

Unrelated failure. A FTP test of test_urllib2net failed with "425 Security: Bad IP connecting":

ERROR: test_ftp_no_leak (test.test_urllib2net.OtherNetworkTests.test_ftp_no_leak) (url='ftp://www.pythontest.net/README')
urllib.error.URLError: <urlopen error ftp error: 425 Security: Bad IP connecting.>

Comment thread Modules/_abc.c
{
int ret = -1;
PyObject *abstracts = PyFrozenSet_New(NULL);
PySetBuilder *abstracts = PySetBuilder_CreateFrozenSet(0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly don't like the naming PySetBuilder_CreateFrozenSet but I don't have anything better to suggest. It's... a bit weird to have the combination of a builder and "create" at the same time. Maybe PySetBuilder_ForFrozenSet though PySetBuilder_ForSet is really weird as well.

If you plan to have more builder APIs such as PyFastSequenceBuilder_CreateList()/CreateTuple, feel free to ignore this comment though.

@vstinner

Copy link
Copy Markdown
Member Author

The API is being discussed at capi-workgroup/decisions#119.

Move PySet_MINSIZE constant at the module level
If set_add_entry_takeref() fails, Py_DECREF(item) must not be called.
ASSERT_CAN_MODIFY_SET() now uses Py_REFCNT()==1 instead of checking
_PyObject_IsUniquelyReferenced().
@rhettinger

rhettinger commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

My preference (which I know doesn't count for much) is to not expand the C-API unless we have to. Unless we expectmostl future C code to be created by AI, we should respect that humans have a much smaller context window.

The current PySet_New() and PySet_Add() have a near zero learning curve, in part because they are simple and in part because they correspond to how we write code in pure Python. That simple API was modeled after existing battletested APIs for other type.

The builder API is more complex. Aside from minute timing differences, the proposal seems gratuitous.

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 commented Dec 6, 2026

Copy link
Copy Markdown
Member Author

I ran benchmarks again with a similar configuration than the previous benchmark. There are 3 changes since the previous benchmark:

Results:

Benchmark set_new set_add builder_add builder_update builder_update_array
size=0 109 ns 55.9 ns: 1.96x faster 60.0 ns: 1.82x faster 120 ns: 1.10x slower 63.0 ns: 1.74x faster
size=4 199 ns 204 ns: 1.02x slower 146 ns: 1.37x faster 215 ns: 1.08x slower 134 ns: 1.49x faster
size=100 2.87 us 4.48 us: 1.56x slower 2.63 us: 1.09x faster 2.89 us: 1.00x slower 1.76 us: 1.63x faster
size=1,000 23.2 us 40.7 us: 1.75x slower 21.3 us: 1.09x faster not significant 17.2 us: 1.35x faster
size=100,000 3.19 ms 4.83 ms: 1.52x slower 2.90 ms: 1.10x faster not significant 2.12 ms: 1.50x faster
Geometric mean (ref) 1.17x slower 1.27x faster 1.04x slower 1.54x faster

Add: On Free Threading, PySetBuilder_Add() is 1.48x faster than PySet_Add()

Benchmark set_add builder_add
size=0 55.9 ns 60.0 ns: 1.07x slower
size=4 204 ns 146 ns: 1.40x faster
size=100 4.48 us 2.63 us: 1.70x faster
size=1,000 40.7 us 21.3 us: 1.91x faster
size=100,000 4.83 ms 2.90 ms: 1.67x faster
Geometric mean (ref) 1.48x faster

Update: PySetBuilder_Update(tuple) is 1.04x slower than PySet_New(tuple) -- on this use case, PySetBuilder is not faster, but a little bit slower

Benchmark set_new builder_update
size=0 109 ns 120 ns: 1.10x slower
size=4 199 ns 215 ns: 1.08x slower
size=100 2.87 us 2.89 us: 1.00x slower
Geometric mean (ref) 1.04x slower

Benchmark hidden because not significant (2): size=1,000, size=100,000

UpdateArray: PySetBuilder_UpdateArray() is 1.54x faster faster than PySet_New(tuple). The comparison in unfair, PySet_New(tuple) uses the slow iterator protocol on the tuple (PyIter_Next()).

Benchmark set_new set_add builder_update_array
size=0 109 ns 55.9 ns: 1.96x faster 63.0 ns: 1.74x faster
size=4 199 ns 204 ns: 1.02x slower 134 ns: 1.49x faster
size=100 2.87 us 4.48 us: 1.56x slower 1.76 us: 1.63x faster
size=1,000 23.2 us 40.7 us: 1.75x slower 17.2 us: 1.35x faster
size=100,000 3.19 ms 4.83 ms: 1.52x slower 2.12 ms: 1.50x faster
Geometric mean (ref) 1.17x slower 1.54x faster

@vstinner

vstinner commented Dec 6, 2026

Copy link
Copy Markdown
Member Author

@rhettinger:

My preference (which I know doesn't count for much) is to not expand the C-API unless we have to.

The rationale for adding PyListBuilder was to fix a performance issue on Free Threading. On the stable ABI, PyList_SetItem() is 4.6x and 6.7x slower than PyList_SET_ITEM() (without the stable ABI).

Sadly, PySet_Add() has a similar performance issue on Free Threading, since it also has to lock the set to add a single item. See the benchmark above: on Free Threading, PySetBuilder_Add() is 1.48x faster than PySet_Add(). PySetBuilder_Add() avoids locking, it's safe since the set cannot be accessed directly before PySetBuilder_Finish().

Aside from minute timing differences, the proposal seems gratuitous.

I'm not sure how you consider that a performance difference is significant or not. But PySetBuilder_UpdateArray() is 1.54x faster faster than PySet_New(tuple). For me, it's quite significant. Currently, there is no PySet C API to pass directly an array of Python object.

@vstinner

vstinner commented Dec 6, 2026

Copy link
Copy Markdown
Member Author

I pushed multiple fixes and changes:

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

Labels

awaiting core review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants