Skip to content

gh-156548: Performance - Presize sets built from sized iterables - #156549

Closed
brittanyrey wants to merge 3 commits into
python:mainfrom
brittanyrey:perf-set-presize-iterable
Closed

gh-156548: Performance - Presize sets built from sized iterables#156549
brittanyrey wants to merge 3 commits into
python:mainfrom
brittanyrey:perf-set-presize-iterable

Conversation

@brittanyrey

@brittanyrey brittanyrey commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

set() and set.update() already presize their underlying containers when they can infer the expected container size from another set or dict, but they fall back to incremental growth for all other iterables.

Since we can also retrieve the size of lists and tuples, we can extend the same logic to sized iterables to reduce container resizing overhead. Iterables that have no clear size (i.e. generators) will remain unaffected.

For this PR, I first refactored the two existing explicit presize calls into a helper function to be shared for the sized iterables as well.

Benchmark main PR
set(list[str]) 10k 158 us 119 us: 1.32x faster
set(list[str]) 1M 88.2 ms 68.7 ms: 1.28x faster
set(tuple[str]) 1M 85.6 ms 66.9 ms: 1.28x faster
set(list[int]) 10k 117 us 95.3 us: 1.23x faster
set(list[int]) 1M 24.9 ms 20.6 ms: 1.21x faster
set(tuple[int]) 1M 23.8 ms 19.9 ms: 1.20x faster
set.update(list) 1M 24.2 ms 20.5 ms: 1.18x faster
frozenset(list) 1M 26.6 ms 22.6 ms: 1.18x faster
set(genexpr) 1M [no hint] 59.4 ms 60.3 ms: 1.02x slower

Note: In the no hint case, theres about a ~8ns fixed overhead to call PyObject_LengthHint and discard the output.

Benchmark script
"""set/frozenset built from a sized iterable (Objects/setobject.c)."""
import pyperf

def mkset(s): 
  set(s)

def mkfrozen(s): 
  frozenset(s)

def upd(s): 
  d = set(); d.update(s)

def gen(n): 
  set(x for x in range(n))

if __name__ == "__main__":
    r = pyperf.Runner()
    ints = list(range(1000000)); strs = [str(i) for i in ints]
    r.bench_func("set(list[int]) 1M", mkset, ints)
    r.bench_func("set(list[str]) 1M", mkset, strs)
    r.bench_func("frozenset(list) 1M", mkfrozen, ints)
    r.bench_func("set.update(list) 1M", upd, ints)
    r.bench_func("set(list) 10k", mkset, list(range(10000)))
    r.bench_func("set(genexpr) 1M [no hint]", gen, 1000000)

set() and set.update() do one big resize up front when the source is
another set or a dict, but fall back to incremental growth for every
other iterable.  Building a set from a list or tuple therefore rebuilds
the table log2(n/8) times, re-probing every live entry on each pass.

Use the operand's length hint to presize on that path too.  Hintless
iterables such as generators are unaffected, and a type whose __len__ or
__length_hint__ fails is still built exactly as before, just without the
presize, so nothing observable changes.

The resize policy, which was already spelled out twice, moves into
set_presize() so all three call sites share it along with a guard
against a __length_hint__ large enough to overflow the arithmetic.

| benchmark          |    before |     after | change |
|--------------------|----------:|----------:|-------:|
| set(list[int]) 1M  |  25.76 ms |  21.27 ms |   -17% |
| set(list[str]) 1M  |  97.81 ms |  89.93 ms |    -8% |
| set(tuple[int]) 1M |  26.51 ms |  20.92 ms |   -21% |
| frozenset(list) 1M |  30.38 ms |  24.15 ms |   -21% |
| s.update(list) 1M  |  26.66 ms |  20.55 ms |   -23% |
| set(list[int]) 10k | 132.59 us | 119.74 us |   -10% |
| set(list[str]) 10k | 179.60 us | 146.24 us |   -19% |
| set(genexpr) 1M    |  40.35 ms |  40.80 ms |    +1% |
| set(list) 3 elems  |   0.10 us |   0.10 us |    +1% |
| s.update(list) 3   |   0.08 us |   0.08 us |    +2% |

The last two rows are the cost of the added length-hint lookup, a fixed
~8ns per call on the iterable path.
@pochmann

pochmann commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

List elements aren't expected to be unique, though. I'd say a major use case for turning a list into a set is precisely that you do have duplicates (and want to remove them). So your tests with lists of only unique elements seem rather unrealistic.

@brittanyrey

Copy link
Copy Markdown
Contributor Author

I'm going to abandon this one

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.

2 participants