summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2022-04-06 16:56:28 (GMT)
committerGitHub <noreply@github.com>2022-04-06 16:56:28 (GMT)
commit31cd25f4e17cd68487dc76c1b2ec162a646818c2 (patch)
tree12efeb87e5b23ff68dd39b5a48845a99df7888ef
parent50872dbadcba1f52867b6f76050cd7b5d0aa1e18 (diff)
downloadcpython-31cd25f4e17cd68487dc76c1b2ec162a646818c2.zip
cpython-31cd25f4e17cd68487dc76c1b2ec162a646818c2.tar.gz
cpython-31cd25f4e17cd68487dc76c1b2ec162a646818c2.tar.bz2
bpo-43464: Optimize set.intersection() for non-set arguments (GH-31316)
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2022-02-13-21-53-29.bpo-43464.yupHjd.rst1
-rw-r--r--Objects/setobject.c4
2 files changed, 5 insertions, 0 deletions
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-02-13-21-53-29.bpo-43464.yupHjd.rst b/Misc/NEWS.d/next/Core and Builtins/2022-02-13-21-53-29.bpo-43464.yupHjd.rst
new file mode 100644
index 0000000..a67ce7c
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-02-13-21-53-29.bpo-43464.yupHjd.rst
@@ -0,0 +1 @@
+Optimize :meth:`set.intersection` for non-set arguments.
diff --git a/Objects/setobject.c b/Objects/setobject.c
index 022ae8e..18dc49b 100644
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -1240,6 +1240,10 @@ set_intersection(PySetObject *so, PyObject *other)
if (rv) {
if (set_add_entry(result, key, hash))
goto error;
+ if (PySet_GET_SIZE(result) >= PySet_GET_SIZE(so)) {
+ Py_DECREF(key);
+ break;
+ }
}
Py_DECREF(key);
}