Perf: Use temporary set to to speed up list_update and list_difference_update - #4939
Open
nikolajmunk wants to merge 3 commits into
Open
Perf: Use temporary set to to speed up list_update and list_difference_update#4939nikolajmunk wants to merge 3 commits into
nikolajmunk wants to merge 3 commits into
Conversation
- capture to list before consuming them elsewhere
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.
Overview: What does this pull request change?
This PR uses a
setfor the repeated membership test performed inlist_updateandlist_difference_updateto improve performance. I've also written tests for these functions, since none existed.The new versions of both methods also explicitly support generator iterables, which is not currently the case despite the
Iterable[T]type hints.Motivation and Explanation: Why and how do your changes improve the library?
In their current implementations, both functions use this pattern:
Clearly, if
l2is a list, then the time complexity isO(len(l1) * len(l2))in the worst case. This small change massively improves performance whenl2is large.I wrote a quick script to benchmark the before and after, measured with cProfile:
Results with current implementation (total time 15.417 seconds):


Results with my implementation (total time 0.0232 seconds):
I haven't done a similar benchmark for small
l2but anecdotally, performance is more or less unchanged.Links to added or changed documentation pages
Further Information and Comments
It seems that there are simply no tests for any of the
utils/iterablesmethods? Let's hope the ones I wrote are a good enough start, then we can add more later :)Reviewer Checklist