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

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_ParseStack(args, nargs, "|n:rotate", &n))
916 return NULL; 917 return NULL;
918 if (!_PyArg_NoStackKeywords("rotate", kwnames)) {
919 return NULL;
920 }
921
917 if (!_deque_rotate(deque, n)) 922 if (!_deque_rotate(deque, n))
918 Py_RETURN_NONE; 923 Py_RETURN_NONE;
919 return NULL; 924 return NULL;
920 } 925 }
921 926
922 PyDoc_STRVAR(rotate_doc, 927 PyDoc_STRVAR(rotate_doc,
923 "Rotate the deque n steps to the right (default n=1). If n is negative, rotates left."); 928 "Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.");
924 929
925 static PyObject * 930 static PyObject *
926 deque_reverse(dequeobject *deque, PyObject *unused) 931 deque_reverse(dequeobject *deque, PyObject *unused)
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
1035 return 0; 1040 return 0;
1036 } 1041 }
1037 1042
1038 static Py_ssize_t 1043 static Py_ssize_t
1039 deque_len(dequeobject *deque) 1044 deque_len(dequeobject *deque)
1040 { 1045 {
1041 return Py_SIZE(deque); 1046 return Py_SIZE(deque);
1042 } 1047 }
1043 1048
1044 static PyObject * 1049 static PyObject *
1045 deque_index(dequeobject *deque, PyObject *args) 1050 deque_index(dequeobject *deque, PyObject **args, Py_ssize_t nargs,
1051 PyObject *kwnames)
1046 { 1052 {
1047 Py_ssize_t i, n, start=0, stop=Py_SIZE(deque); 1053 Py_ssize_t i, n, start=0, stop=Py_SIZE(deque);
1048 PyObject *v, *item; 1054 PyObject *v, *item;
1049 block *b = deque->leftblock; 1055 block *b = deque->leftblock;
1050 Py_ssize_t index = deque->leftindex; 1056 Py_ssize_t index = deque->leftindex;
1051 size_t start_state = deque->state; 1057 size_t start_state = deque->state;
1052 int cmp; 1058 int cmp;
1053 1059
1054 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v, 1060 if (!_PyArg_ParseStack(args, nargs, "O|O&O&:index", &v,
1055 _PyEval_SliceIndex, &start, 1061 _PyEval_SliceIndex, &start,
1056 _PyEval_SliceIndex, &stop)) 1062 _PyEval_SliceIndex, &stop)) {
1057 return NULL; 1063 return NULL;
1064 }
1065 if (!_PyArg_NoStackKeywords("index", kwnames)) {
1066 return NULL;
1067 }
1068
1058 if (start < 0) { 1069 if (start < 0) {
1059 start += Py_SIZE(deque); 1070 start += Py_SIZE(deque);
1060 if (start < 0) 1071 if (start < 0)
1061 start = 0; 1072 start = 0;
1062 } 1073 }
1063 if (stop < 0) { 1074 if (stop < 0) {
1064 stop += Py_SIZE(deque); 1075 stop += Py_SIZE(deque);
1065 if (stop < 0) 1076 if (stop < 0)
1066 stop = 0; 1077 stop = 0;
1067 } 1078 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1110 1121
1111 /* insert(), remove(), and delitem() are implemented in terms of 1122 /* insert(), remove(), and delitem() are implemented in terms of
1112 rotate() for simplicity and reasonable performance near the end 1123 rotate() for simplicity and reasonable performance near the end
1113 points. If for some reason these methods become popular, it is not 1124 points. If for some reason these methods become popular, it is not
1114 hard to re-implement this using direct data movement (similar to 1125 hard to re-implement this using direct data movement (similar to
1115 the code used in list slice assignments) and achieve a performance 1126 the code used in list slice assignments) and achieve a performance
1116 boost (by moving each pointer only once instead of twice). 1127 boost (by moving each pointer only once instead of twice).
1117 */ 1128 */
1118 1129
1119 static PyObject * 1130 static PyObject *
1120 deque_insert(dequeobject *deque, PyObject *args) 1131 deque_insert(dequeobject *deque, PyObject **args, Py_ssize_t nargs,
1132 PyObject *kwnames)
1121 { 1133 {
1122 Py_ssize_t index; 1134 Py_ssize_t index;
1123 Py_ssize_t n = Py_SIZE(deque); 1135 Py_ssize_t n = Py_SIZE(deque);
1124 PyObject *value; 1136 PyObject *value;
1125 PyObject *rv; 1137 PyObject *rv;
1126 1138
1127 if (!PyArg_ParseTuple(args, "nO:insert", &index, &value)) 1139 if (!_PyArg_ParseStack(args, nargs, "nO:insert", &index, &value))
1128 return NULL; 1140 return NULL;
1141 if (!_PyArg_NoStackKeywords("insert", kwnames)) {
1142 return NULL;
1143 }
1144
1129 if (deque->maxlen == Py_SIZE(deque)) { 1145 if (deque->maxlen == Py_SIZE(deque)) {
1130 PyErr_SetString(PyExc_IndexError, "deque already at its maximum size"); 1146 PyErr_SetString(PyExc_IndexError, "deque already at its maximum size");
1131 return NULL; 1147 return NULL;
1132 } 1148 }
1133 if (index >= n) 1149 if (index >= n)
1134 return deque_append(deque, value); 1150 return deque_append(deque, value);
1135 if (index <= -n || index == 0) 1151 if (index <= -n || index == 0)
1136 return deque_appendleft(deque, value); 1152 return deque_appendleft(deque, value);
1137 if (_deque_rotate(deque, -index)) 1153 if (_deque_rotate(deque, -index))
1138 return NULL; 1154 return NULL;
(...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after
1588 METH_NOARGS, copy_doc}, 1604 METH_NOARGS, copy_doc},
1589 {"copy", (PyCFunction)deque_copy, 1605 {"copy", (PyCFunction)deque_copy,
1590 METH_NOARGS, copy_doc}, 1606 METH_NOARGS, copy_doc},
1591 {"count", (PyCFunction)deque_count, 1607 {"count", (PyCFunction)deque_count,
1592 METH_O, count_doc}, 1608 METH_O, count_doc},
1593 {"extend", (PyCFunction)deque_extend, 1609 {"extend", (PyCFunction)deque_extend,
1594 METH_O, extend_doc}, 1610 METH_O, extend_doc},
1595 {"extendleft", (PyCFunction)deque_extendleft, 1611 {"extendleft", (PyCFunction)deque_extendleft,
1596 METH_O, extendleft_doc}, 1612 METH_O, extendleft_doc},
1597 {"index", (PyCFunction)deque_index, 1613 {"index", (PyCFunction)deque_index,
1598 METH_VARARGS, index_doc}, 1614 METH_FASTCALL, index_doc},
1599 {"insert", (PyCFunction)deque_insert, 1615 {"insert", (PyCFunction)deque_insert,
1600 METH_VARARGS, insert_doc}, 1616 METH_FASTCALL, insert_doc},
1601 {"pop", (PyCFunction)deque_pop, 1617 {"pop", (PyCFunction)deque_pop,
1602 METH_NOARGS, pop_doc}, 1618 METH_NOARGS, pop_doc},
1603 {"popleft", (PyCFunction)deque_popleft, 1619 {"popleft", (PyCFunction)deque_popleft,
1604 METH_NOARGS, popleft_doc}, 1620 METH_NOARGS, popleft_doc},
1605 {"__reduce__", (PyCFunction)deque_reduce, 1621 {"__reduce__", (PyCFunction)deque_reduce,
1606 METH_NOARGS, reduce_doc}, 1622 METH_NOARGS, reduce_doc},
1607 {"remove", (PyCFunction)deque_remove, 1623 {"remove", (PyCFunction)deque_remove,
1608 METH_O, remove_doc}, 1624 METH_O, remove_doc},
1609 {"__reversed__", (PyCFunction)deque_reviter, 1625 {"__reversed__", (PyCFunction)deque_reviter,
1610 METH_NOARGS, reversed_doc}, 1626 METH_NOARGS, reversed_doc},
1611 {"reverse", (PyCFunction)deque_reverse, 1627 {"reverse", (PyCFunction)deque_reverse,
1612 METH_NOARGS, reverse_doc}, 1628 METH_NOARGS, reverse_doc},
1613 {"rotate", (PyCFunction)deque_rotate, 1629 {"rotate", (PyCFunction)deque_rotate,
1614 METH_VARARGS, rotate_doc}, 1630 METH_FASTCALL, rotate_doc},
1615 {"__sizeof__", (PyCFunction)deque_sizeof, 1631 {"__sizeof__", (PyCFunction)deque_sizeof,
1616 METH_NOARGS, sizeof_doc}, 1632 METH_NOARGS, sizeof_doc},
1617 {NULL, NULL} /* sentinel */ 1633 {NULL, NULL} /* sentinel */
1618 }; 1634 };
1619 1635
1620 PyDoc_STRVAR(deque_doc, 1636 PyDoc_STRVAR(deque_doc,
1621 "deque([iterable[, maxlen]]) --> deque object\n\ 1637 "deque([iterable[, maxlen]]) --> deque object\n\
1622 \n\ 1638 \n\
1623 A list-like sequence optimized for data accesses near its endpoints."); 1639 A list-like sequence optimized for data accesses near its endpoints.");
1624 1640
(...skipping 782 matching lines...) Expand 10 before | Expand all | Expand 10 after
2407 Py_INCREF(&dequeiter_type); 2423 Py_INCREF(&dequeiter_type);
2408 PyModule_AddObject(m, "_deque_iterator", (PyObject *)&dequeiter_type); 2424 PyModule_AddObject(m, "_deque_iterator", (PyObject *)&dequeiter_type);
2409 2425
2410 if (PyType_Ready(&dequereviter_type) < 0) 2426 if (PyType_Ready(&dequereviter_type) < 0)
2411 return NULL; 2427 return NULL;
2412 Py_INCREF(&dequereviter_type); 2428 Py_INCREF(&dequereviter_type);
2413 PyModule_AddObject(m, "_deque_reverse_iterator", (PyObject *)&dequereviter_t ype); 2429 PyModule_AddObject(m, "_deque_reverse_iterator", (PyObject *)&dequereviter_t ype);
2414 2430
2415 return m; 2431 return m;
2416 } 2432 }
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+