Skip to content

gh-156145: Don't rely on errno for float pow and is_integer - #156551

Open
hpkfft wants to merge 3 commits into
python:mainfrom
hpkfft:errno-floatobject
Open

hpkfft wants to merge 3 commits into
python:mainfrom
hpkfft:errno-floatobject

Conversation

@hpkfft

@hpkfft hpkfft commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

This PR only addresses the Objects/floatobject.c portion of the linked issue.

Buy one get one free: I believe this PR allows closing #91290

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>

@skirpichev skirpichev left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is one extra case in this file: double_round(). We use errno here to check overflows in _Py_dg_dtoa(), but can use isinf() as well.

On one hand, dtoa.c code bundled with the CPython and we can trust errno value here. On another hand, as discussed in the issue thread, we can get some performance boost with skipping errno checks (gcc v14.2, default configure flags):

Benchmark ref patch
round(31.4) 1.09 us 978 ns: 1.12x faster
benchmark and patch
import pyperf

x = 31.4

runner = pyperf.Runner()
runner.bench_func(f"round({x})", round, x, -1)
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 17e6a729dcd..8342dcbbd16 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -937,11 +936,10 @@ double_round(double x, int ndigits) {
                   buf, decpt - (int)buflen);
 
     /* and convert the resulting string back to a double */
-    errno = 0;
     _Py_SET_53BIT_PRECISION_START;
     rounded = _Py_dg_strtod(mybuf, NULL);
     _Py_SET_53BIT_PRECISION_END;
-    if (errno == ERANGE && fabs(rounded) >= 1.)
+    if (isinf(rounded))
         PyErr_SetString(PyExc_OverflowError,
                         "rounded value too large to represent");
     else

@skirpichev

Copy link
Copy Markdown
Member

CC @picnixz

@skirpichev

Copy link
Copy Markdown
Member

Another macro removed in #156694.

@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 25, 2026
@hpkfft

hpkfft commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

The motivation for this PR is that errno is mutable, global state, and hence, in my opinion, evil. As @mdickinson noted on d.p.o:

float_pow in particular seems unnecessarily evil in that it’s not just reading that global state, but also writing to it to force overflow exceptions, so even on boxes whose libm doesn’t touch errno (e.g., macOS), correct operation still relies on writes and reads of errno.

There's a performance benefit (using Intel's icx compiler and an optimized build):

Benchmark main patched
is_integer 8.63 ns 7.68 ns: 1.12x faster
float.pow 22.1 ns 21.2 ns: 1.04x faster
Geometric mean (ref) 1.08x faster
pyperf script
import time
import pyperf

def bench_is_integer(loops):
    x = 3.140625
    y = 4.0
    range_it = range(loops)
    t0 = time.perf_counter()
    for _ in range_it:
        result = x.is_integer(); result = y.is_integer();
        result = x.is_integer(); result = y.is_integer();
        result = x.is_integer(); result = y.is_integer();
        result = x.is_integer(); result = y.is_integer();
        result = x.is_integer(); result = y.is_integer();
        result = x.is_integer(); result = y.is_integer();
        result = x.is_integer(); result = y.is_integer();
        result = x.is_integer(); result = y.is_integer();
        result = x.is_integer(); result = y.is_integer();
        result = x.is_integer(); result = y.is_integer();
    return time.perf_counter() - t0

def bench_pow(loops):
    x = 3.140625
    range_it = range(loops)
    t0 = time.perf_counter()
    for _ in range_it:
        result = x**-2; result = x**2; result = x**3; result = x**x;
        result = x**-2; result = x**2; result = x**3; result = x**x;
        result = x**-2; result = x**2; result = x**3; result = x**x;
        result = x**-2; result = x**2; result = x**3; result = x**x;
        result = x**-2; result = x**2; result = x**3; result = x**x;
    return time.perf_counter() - t0

runner = pyperf.Runner(values=10, processes=24)
runner.bench_time_func("is_integer", bench_is_integer, inner_loops=20)
runner.bench_time_func("float.pow",  bench_pow,        inner_loops=20)

I'm personally (at least currently) not willing to modify _Py_dg_dtoa() since I don't feel I know the code well enough. I think the speedup of 1ns that I see above for this PR is expected, and wouldn't expect much more than that. See also Faster float-to-string conversions. (I have some bias in favor of Teju Jagua, because I like the idea of a dragon that feeds on fruit and honey. 😄 )

@skirpichev

Copy link
Copy Markdown
Member

I'm personally (at least currently) not willing to modify _Py_dg_dtoa()

I'm not suggesting this. To detect overflows you can check x and rounded values.

Though, we still have to test for errno == ENOMEM...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants