This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Optimize set.issuperset() for non-set argument
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2022-02-11 15:15 by serhiy.storchaka, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 31280 merged serhiy.storchaka, 2022-02-11 15:17
Messages (3)
msg413075 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2022-02-11 15:15
If the argument of set.issuperset() is not a set, it is first converted to a set. It is equivalent to the following code:

    if not isinstance(other, (set, frozenset)):
        other = set(other)
    # The following is equivalent to:
    # return set.issubset(other, self)
    for x in other:
        if x not in self
            return False
    return True

Two drawbacks of this algorithm:

1. It creates a new set, which takes O(len(other)) time and consumes O(len(set(other))) memory.
2. It needs to iterate other to the end, even if the result is known earlier.

The proposed PR straightforward the code. The C code is now larger, but it no longer need additional memory, performs less operations and can stop earlier.
msg413076 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2022-02-11 15:23
The new code is similar to the code of set.isdisjoint(), so we can share the code if generalize it.
msg416887 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2022-04-06 16:57
New changeset a69a4a917c436579c2c4112081ea86a70f1f05d3 by Serhiy Storchaka in branch 'main':
bpo-46721: Optimize set.issuperset() for non-set arguments (GH-31280)
https://github.com/python/cpython/commit/a69a4a917c436579c2c4112081ea86a70f1f05d3
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 90877
2022-04-06 17:05:20serhiy.storchakasetstatus: open -> closed
dependencies: - Use-after-free by mutating set during set operations
resolution: fixed
stage: patch review -> resolved
2022-04-06 16:57:17serhiy.storchakasetmessages: + msg416887
2022-02-11 18:17:41rhettingersetassignee: rhettinger
2022-02-11 15:40:10serhiy.storchakasetdependencies: + Use-after-free by mutating set during set operations
2022-02-11 15:23:35serhiy.storchakasetmessages: + msg413076
2022-02-11 15:17:17serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request29440
2022-02-11 15:15:23serhiy.storchakacreate