Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(12137)

Side by Side Diff: Modules/_collectionsmodule.c

Issue 29452: Use FASTCALL for collections.deque methods: index, insert, rotate
Patch Set: Created 2 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #include "Python.h" 1 #include "Python.h"
2 #include "structmember.h" 2 #include "structmember.h"
3 3
4 #ifdef STDC_HEADERS 4 #ifdef STDC_HEADERS
5 #include <stddef.h> 5 #include <stddef.h>
6 #else 6 #else
7 #include <sys/types.h> /* For size_t */ 7 #include <sys/types.h> /* For size_t */
8 #endif 8 #endif
9 9
10 /* collections module implementation of a deque() datatype 10 /* collections module implementation of a deque() datatype
(...skipping 890 matching lines...) Expand 10 before | Expand all | Expand 10 after
901 freeblock(b); 901 freeblock(b);
902 deque->leftblock = leftblock; 902 deque->leftblock = leftblock;
903 deque->rightblock = rightblock; 903 deque->rightblock = rightblock;
904 deque->leftindex = leftindex; 904 deque->leftindex = leftindex;
905 deque->rightindex = rightindex; 905 deque->rightindex = rightindex;
906 906
907 return rv; 907 return rv;
908 } 908 }
909 909
910 static PyObject * 910 static PyObject *
911 deque_rotate(dequeobject *deque, PyObject *args) 911 deque_rotate(dequeobject *deque, PyObject **args, Py_ssize_t nargs,
912 PyObject *kwnames)
912 { 913 {
913 Py_ssize_t n=1; 914 Py_ssize_t n=1;
914 915
915 if (!PyArg_ParseTuple(args, "|n:rotate", &n)) 916 if (!_PyArg_NoStackKeywords("rotate", kwnames)) {
916 return NULL; 917 return NULL;
918 }
919 if (!_PyArg_ParseStack(args, nargs, "|n:rotate", &n)) {
920 return NULL;
921 }
922
917 if (!_deque_rotate(deque, n)) 923 if (!_deque_rotate(deque, n))
918 Py_RETURN_NONE; 924 Py_RETURN_NONE;
919 return NULL; 925 return NULL;
920 } 926 }
921 927
922 PyDoc_STRVAR(rotate_doc, 928 PyDoc_STRVAR(rotate_doc,
923 "Rotate the deque n steps to the right (default n=1). If n is negative, rotates left."); 929 "Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.");
924 930
925 static PyObject * 931 static PyObject *
926 deque_reverse(dequeobject *deque, PyObject *unused) 932 deque_reverse(dequeobject *deque, PyObject *unused)
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
1035 return 0; 1041 return 0;
1036 } 1042 }
1037 1043
1038 static Py_ssize_t 1044 static Py_ssize_t
1039 deque_len(dequeobject *deque) 1045 deque_len(dequeobject *deque)
1040 { 1046 {
1041 return Py_SIZE(deque); 1047 return Py_SIZE(deque);
1042 } 1048 }
1043 1049
1044 static PyObject * 1050 static PyObject *
1045 deque_index(dequeobject *deque, PyObject *args) 1051 deque_index(dequeobject *deque, PyObject **args, Py_ssize_t nargs,
1052 PyObject *kwnames)
1046 { 1053 {
1047 Py_ssize_t i, n, start=0, stop=Py_SIZE(deque); 1054 Py_ssize_t i, n, start=0, stop=Py_SIZE(deque);
1048 PyObject *v, *item; 1055 PyObject *v, *item;
1049 block *b = deque->leftblock; 1056 block *b = deque->leftblock;
1050 Py_ssize_t index = deque->leftindex; 1057 Py_ssize_t index = deque->leftindex;
1051 size_t start_state = deque->state; 1058 size_t start_state = deque->state;
1052 int cmp; 1059 int cmp;
1053 1060
1054 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v, 1061 if (!_PyArg_NoStackKeywords("index", kwnames)) {
1055 _PyEval_SliceIndex, &start,
1056 _PyEval_SliceIndex, &stop))
1057 return NULL; 1062 return NULL;
1063 }
1064 if (!_PyArg_ParseStack(args, nargs, "O|O&O&:index", &v,
1065 _PyEval_SliceIndex, &start,
1066 _PyEval_SliceIndex, &stop)) {
1067 return NULL;
1068 }
1069
1058 if (start < 0) { 1070 if (start < 0) {
1059 start += Py_SIZE(deque); 1071 start += Py_SIZE(deque);
1060 if (start < 0) 1072 if (start < 0)
1061 start = 0; 1073 start = 0;
1062 } 1074 }
1063 if (stop < 0) { 1075 if (stop < 0) {
1064 stop += Py_SIZE(deque); 1076 stop += Py_SIZE(deque);
1065 if (stop < 0) 1077 if (stop < 0)
1066 stop = 0; 1078 stop = 0;
1067 } 1079 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1110 1122
1111 /* insert(), remove(), and delitem() are implemented in terms of 1123 /* insert(), remove(), and delitem() are implemented in terms of
1112 rotate() for simplicity and reasonable performance near the end 1124 rotate() for simplicity and reasonable performance near the end
1113 points. If for some reason these methods become popular, it is not 1125 points. If for some reason these methods become popular, it is not
1114 hard to re-implement this using direct data movement (similar to 1126 hard to re-implement this using direct data movement (similar to
1115 the code used in list slice assignments) and achieve a performance 1127 the code used in list slice assignments) and achieve a performance
1116 boost (by moving each pointer only once instead of twice). 1128 boost (by moving each pointer only once instead of twice).
1117 */ 1129 */
1118 1130
1119 static PyObject * 1131 static PyObject *
1120 deque_insert(dequeobject *deque, PyObject *args) 1132 deque_insert(dequeobject *deque, PyObject **args, Py_ssize_t nargs,
1133 PyObject *kwnames)
1121 { 1134 {
1122 Py_ssize_t index; 1135 Py_ssize_t index;
1123 Py_ssize_t n = Py_SIZE(deque); 1136 Py_ssize_t n = Py_SIZE(deque);
1124 PyObject *value; 1137 PyObject *value;
1125 PyObject *rv; 1138 PyObject *rv;
1126 1139
1127 if (!PyArg_ParseTuple(args, "nO:insert", &index, &value)) 1140 if (!_PyArg_NoStackKeywords("insert", kwnames)) {
1128 return NULL; 1141 return NULL;
1142 }
1143 if (!_PyArg_ParseStack(args, nargs, "nO:insert", &index, &value)) {
1144 return NULL;
1145 }
1146
1129 if (deque->maxlen == Py_SIZE(deque)) { 1147 if (deque->maxlen == Py_SIZE(deque)) {
1130 PyErr_SetString(PyExc_IndexError, "deque already at its maximum size"); 1148 PyErr_SetString(PyExc_IndexError, "deque already at its maximum size");
1131 return NULL; 1149 return NULL;
1132 } 1150 }
1133 if (index >= n) 1151 if (index >= n)
1134 return deque_append(deque, value); 1152 return deque_append(deque, value);
1135 if (index <= -n || index == 0) 1153 if (index <= -n || index == 0)
1136 return deque_appendleft(deque, value); 1154 return deque_appendleft(deque, value);
1137 if (_deque_rotate(deque, -index)) 1155 if (_deque_rotate(deque, -index))
1138 return NULL; 1156 return NULL;
(...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after
1588 METH_NOARGS, copy_doc}, 1606 METH_NOARGS, copy_doc},
1589 {"copy", (PyCFunction)deque_copy, 1607 {"copy", (PyCFunction)deque_copy,
1590 METH_NOARGS, copy_doc}, 1608 METH_NOARGS, copy_doc},
1591 {"count", (PyCFunction)deque_count, 1609 {"count", (PyCFunction)deque_count,
1592 METH_O, count_doc}, 1610 METH_O, count_doc},
1593 {"extend", (PyCFunction)deque_extend, 1611 {"extend", (PyCFunction)deque_extend,
1594 METH_O, extend_doc}, 1612 METH_O, extend_doc},
1595 {"extendleft", (PyCFunction)deque_extendleft, 1613 {"extendleft", (PyCFunction)deque_extendleft,
1596 METH_O, extendleft_doc}, 1614 METH_O, extendleft_doc},
1597 {"index", (PyCFunction)deque_index, 1615 {"index", (PyCFunction)deque_index,
1598 METH_VARARGS, index_doc}, 1616 METH_FASTCALL, index_doc},
1599 {"insert", (PyCFunction)deque_insert, 1617 {"insert", (PyCFunction)deque_insert,
1600 METH_VARARGS, insert_doc}, 1618 METH_FASTCALL, insert_doc},
1601 {"pop", (PyCFunction)deque_pop, 1619 {"pop", (PyCFunction)deque_pop,
1602 METH_NOARGS, pop_doc}, 1620 METH_NOARGS, pop_doc},
1603 {"popleft", (PyCFunction)deque_popleft, 1621 {"popleft", (PyCFunction)deque_popleft,
1604 METH_NOARGS, popleft_doc}, 1622 METH_NOARGS, popleft_doc},
1605 {"__reduce__", (PyCFunction)deque_reduce, 1623 {"__reduce__", (PyCFunction)deque_reduce,
1606 METH_NOARGS, reduce_doc}, 1624 METH_NOARGS, reduce_doc},
1607 {"remove", (PyCFunction)deque_remove, 1625 {"remove", (PyCFunction)deque_remove,
1608 METH_O, remove_doc}, 1626 METH_O, remove_doc},
1609 {"__reversed__", (PyCFunction)deque_reviter, 1627 {"__reversed__", (PyCFunction)deque_reviter,
1610 METH_NOARGS, reversed_doc}, 1628 METH_NOARGS, reversed_doc},
1611 {"reverse", (PyCFunction)deque_reverse, 1629 {"reverse", (PyCFunction)deque_reverse,
1612 METH_NOARGS, reverse_doc}, 1630 METH_NOARGS, reverse_doc},
1613 {"rotate", (PyCFunction)deque_rotate, 1631 {"rotate", (PyCFunction)deque_rotate,
1614 METH_VARARGS, rotate_doc}, 1632 METH_FASTCALL, rotate_doc},
1615 {"__sizeof__", (PyCFunction)deque_sizeof, 1633 {"__sizeof__", (PyCFunction)deque_sizeof,
1616 METH_NOARGS, sizeof_doc}, 1634 METH_NOARGS, sizeof_doc},
1617 {NULL, NULL} /* sentinel */ 1635 {NULL, NULL} /* sentinel */
1618 }; 1636 };
1619 1637
1620 PyDoc_STRVAR(deque_doc, 1638 PyDoc_STRVAR(deque_doc,
1621 "deque([iterable[, maxlen]]) --> deque object\n\ 1639 "deque([iterable[, maxlen]]) --> deque object\n\
1622 \n\ 1640 \n\
1623 A list-like sequence optimized for data accesses near its endpoints."); 1641 A list-like sequence optimized for data accesses near its endpoints.");
1624 1642
(...skipping 782 matching lines...) Expand 10 before | Expand all | Expand 10 after
2407 Py_INCREF(&dequeiter_type); 2425 Py_INCREF(&dequeiter_type);
2408 PyModule_AddObject(m, "_deque_iterator", (PyObject *)&dequeiter_type); 2426 PyModule_AddObject(m, "_deque_iterator", (PyObject *)&dequeiter_type);
2409 2427
2410 if (PyType_Ready(&dequereviter_type) < 0) 2428 if (PyType_Ready(&dequereviter_type) < 0)
2411 return NULL; 2429 return NULL;
2412 Py_INCREF(&dequereviter_type); 2430 Py_INCREF(&dequereviter_type);
2413 PyModule_AddObject(m, "_deque_reverse_iterator", (PyObject *)&dequereviter_t ype); 2431 PyModule_AddObject(m, "_deque_reverse_iterator", (PyObject *)&dequereviter_t ype);
2414 2432
2415 return m; 2433 return m;
2416 } 2434 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+