bpo-46721: Optimize set.issuperset() for non-set arguments (GH-31280)

This commit is contained in:
Serhiy Storchaka 2022-04-06 19:57:13 +03:00 committed by GitHub
parent 31cd25f4e1
commit a69a4a917c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 18 deletions

View File

@ -0,0 +1 @@
Optimize :meth:`set.issuperset` for non-set argument.

View File

@ -1382,14 +1382,7 @@ set_isdisjoint(PySetObject *so, PyObject *other)
return NULL;
while ((key = PyIter_Next(it)) != NULL) {
Py_hash_t hash = PyObject_Hash(key);
if (hash == -1) {
Py_DECREF(key);
Py_DECREF(it);
return NULL;
}
rv = set_contains_entry(so, key, hash);
rv = set_contains_key(so, key);
Py_DECREF(key);
if (rv < 0) {
Py_DECREF(it);
@ -1773,17 +1766,31 @@ PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
static PyObject *
set_issuperset(PySetObject *so, PyObject *other)
{
PyObject *tmp, *result;
if (!PyAnySet_Check(other)) {
tmp = make_new_set(&PySet_Type, other);
if (tmp == NULL)
return NULL;
result = set_issuperset(so, tmp);
Py_DECREF(tmp);
return result;
if (PyAnySet_Check(other)) {
return set_issubset((PySetObject *)other, (PyObject *)so);
}
return set_issubset((PySetObject *)other, (PyObject *)so);
PyObject *key, *it = PyObject_GetIter(other);
if (it == NULL) {
return NULL;
}
while ((key = PyIter_Next(it)) != NULL) {
int rv = set_contains_key(so, key);
Py_DECREF(key);
if (rv < 0) {
Py_DECREF(it);
return NULL;
}
if (!rv) {
Py_DECREF(it);
Py_RETURN_FALSE;
}
}
Py_DECREF(it);
if (PyErr_Occurred()) {
return NULL;
}
Py_RETURN_TRUE;
}
PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");