summaryrefslogtreecommitdiffstats
path: root/Modules/_collectionsmodule.c
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2009-03-10 09:31:48 (GMT)
committerRaymond Hettinger <python@rcn.com>2009-03-10 09:31:48 (GMT)
commitbac769bb5e7656c65e75e27c9d4c914b5d98f12c (patch)
tree6960121bdcd288e8e51690209aaf9aedbd8c43fe /Modules/_collectionsmodule.c
parentced6b1da83b72e15eba8db7ebb0ae66c3975108a (diff)
downloadcpython-bac769bb5e7656c65e75e27c9d4c914b5d98f12c.zip
cpython-bac769bb5e7656c65e75e27c9d4c914b5d98f12c.tar.gz
cpython-bac769bb5e7656c65e75e27c9d4c914b5d98f12c.tar.bz2
Small optimization for corner case where maxlen==0.
Diffstat (limited to 'Modules/_collectionsmodule.c')
-rw-r--r--Modules/_collectionsmodule.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index 0029532..9532f1b 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -276,6 +276,23 @@ deque_appendleft(dequeobject *deque, PyObject *item)
PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque.");
+
+/* Run an iterator to exhaustion. Shortcut for
+ the extend/extendleft methods when maxlen == 0. */
+static PyObject*
+consume_iterator(PyObject *it)
+{
+ PyObject *item;
+
+ while ((item = PyIter_Next(it)) != NULL) {
+ Py_DECREF(item);
+ }
+ Py_DECREF(it);
+ if (PyErr_Occurred())
+ return NULL;
+ Py_RETURN_NONE;
+}
+
static PyObject *
deque_extend(dequeobject *deque, PyObject *iterable)
{
@@ -285,6 +302,9 @@ deque_extend(dequeobject *deque, PyObject *iterable)
if (it == NULL)
return NULL;
+ if (deque->maxlen == 0)
+ return consume_iterator(it);
+
while ((item = PyIter_Next(it)) != NULL) {
deque->state++;
if (deque->rightindex == BLOCKLEN-1) {
@@ -323,6 +343,9 @@ deque_extendleft(dequeobject *deque, PyObject *iterable)
if (it == NULL)
return NULL;
+ if (deque->maxlen == 0)
+ return consume_iterator(it);
+
while ((item = PyIter_Next(it)) != NULL) {
deque->state++;
if (deque->leftindex == 0) {