From 31cd25f4e17cd68487dc76c1b2ec162a646818c2 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 6 Apr 2022 19:56:28 +0300 Subject: bpo-43464: Optimize set.intersection() for non-set arguments (GH-31316) --- .../next/Core and Builtins/2022-02-13-21-53-29.bpo-43464.yupHjd.rst | 1 + Objects/setobject.c | 4 ++++ 2 files changed, 5 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-02-13-21-53-29.bpo-43464.yupHjd.rst 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); } -- cgit v0.12