]> granicus.if.org Git - python/commitdiff
Issue #26288: Optimize PyLong_AsDouble.
authorYury Selivanov <yselivanov@sprymix.com>
Sat, 6 Feb 2016 00:40:01 +0000 (19:40 -0500)
committerYury Selivanov <yselivanov@sprymix.com>
Sat, 6 Feb 2016 00:40:01 +0000 (19:40 -0500)
Misc/NEWS
Objects/longobject.c

index dcccc4af66e5b1ae3664b81b9b5c955c11b85be9..510748b25697c0529a548919cb15ac74cab6e390 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -165,6 +165,7 @@ Core and Builtins
 
 - Issue #25660: Fix TAB key behaviour in REPL with readline.
 
+- Issue #26288: Optimize PyLong_AsDouble.
 
 Library
 -------
index d05de8bf07a0421abe7fdede6cb380724e32f717..c1edeacf62e0f51c2e3d88f46cf89086ad71a53b 100644 (file)
@@ -2769,6 +2769,13 @@ PyLong_AsDouble(PyObject *v)
         PyErr_SetString(PyExc_TypeError, "an integer is required");
         return -1.0;
     }
+    if (Py_ABS(Py_SIZE(v)) <= 1) {
+        /* Fast path; single digit will always fit decimal.
+           This improves performance of FP/long operations by at
+           least 20%. This is even visible on macro-benchmarks.
+        */
+        return (double)MEDIUM_VALUE((PyLongObject *)v);
+    }
     x = _PyLong_Frexp((PyLongObject *)v, &exponent);
     if ((x == -1.0 && PyErr_Occurred()) || exponent > DBL_MAX_EXP) {
         PyErr_SetString(PyExc_OverflowError,