Skip to content

Improve performance of set membership testing from set arguments #121795

Closed

Description

@HarryLHW

Feature or enhancement

Proposal:

For set s and set elem, the operations elem in s, s.remove(elem), and s.discard(elem) are equivalent to frozenset(elem) in s, s.remove(frozenset(elem)), and s.discard(frozenset(elem)), respectively. This feature is documented in Built-in Types:

Note, the elem argument to the contains(), remove(), and discard() methods may be a set. To support searching for an equivalent frozenset, a temporary one is created from elem.

The implementation can be improved by calculating the hash value directly from the set object (as if it is a frozenset), instead of copying it into a new frozenset before calculating the hash value. The hash function for set is internal use only, and set is still unhashable.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. self-assigned this
    on Jul 15, 2024
  2. HarryLHW commented on Jul 15, 2024

    @HarryLHW
    ContributorAuthor

    Benchmark (on an M2 Macbook Air):

    Script:

    import timeit
    
    for n in (1, 10, 100, 1000, 10000):
        setup = f'''
    a = set(range({n}))
    b = {{frozenset(a)}}
    '''
        print(timeit.timeit("a in b", setup), f'N = {n}')
    

    main branch result:

    0.3688333749996673 N = 1
    0.48004929200033075 N = 10
    1.781020708000142 N = 100
    11.256919416999153 N = 1000
    152.03752795800028 N = 10000
    

    my branch result:

    0.30569541699878755 N = 1
    0.3746081659992342 N = 10
    1.2535620420021587 N = 100
    7.2957435000025725 N = 1000
    86.63279329200304 N = 10000
    

    There will be no performance regression on the normal case.

  3. added a commit that references this issue on Jul 22, 2024
  4. rhettinger commented on Jul 23, 2024

    @rhettinger
    Contributor

    Sorry, I haven't been feeling well. Please get another core dev to review this.

  5. removed their assignment
    on Jul 23, 2024
  6. erlend-aasland commented on Jul 23, 2024

    @erlend-aasland
    Contributor

    Thanks for the PR, @HarryLHW! I believe this can be closed, as the PR was recently merged. Good job!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagetype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions