Dennis Sweeney <sweeney.dennis...@gmail.com> added the comment:

If the argument clinic is too disruptive, would it be okay to inline the 
equivalent code like this?

diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index 90bafb0ea8..d75388abc8 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -880,9 +880,19 @@ deque_rotate(dequeobject *deque, PyObject *const *args, 
Py_ssize_t nargs)
 {
     Py_ssize_t n=1;

-    if (!_PyArg_ParseStack(args, nargs, "|n:rotate", &n)) {
+    if (!_PyArg_CheckPositional("deque.rotate", nargs, 0, 1)) {
         return NULL;
     }
+    if (nargs) {
+        PyObject *index = _PyNumber_Index(args[0]);
+        if (index == NULL) {
+            return NULL;
+        }
+        n = PyLong_AsSsize_t(index);
+        if (n == -1 && PyErr_Occurred()) {
+            return NULL;
+        }
+    }

     if (!_deque_rotate(deque, n))
         Py_RETURN_NONE;

Benchmarks for this change:

    .\python.bat -m pyperf timeit -s "from collections import deque; d = 
deque(range(100))" "d.rotate()"
    Before: Mean +- std dev: 51.2 ns +- 0.9 ns
    After:  Mean +- std dev: 39.3 ns +- 0.3 ns

    .\python.bat -m pyperf timeit -s "from collections import deque; d = 
deque(range(100))" "d.rotate(-1)"
    Before: Mean +- std dev: 64.5 ns +- 0.3 ns
    After:  Mean +- std dev: 46.2 ns +- 0.3 ns

    .\python.bat -m pyperf timeit -s "from collections import deque; d = 
deque(range(100))" "d.rotate(1)"
    Before: Mean +- std dev: 64.4 ns +- 0.3 ns
    After:  Mean +- std dev: 45.7 ns +- 0.2 ns

Similar changes could apply to deque.insert() and deque.index().

----------
nosy: +Dennis Sweeney

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue41361>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to