Commit 66221af5 authored by Antoine Pitrou's avatar Antoine Pitrou

Optimize `x in set` using PySet_Contains()

parent 757c030e
......@@ -12197,6 +12197,11 @@ class CmpNode(object):
self.special_bool_cmp_utility_code = UtilityCode.load_cached("PyDictContains", "ObjectHandling.c")
self.special_bool_cmp_function = "__Pyx_PyDict_ContainsTF"
return True
elif self.operand2.type is Builtin.set_type:
self.operand2 = self.operand2.as_none_safe_node("'NoneType' object is not iterable")
self.special_bool_cmp_utility_code = UtilityCode.load_cached("PySetContains", "ObjectHandling.c")
self.special_bool_cmp_function = "__Pyx_PySet_ContainsTF"
return True
elif self.operand2.type is Builtin.unicode_type:
self.operand2 = self.operand2.as_none_safe_node("'NoneType' object is not iterable")
self.special_bool_cmp_utility_code = UtilityCode.load_cached("PyUnicodeContains", "StringTools.c")
......
......@@ -1035,6 +1035,32 @@ static CYTHON_INLINE int __Pyx_PyDict_ContainsTF(PyObject* item, PyObject* dict,
return unlikely(result < 0) ? result : (result == (eq == Py_EQ));
}
/////////////// PySetContains.proto ///////////////
static int __Pyx_PySet_ContainsUnhashable(PyObject *set, PyObject *key) {
int result = -1;
if (PySet_Check(key) && PyErr_ExceptionMatches(PyExc_TypeError)) {
/* Convert key to frozenset */
PyObject *tmpkey;
PyErr_Clear();
tmpkey = PyFrozenSet_New(key);
if (tmpkey != NULL) {
result = PySet_Contains(set, tmpkey);
Py_DECREF(tmpkey);
}
}
return result;
}
static CYTHON_INLINE int __Pyx_PySet_ContainsTF(PyObject* key, PyObject* set, int eq) {
int result = PySet_Contains(set, key);
if (unlikely(result < 0)) {
result = __Pyx_PySet_ContainsUnhashable(set, key);
}
return unlikely(result < 0) ? result : (result == (eq == Py_EQ));
}
/////////////// PySequenceContains.proto ///////////////
static CYTHON_INLINE int __Pyx_PySequence_ContainsTF(PyObject* item, PyObject* seq, int eq) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment