diff options
author | Yury Selivanov <yselivanov@sprymix.com> | 2016-02-06 00:40:01 (GMT) |
---|---|---|
committer | Yury Selivanov <yselivanov@sprymix.com> | 2016-02-06 00:40:01 (GMT) |
commit | 186c30b7ae2c304b863143809867cfef7eb1bf29 (patch) | |
tree | a2de8be2359efd47be4a037eb0540650d2f27ec7 /Objects/longobject.c | |
parent | eb588a1d10e94085c622eb732cfe535e0ebaec8a (diff) | |
download | cpython-186c30b7ae2c304b863143809867cfef7eb1bf29.zip cpython-186c30b7ae2c304b863143809867cfef7eb1bf29.tar.gz cpython-186c30b7ae2c304b863143809867cfef7eb1bf29.tar.bz2 |
Issue #26288: Optimize PyLong_AsDouble.
Diffstat (limited to 'Objects/longobject.c')
-rw-r--r-- | Objects/longobject.c | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c index d05de8b..c1edeac 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -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, |