Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0d30940

Browse files
committed
Add fast paths to deque_init() for the common cases
1 parent a53a818 commit 0d30940

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

Modules/_collectionsmodule.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,8 +1456,14 @@ deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
14561456
Py_ssize_t maxlen = -1;
14571457
char *kwlist[] = {"iterable", "maxlen", 0};
14581458

1459-
if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj))
1460-
return -1;
1459+
if (kwdargs == NULL) {
1460+
if (!PyArg_UnpackTuple(args, "deque()", 0, 2, &iterable, &maxlenobj))
1461+
return -1;
1462+
} else {
1463+
if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist,
1464+
&iterable, &maxlenobj))
1465+
return -1;
1466+
}
14611467
if (maxlenobj != NULL && maxlenobj != Py_None) {
14621468
maxlen = PyLong_AsSsize_t(maxlenobj);
14631469
if (maxlen == -1 && PyErr_Occurred())
@@ -1468,7 +1474,8 @@ deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
14681474
}
14691475
}
14701476
deque->maxlen = maxlen;
1471-
deque_clear(deque);
1477+
if (Py_SIZE(deque) > 0)
1478+
deque_clear(deque);
14721479
if (iterable != NULL) {
14731480
PyObject *rv = deque_extend(deque, iterable);
14741481
if (rv == NULL)

0 commit comments

Comments
 (0)