diff -r 178f9042af81 Objects/setobject.c --- a/Objects/setobject.c Sun Sep 23 11:11:07 2012 +0200 +++ b/Objects/setobject.c Mon Sep 24 22:02:13 2012 +0200 @@ -834,7 +834,7 @@ /* copy the itertor state */ tmp = *si; Py_XINCREF(tmp.si_set); - + /* iterate the temporary into a list */ for(;;) { PyObject *element = setiter_iternext(&tmp); @@ -1529,10 +1529,24 @@ if (PyAnySet_Check(other)) { setentry *entry; Py_ssize_t pos = 0; + int intersected = 0; + + // if small -= big , optimize. #issue8425 + if (PySet_GET_SIZE(other) > 8*PySet_GET_SIZE(so)) { + other = set_intersection(so, other); + if (other == NULL) + return -1; + else + intersected = 1; + } while (set_next((PySetObject *)other, &pos, &entry)) if (set_discard_entry(so, entry) == -1) return -1; + + if (intersected) + Py_DECREF(other); + } else { PyObject *key, *it; it = PyObject_GetIter(other);