[Python-checkins] cpython: Issue #26288: Optimize PyLong_AsDouble.

yury.selivanov python-checkins at python.org
Fri Feb 5 19:40:08 EST 2016


https://hg.python.org/cpython/rev/986184c355e8
changeset:   100167:986184c355e8
user:        Yury Selivanov <yselivanov at sprymix.com>
date:        Fri Feb 05 19:40:01 2016 -0500
summary:
  Issue #26288: Optimize PyLong_AsDouble.

files:
  Misc/NEWS            |  1 +
  Objects/longobject.c |  7 +++++++
  2 files changed, 8 insertions(+), 0 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -165,6 +165,7 @@
 
 - Issue #25660: Fix TAB key behaviour in REPL with readline.
 
+- Issue #26288: Optimize PyLong_AsDouble.
 
 Library
 -------
diff --git a/Objects/longobject.c b/Objects/longobject.c
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -2769,6 +2769,13 @@
         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,

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list