[Python-checkins] Speed-up math.dist() by 30% (GH-9628)

Raymond Hettinger webhook-mailer at python.org
Sat Sep 29 17:30:42 EDT 2018


https://github.com/python/cpython/commit/df8101517aa1c917fdf8aeb466e480c26d4e878c
commit: df8101517aa1c917fdf8aeb466e480c26d4e878c
branch: master
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-09-29T14:30:38-07:00
summary:

Speed-up math.dist() by 30% (GH-9628)

files:
M Modules/clinic/mathmodule.c.h
M Modules/mathmodule.c

diff --git a/Modules/clinic/mathmodule.c.h b/Modules/clinic/mathmodule.c.h
index c4d278634101..b40a227dbea5 100644
--- a/Modules/clinic/mathmodule.c.h
+++ b/Modules/clinic/mathmodule.c.h
@@ -294,8 +294,9 @@ math_dist(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
     PyObject *p;
     PyObject *q;
 
-    if (!_PyArg_ParseStack(args, nargs, "O!O!:dist",
-        &PyTuple_Type, &p, &PyTuple_Type, &q)) {
+    if (!_PyArg_UnpackStack(args, nargs, "dist",
+        2, 2,
+        &p, &q)) {
         goto exit;
     }
     return_value = math_dist_impl(module, p, q);
@@ -522,4 +523,4 @@ math_isclose(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=d936137c1189b89b input=a9049054013a1b77]*/
+/*[clinic end generated code: output=239c51a5acefbafb input=a9049054013a1b77]*/
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index e872e473f5fe..e956314e27fc 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -2101,8 +2101,8 @@ vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
 /*[clinic input]
 math.dist
 
-    p: object(subclass_of='&PyTuple_Type')
-    q: object(subclass_of='&PyTuple_Type')
+    p: object
+    q: object
     /
 
 Return the Euclidean distance between two points p and q.
@@ -2116,7 +2116,7 @@ Roughly equivalent to:
 
 static PyObject *
 math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
-/*[clinic end generated code: output=56bd9538d06bbcfe input=937122eaa5f19272]*/
+/*[clinic end generated code: output=56bd9538d06bbcfe input=8c83c07c7a524664]*/
 {
     PyObject *item;
     double max = 0.0;
@@ -2126,6 +2126,11 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
     double diffs_on_stack[NUM_STACK_ELEMS];
     double *diffs = diffs_on_stack;
 
+    if (!PyTuple_Check(p) || !PyTuple_Check(q)) {
+        PyErr_SetString(PyExc_TypeError, "dist argument must be a tuple");
+        return NULL;
+    }
+
     m = PyTuple_GET_SIZE(p);
     n = PyTuple_GET_SIZE(q);
     if (m != n) {



More information about the Python-checkins mailing list