Skip to content

gh-160147: Add PySetBuilder C API - #162582

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

vstinner wants to merge 9 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
📖 51 files modified

Preview · build overview · cpython-previews · built Dec. 4, 2026, 6:32 p.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.

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