Skip to content

Zend: Update dtoa.c to the latest version - #25873

Open
adapik wants to merge 1 commit into
php:masterfrom
adapik:zend-dtoa-update
Open

adapik wants to merge 1 commit into
php:masterfrom
adapik:zend-dtoa-update

Conversation

@adapik

@adapik adapik commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

This PR updates David M. Gay's dtoa.c, which we vendor in Zend/zend_strtod.c. Our copy corresponds to the upstream version of 2013-12-09. This PR updates it to the version of 2026-09-08, the latest one available.

The public interface of the functions PHP uses and the big integer algorithms are unchanged. The most noticable change is in the 2016-04-29 version, which added much faster conversion paths based on 64-bit integer arithmetic and a table of 96-bit powers of ten.

The versions since then also fix many bugs and incorrect results. A good part of those fixes are in the new code or in options PHP does not use. One of them is visible in PHP: when an exact tie is rounded down, there is no trailing zero anymore (2016-03-25), so sprintf('%.3G', 370500.0) now gives 3.7E+5 instead of 3.70E+5. The same applies to precision, and to var_export(), json_encode() and serialize() when serialize_precision is not -1.

Two of our local changes are not needed anymore:

  • the fix for signed overflow while reading the exponent (b8cba88904a), because upstream fixed it (2023-12-09);
  • the explicit initialization of variables in zend_dtoa() added to silence uninitialized variable warnings (feba2edba2e, e1ab6862281). Upstream now initializes ilim and spec_case itself, and GCC 13 does not warn about j1 in the new code.

The update makes float to string and string to float conversions significantly faster. It also closes the gap with upstream that built up over the years, which should make future updates easier.

Benchmarks:

Float to string

function \ significant digits 1 3 6 9 12 15 17
json_encode() -34% -55% -69% -74% -77% -79% -79%
serialize() -22% -49% -67% -74% -76% -78% -78%
var_export() -15% -35% -57% -67% -70% -73% -74%
(string) $f -3% -45% -43% -43% -43% -50% -49%
sprintf('%g') -8% -27% -23% -25% -26% -25% -28%
sprintf('%.2f') 0% -20% -17% -21% -20% -19% -22%
sprintf('%.10f') +3% -25% -32% -42% -42% -43% -45%
number_format($f, 2) -2% -19% -14% -15% -15% -15% -17%

String to float

function \ significant digits 1 3 6 9 12 15 17
json_decode() 0% -1% -3% -5% -8% -8% -53%
unserialize() -2% -3% -6% -6% -7% -7% -53%
(float) $s 0% -1% -5% -8% -9% -7% -59%

The release zend_strtod.o text grows from 23 KB to 41 KB, mostly because of the new tables.

@devnexen

Copy link
Copy Markdown
Member

I think that s the right route, might be good to write as comment the changelog version related to this somewhere.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants