Commit 186c30b7 authored by Yury Selivanov's avatar Yury Selivanov

Issue #26288: Optimize PyLong_AsDouble.

parent eb588a1d
...@@ -165,6 +165,7 @@ Core and Builtins ...@@ -165,6 +165,7 @@ Core and Builtins
- Issue #25660: Fix TAB key behaviour in REPL with readline. - Issue #25660: Fix TAB key behaviour in REPL with readline.
- Issue #26288: Optimize PyLong_AsDouble.
Library Library
------- -------
......
...@@ -2769,6 +2769,13 @@ PyLong_AsDouble(PyObject *v) ...@@ -2769,6 +2769,13 @@ PyLong_AsDouble(PyObject *v)
PyErr_SetString(PyExc_TypeError, "an integer is required"); PyErr_SetString(PyExc_TypeError, "an integer is required");
return -1.0; 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); x = _PyLong_Frexp((PyLongObject *)v, &exponent);
if ((x == -1.0 && PyErr_Occurred()) || exponent > DBL_MAX_EXP) { if ((x == -1.0 && PyErr_Occurred()) || exponent > DBL_MAX_EXP) {
PyErr_SetString(PyExc_OverflowError, PyErr_SetString(PyExc_OverflowError,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment