diff options
author | Raymond Hettinger <python@rcn.com> | 2015-10-11 16:43:50 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2015-10-11 16:43:50 (GMT) |
commit | 6b1e113f9f142a0e531d0ae3d45cc31341fc9949 (patch) | |
tree | 14eabbf91acc06a706bf589168d2daf5ec7c72e6 /Modules/_collectionsmodule.c | |
parent | 20d15b5100bc12b21c561ee1ea70d8436f944d98 (diff) | |
download | cpython-6b1e113f9f142a0e531d0ae3d45cc31341fc9949.zip cpython-6b1e113f9f142a0e531d0ae3d45cc31341fc9949.tar.gz cpython-6b1e113f9f142a0e531d0ae3d45cc31341fc9949.tar.bz2 |
Hoist the deque->maxlen lookup out of the inner-loop.
Diffstat (limited to 'Modules/_collectionsmodule.c')
-rw-r--r-- | Modules/_collectionsmodule.c | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index 52f40b2..8cd22d7 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -385,7 +385,7 @@ deque_extend(dequeobject *deque, PyObject *iterable) { PyObject *it, *item; PyObject *(*iternext)(PyObject *); - int trim = (deque->maxlen >= 0); + Py_ssize_t maxlen = deque->maxlen; /* Handle case where id(deque) == id(iterable) */ if ((PyObject *)deque == iterable) { @@ -433,8 +433,10 @@ deque_extend(dequeobject *deque, PyObject *iterable) Py_SIZE(deque)++; deque->rightindex++; deque->rightblock->data[deque->rightindex] = item; - if (trim) - deque_trim_left(deque); + if (maxlen >= 0 && Py_SIZE(deque) > maxlen) { + PyObject *rv = deque_popleft(deque, NULL); + Py_DECREF(rv); + } } return finalize_iterator(it); } @@ -447,7 +449,7 @@ deque_extendleft(dequeobject *deque, PyObject *iterable) { PyObject *it, *item; PyObject *(*iternext)(PyObject *); - int trim = (deque->maxlen >= 0); + Py_ssize_t maxlen = deque->maxlen; /* Handle case where id(deque) == id(iterable) */ if ((PyObject *)deque == iterable) { @@ -495,8 +497,10 @@ deque_extendleft(dequeobject *deque, PyObject *iterable) Py_SIZE(deque)++; deque->leftindex--; deque->leftblock->data[deque->leftindex] = item; - if (trim) - deque_trim_right(deque); + if (maxlen >= 0 && Py_SIZE(deque) > maxlen) { + PyObject *rv = deque_pop(deque, NULL); + Py_DECREF(rv); + } } return finalize_iterator(it); } |