Hoist constant expression out of an inner loop.

This commit is contained in:
Raymond Hettinger 2015-09-26 01:30:51 -07:00
parent 98de5340d4
commit 7a84552c84
1 changed files with 18 additions and 6 deletions

View File

@ -371,6 +371,7 @@ static PyObject *
deque_extend(dequeobject *deque, PyObject *iterable) deque_extend(dequeobject *deque, PyObject *iterable)
{ {
PyObject *it, *item; PyObject *it, *item;
PyObject *(*iternext)(PyObject *);
int trim = (deque->maxlen != -1); int trim = (deque->maxlen != -1);
/* Handle case where id(deque) == id(iterable) */ /* Handle case where id(deque) == id(iterable) */
@ -399,7 +400,8 @@ deque_extend(dequeobject *deque, PyObject *iterable)
if (deque->maxlen == 0) if (deque->maxlen == 0)
return consume_iterator(it); return consume_iterator(it);
while ((item = PyIter_Next(it)) != NULL) { iternext = *Py_TYPE(it)->tp_iternext;
while ((item = iternext(it)) != NULL) {
deque->state++; deque->state++;
if (deque->rightindex == BLOCKLEN - 1) { if (deque->rightindex == BLOCKLEN - 1) {
block *b = newblock(Py_SIZE(deque)); block *b = newblock(Py_SIZE(deque));
@ -422,8 +424,12 @@ deque_extend(dequeobject *deque, PyObject *iterable)
deque_trim_left(deque); deque_trim_left(deque);
} }
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
Py_DECREF(it); if (PyErr_ExceptionMatches(PyExc_StopIteration))
return NULL; PyErr_Clear();
else {
Py_DECREF(it);
return NULL;
}
} }
Py_DECREF(it); Py_DECREF(it);
Py_RETURN_NONE; Py_RETURN_NONE;
@ -436,6 +442,7 @@ static PyObject *
deque_extendleft(dequeobject *deque, PyObject *iterable) deque_extendleft(dequeobject *deque, PyObject *iterable)
{ {
PyObject *it, *item; PyObject *it, *item;
PyObject *(*iternext)(PyObject *);
int trim = (deque->maxlen != -1); int trim = (deque->maxlen != -1);
/* Handle case where id(deque) == id(iterable) */ /* Handle case where id(deque) == id(iterable) */
@ -464,7 +471,8 @@ deque_extendleft(dequeobject *deque, PyObject *iterable)
if (deque->maxlen == 0) if (deque->maxlen == 0)
return consume_iterator(it); return consume_iterator(it);
while ((item = PyIter_Next(it)) != NULL) { iternext = *Py_TYPE(it)->tp_iternext;
while ((item = iternext(it)) != NULL) {
deque->state++; deque->state++;
if (deque->leftindex == 0) { if (deque->leftindex == 0) {
block *b = newblock(Py_SIZE(deque)); block *b = newblock(Py_SIZE(deque));
@ -487,8 +495,12 @@ deque_extendleft(dequeobject *deque, PyObject *iterable)
deque_trim_right(deque); deque_trim_right(deque);
} }
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
Py_DECREF(it); if (PyErr_ExceptionMatches(PyExc_StopIteration))
return NULL; PyErr_Clear();
else {
Py_DECREF(it);
return NULL;
}
} }
Py_DECREF(it); Py_DECREF(it);
Py_RETURN_NONE; Py_RETURN_NONE;