[Python-checkins] bpo-43464: Optimize set.intersection() for non-set arguments (GH-31316)

serhiy-storchaka webhook-mailer at python.org
Wed Apr 6 12:56:42 EDT 2022


https://github.com/python/cpython/commit/31cd25f4e17cd68487dc76c1b2ec162a646818c2
commit: 31cd25f4e17cd68487dc76c1b2ec162a646818c2
branch: main
author: Serhiy Storchaka <storchaka at gmail.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2022-04-06T19:56:28+03:00
summary:

bpo-43464: Optimize set.intersection() for non-set arguments (GH-31316)

files:
A Misc/NEWS.d/next/Core and Builtins/2022-02-13-21-53-29.bpo-43464.yupHjd.rst
M Objects/setobject.c

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 0000000000000..a67ce7c9688e2
--- /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 022ae8e7f9392..18dc49be93d6d 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);
     }



More information about the Python-checkins mailing list