Skip to content

Commit dd407d5

Browse files
committed
Optimize deque index, insert and rotate() methods
Issue python#29452: Use METH_FASTCALL calling convention for index(), insert() and rotate() methods of collections.deque to avoid the creation a temporary tuple to pass position arguments. Speedup on deque methods: * d.rotate(): 1.10x faster * d.rotate(1): 1.24x faster * d.insert(): 1.18x faster * d.index(): 1.24x faster
1 parent 193ee0a commit dd407d5

1 file changed

Lines changed: 29 additions & 11 deletions

File tree

Modules/_collectionsmodule.c

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -908,12 +908,18 @@ _deque_rotate(dequeobject *deque, Py_ssize_t n)
908908
}
909909

910910
static PyObject *
911-
deque_rotate(dequeobject *deque, PyObject *args)
911+
deque_rotate(dequeobject *deque, PyObject **args, Py_ssize_t nargs,
912+
PyObject *kwnames)
912913
{
913914
Py_ssize_t n=1;
914915

915-
if (!PyArg_ParseTuple(args, "|n:rotate", &n))
916+
if (!_PyArg_NoStackKeywords("rotate", kwnames)) {
916917
return NULL;
918+
}
919+
if (!_PyArg_ParseStack(args, nargs, "|n:rotate", &n)) {
920+
return NULL;
921+
}
922+
917923
if (!_deque_rotate(deque, n))
918924
Py_RETURN_NONE;
919925
return NULL;
@@ -1042,7 +1048,8 @@ deque_len(dequeobject *deque)
10421048
}
10431049

10441050
static PyObject *
1045-
deque_index(dequeobject *deque, PyObject *args)
1051+
deque_index(dequeobject *deque, PyObject **args, Py_ssize_t nargs,
1052+
PyObject *kwnames)
10461053
{
10471054
Py_ssize_t i, n, start=0, stop=Py_SIZE(deque);
10481055
PyObject *v, *item;
@@ -1051,10 +1058,15 @@ deque_index(dequeobject *deque, PyObject *args)
10511058
size_t start_state = deque->state;
10521059
int cmp;
10531060

1054-
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
1055-
_PyEval_SliceIndex, &start,
1056-
_PyEval_SliceIndex, &stop))
1061+
if (!_PyArg_NoStackKeywords("index", kwnames)) {
1062+
return NULL;
1063+
}
1064+
if (!_PyArg_ParseStack(args, nargs, "O|O&O&:index", &v,
1065+
_PyEval_SliceIndex, &start,
1066+
_PyEval_SliceIndex, &stop)) {
10571067
return NULL;
1068+
}
1069+
10581070
if (start < 0) {
10591071
start += Py_SIZE(deque);
10601072
if (start < 0)
@@ -1117,15 +1129,21 @@ PyDoc_STRVAR(index_doc,
11171129
*/
11181130

11191131
static PyObject *
1120-
deque_insert(dequeobject *deque, PyObject *args)
1132+
deque_insert(dequeobject *deque, PyObject **args, Py_ssize_t nargs,
1133+
PyObject *kwnames)
11211134
{
11221135
Py_ssize_t index;
11231136
Py_ssize_t n = Py_SIZE(deque);
11241137
PyObject *value;
11251138
PyObject *rv;
11261139

1127-
if (!PyArg_ParseTuple(args, "nO:insert", &index, &value))
1140+
if (!_PyArg_NoStackKeywords("insert", kwnames)) {
1141+
return NULL;
1142+
}
1143+
if (!_PyArg_ParseStack(args, nargs, "nO:insert", &index, &value)) {
11281144
return NULL;
1145+
}
1146+
11291147
if (deque->maxlen == Py_SIZE(deque)) {
11301148
PyErr_SetString(PyExc_IndexError, "deque already at its maximum size");
11311149
return NULL;
@@ -1595,9 +1613,9 @@ static PyMethodDef deque_methods[] = {
15951613
{"extendleft", (PyCFunction)deque_extendleft,
15961614
METH_O, extendleft_doc},
15971615
{"index", (PyCFunction)deque_index,
1598-
METH_VARARGS, index_doc},
1616+
METH_FASTCALL, index_doc},
15991617
{"insert", (PyCFunction)deque_insert,
1600-
METH_VARARGS, insert_doc},
1618+
METH_FASTCALL, insert_doc},
16011619
{"pop", (PyCFunction)deque_pop,
16021620
METH_NOARGS, pop_doc},
16031621
{"popleft", (PyCFunction)deque_popleft,
@@ -1611,7 +1629,7 @@ static PyMethodDef deque_methods[] = {
16111629
{"reverse", (PyCFunction)deque_reverse,
16121630
METH_NOARGS, reverse_doc},
16131631
{"rotate", (PyCFunction)deque_rotate,
1614-
METH_VARARGS, rotate_doc},
1632+
METH_FASTCALL, rotate_doc},
16151633
{"__sizeof__", (PyCFunction)deque_sizeof,
16161634
METH_NOARGS, sizeof_doc},
16171635
{NULL, NULL} /* sentinel */

0 commit comments

Comments
 (0)