Feature or enhancement
Proposal:
Category
Performance
Description
Currently, set membership testing (x in s) via _PySet_Contains suffers from two performance bottlenecks:
- Indirect Function Pointer Calls in Probing Loop:
The table probing function set_do_lookup in Objects/setobject.c receives the comparison function (compare_entry) as a function pointer argument:
static int
set_do_lookup(PySetObject *so, setentry *table, size_t mask, PyObject *key,
Py_hash_t hash, setentry **epp, compare_func compare_entry)
- The second optimization targets string comparison overhead during membership lookups on
frozenset. By adding a fast-path check using PyUnicode_CheckExact and unicode_eq in set_compare_frozenset before falling back to PyObject_RichCompareBool, we bypass the general comparison overhead for the common case of string lookups:
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);
Linked PRs
Feature or enhancement
Proposal:
Category
Performance
Description
Currently, set membership testing (
x in s) via_PySet_Containssuffers from two performance bottlenecks:The table probing function
set_do_lookupinObjects/setobject.creceives the comparison function (compare_entry) as a function pointer argument:frozenset. By adding a fast-path check usingPyUnicode_CheckExactandunicode_eqinset_compare_frozensetbefore falling back toPyObject_RichCompareBool, we bypass the general comparison overhead for the common case of string lookups:Linked PRs