diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -578,31 +578,30 @@ vt = (PyTupleObject *)v; wt = (PyTupleObject *)w; + /* Note that because tuples are immutable, it's safe to + * reuse vlen and wlen across the comparison calls. + */ + vlen = Py_SIZE(vt); wlen = Py_SIZE(wt); - /* Note: the corresponding code for lists has an "early out" test - * here when op is EQ or NE and the lengths differ. That pays there, - * but Tim was unable to find any real code where EQ/NE tuple - * compares don't have the same length, so testing for it here would - * have cost without benefit. - */ - - /* Search for the first index where items are different. - * Note that because tuples are immutable, it's safe to reuse - * vlen and wlen across the comparison calls. - */ - for (i = 0; i < vlen && i < wlen; i++) { - int k = PyObject_RichCompareBool(vt->ob_item[i], - wt->ob_item[i], Py_EQ); - if (k < 0) - return NULL; - if (!k) - break; + i = vlen; + if (vlen == wlen) { + /* Search for the first index where items are different. */ + for (i = 0; i < vlen ; i++) { + int k = PyObject_RichCompareBool(vt->ob_item[i], + wt->ob_item[i], Py_EQ); + if (k < 0) + return NULL; + if (!k) + break; + } } - if (i >= vlen || i >= wlen) { - /* No more items to compare -- compare sizes */ + if (i >= vlen) { + /* If all items are equal or if the lengths differ, + * then compare sizes + */ int cmp; PyObject *res; switch (op) {