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 00000000000..a67ce7c9688 --- /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 022ae8e7f93..18dc49be93d 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); }