Add fast paths to deque_init() for the common cases

This commit is contained in:
Raymond Hettinger 2015-09-30 23:15:02 -07:00
parent a53a818c3c
commit 0d30940dd2
1 changed files with 10 additions and 3 deletions

View File

@ -1456,8 +1456,14 @@ deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
Py_ssize_t maxlen = -1; Py_ssize_t maxlen = -1;
char *kwlist[] = {"iterable", "maxlen", 0}; char *kwlist[] = {"iterable", "maxlen", 0};
if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj)) if (kwdargs == NULL) {
return -1; if (!PyArg_UnpackTuple(args, "deque()", 0, 2, &iterable, &maxlenobj))
return -1;
} else {
if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist,
&iterable, &maxlenobj))
return -1;
}
if (maxlenobj != NULL && maxlenobj != Py_None) { if (maxlenobj != NULL && maxlenobj != Py_None) {
maxlen = PyLong_AsSsize_t(maxlenobj); maxlen = PyLong_AsSsize_t(maxlenobj);
if (maxlen == -1 && PyErr_Occurred()) if (maxlen == -1 && PyErr_Occurred())
@ -1468,7 +1474,8 @@ deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
} }
} }
deque->maxlen = maxlen; deque->maxlen = maxlen;
deque_clear(deque); if (Py_SIZE(deque) > 0)
deque_clear(deque);
if (iterable != NULL) { if (iterable != NULL) {
PyObject *rv = deque_extend(deque, iterable); PyObject *rv = deque_extend(deque, iterable);
if (rv == NULL) if (rv == NULL)