[Python-checkins] bpo-34925: Optimize common case for bisect() argument parsing (#9753)

Raymond Hettinger webhook-mailer at python.org
Mon Oct 8 11:02:47 EDT 2018


https://github.com/python/cpython/commit/de2e448414530689f2e60e84fd78bdfebb772898
commit: de2e448414530689f2e60e84fd78bdfebb772898
branch: master
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-10-08T08:02:41-07:00
summary:

bpo-34925: Optimize common case for bisect() argument parsing (#9753)

files:
A Misc/NEWS.d/next/Library/2018-10-07-20-37-02.bpo-34925.KlkZ-Y.rst
M Modules/_bisectmodule.c

diff --git a/Misc/NEWS.d/next/Library/2018-10-07-20-37-02.bpo-34925.KlkZ-Y.rst b/Misc/NEWS.d/next/Library/2018-10-07-20-37-02.bpo-34925.KlkZ-Y.rst
new file mode 100644
index 000000000000..b7853684a959
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-10-07-20-37-02.bpo-34925.KlkZ-Y.rst
@@ -0,0 +1 @@
+25% speedup in argument parsing for the functions in the bisect module.
diff --git a/Modules/_bisectmodule.c b/Modules/_bisectmodule.c
index 831e10aa6078..7dd73f94750b 100644
--- a/Modules/_bisectmodule.c
+++ b/Modules/_bisectmodule.c
@@ -8,7 +8,7 @@ Converted to C by Dmitry Vasiliev (dima at hlabs.spb.ru).
 
 _Py_IDENTIFIER(insert);
 
-static Py_ssize_t
+static inline Py_ssize_t
 internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi)
 {
     PyObject *litem;
@@ -53,9 +53,15 @@ bisect_right(PyObject *self, PyObject *args, PyObject *kw)
     Py_ssize_t index;
     static char *keywords[] = {"a", "x", "lo", "hi", NULL};
 
-    if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_right",
-        keywords, &list, &item, &lo, &hi))
-        return NULL;
+    if (kw == NULL && PyTuple_GET_SIZE(args) == 2) {
+        list = PyTuple_GET_ITEM(args, 0);
+        item = PyTuple_GET_ITEM(args, 1);
+    }
+    else {
+        if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_right",
+                                         keywords, &list, &item, &lo, &hi))
+            return NULL;
+    }
     index = internal_bisect_right(list, item, lo, hi);
     if (index < 0)
         return NULL;
@@ -83,16 +89,23 @@ insort_right(PyObject *self, PyObject *args, PyObject *kw)
     Py_ssize_t index;
     static char *keywords[] = {"a", "x", "lo", "hi", NULL};
 
-    if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_right",
-        keywords, &list, &item, &lo, &hi))
-        return NULL;
+    if (kw == NULL && PyTuple_GET_SIZE(args) == 2) {
+        list = PyTuple_GET_ITEM(args, 0);
+        item = PyTuple_GET_ITEM(args, 1);
+    }
+    else {
+        if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_right",
+                                         keywords, &list, &item, &lo, &hi))
+            return NULL;
+    }
     index = internal_bisect_right(list, item, lo, hi);
     if (index < 0)
         return NULL;
     if (PyList_CheckExact(list)) {
         if (PyList_Insert(list, index, item) < 0)
             return NULL;
-    } else {
+    }
+    else {
         result = _PyObject_CallMethodId(list, &PyId_insert, "nO", index, item);
         if (result == NULL)
             return NULL;
@@ -112,7 +125,7 @@ If x is already in a, insert it to the right of the rightmost x.\n\
 Optional args lo (default 0) and hi (default len(a)) bound the\n\
 slice of a to be searched.\n");
 
-static Py_ssize_t
+static inline Py_ssize_t
 internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi)
 {
     PyObject *litem;
@@ -157,9 +170,15 @@ bisect_left(PyObject *self, PyObject *args, PyObject *kw)
     Py_ssize_t index;
     static char *keywords[] = {"a", "x", "lo", "hi", NULL};
 
-    if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_left",
-        keywords, &list, &item, &lo, &hi))
-        return NULL;
+    if (kw == NULL && PyTuple_GET_SIZE(args) == 2) {
+        list = PyTuple_GET_ITEM(args, 0);
+        item = PyTuple_GET_ITEM(args, 1);
+    }
+    else {
+        if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_left",
+                                         keywords, &list, &item, &lo, &hi))
+            return NULL;
+    }
     index = internal_bisect_left(list, item, lo, hi);
     if (index < 0)
         return NULL;
@@ -187,9 +206,14 @@ insort_left(PyObject *self, PyObject *args, PyObject *kw)
     Py_ssize_t index;
     static char *keywords[] = {"a", "x", "lo", "hi", NULL};
 
-    if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_left",
-        keywords, &list, &item, &lo, &hi))
-        return NULL;
+    if (kw == NULL && PyTuple_GET_SIZE(args) == 2) {
+        list = PyTuple_GET_ITEM(args, 0);
+        item = PyTuple_GET_ITEM(args, 1);
+    } else {
+        if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_left",
+                                         keywords, &list, &item, &lo, &hi))
+            return NULL;
+    }
     index = internal_bisect_left(list, item, lo, hi);
     if (index < 0)
         return NULL;



More information about the Python-checkins mailing list