From 80753f013c653831802b12a1056c1d3d6e05890f Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Sat, 6 Mar 2021 03:37:14 -0500 Subject: [PATCH 1/3] Optimized argument parsing for deque_rotate --- Modules/_collectionsmodule.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index 90bafb0ea86d92..d75388abc80908 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; From 8f70ae0246968a02c69a605ee5b750a8b31408ec Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Sat, 13 Mar 2021 02:19:12 -0500 Subject: [PATCH 2/3] Fix refleak --- Modules/_collectionsmodule.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index d75388abc80908..ca63f710cd8648 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -889,6 +889,7 @@ deque_rotate(dequeobject *deque, PyObject *const *args, Py_ssize_t nargs) return NULL; } n = PyLong_AsSsize_t(index); + Py_DECREF(index); if (n == -1 && PyErr_Occurred()) { return NULL; } From 2caf4804ce741cd1f88888c1f0a8b9c26ce96bb2 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 13 Mar 2021 08:18:03 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2021-03-13-08-18-01.bpo-41361.lXDIlr.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-03-13-08-18-01.bpo-41361.lXDIlr.rst diff --git a/Misc/NEWS.d/next/Library/2021-03-13-08-18-01.bpo-41361.lXDIlr.rst b/Misc/NEWS.d/next/Library/2021-03-13-08-18-01.bpo-41361.lXDIlr.rst new file mode 100644 index 00000000000000..19e08f810bb7c0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-03-13-08-18-01.bpo-41361.lXDIlr.rst @@ -0,0 +1 @@ +:meth:`~collections.deque.rotate` calls are now slightly faster due to faster argument parsing. \ No newline at end of file