From d25015b03427aba8aad046e577b461f140897ce7 Mon Sep 17 00:00:00 2001 From: rajat315315 Date: Mon, 6 Jul 2026 19:42:20 +0530 Subject: [PATCH 1/2] perf: optimize set lookups by inlining set_do_lookup and adding a fast-path for identical Unicode keys --- Objects/setobject.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Objects/setobject.c b/Objects/setobject.c index 8fdd1eb26118c0a..fb2e447a4dc7b44 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -182,6 +182,11 @@ set_compare_frozenset(PySetObject *so, setentry *table, setentry *ep, } Py_ssize_t ep_hash = ep->hash; if (ep_hash == hash) { + if (PyUnicode_CheckExact(startkey) + && PyUnicode_CheckExact(key) + && unicode_eq(startkey, key)) { + return SET_LOOKKEY_FOUND; + } int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); if (cmp < 0) { return SET_LOOKKEY_ERROR; @@ -217,7 +222,7 @@ set_zero_table(setentry *table, size_t size) /* This must be >= 1 */ #define PERTURB_SHIFT 5 -static int +static inline Py_ALWAYS_INLINE int set_do_lookup(PySetObject *so, setentry *table, size_t mask, PyObject *key, Py_hash_t hash, setentry **epp, compare_func compare_entry) { From 81f2fa714cb0a348a104eae433d0b8e5f24cfca6 Mon Sep 17 00:00:00 2001 From: rajat315315 Date: Mon, 6 Jul 2026 19:54:11 +0530 Subject: [PATCH 2/2] perf: optimize set and frozenset membership lookup by inlining lookups and adding a fast path --- .../2026-07-06-14-25-00.gh-issue-153199.abc123.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-07-06-14-25-00.gh-issue-153199.abc123.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-06-14-25-00.gh-issue-153199.abc123.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-06-14-25-00.gh-issue-153199.abc123.rst new file mode 100644 index 000000000000000..cde48ce212720a3 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-06-14-25-00.gh-issue-153199.abc123.rst @@ -0,0 +1 @@ +Optimize set and frozenset membership lookup by inlining ``set_do_lookup`` and adding a ``PyUnicode_CheckExact`` fast path to ``set_compare_frozenset``.