gh-156548: Performance - Presize sets built from sized iterables - #156549
Closed
brittanyrey wants to merge 3 commits into
Closed
gh-156548: Performance - Presize sets built from sized iterables#156549brittanyrey wants to merge 3 commits into
brittanyrey wants to merge 3 commits into
Conversation
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.
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. |
Contributor
Author
|
I'm going to abandon this one |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
set()andset.update()already presize their underlying containers when they can infer the expected container size from anothersetordict, 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.
Note: In the no hint case, theres about a ~8ns fixed overhead to call
PyObject_LengthHintand discard the output.Benchmark script