Request for Comments: Add grisu3 strtod algorithm to PHP
- Version: 0.9
- Date: 2011-07-15
- Author: David Soria Parra <dsp at php dot net>
- Status: Inactive
- Proposed for: PHP 5.4
Converting strings to doubles is a common problem in applications. The current standard in solving this problem is David Gays strtod algorithm. This algorithm is used in PHP and can be found in zend_strtod. Recently Florian Loitsch found a new algorithm, called grisu3, to this problem. His algorithm is faster than David Gays algorithm. An OpenSource implementation exists here. The grisu3 algorithm fails in about 0.5% cases. The library will fall back to David Gays algorithm in that case.
- add grisu3 library to PHP source
- add grisu3 to the PHP build system
- add --enable-grisu3 option to PHP configure. disabled by default.
Why do we need a new strtod?
Using grisu3 will speed up string to double and double to string conversions. In particular json_encode and json_decode of json data includign doubles will be faster. Simple microbenchmarks show about 20% faster json_decode.
The library includes strtod (string to double) and dtoa (double to string) conversion methods. zend_strtod and zend_dtoa will provide the same signature. The current algorithm will be moved to zend_strtod_dragon4.c and the zend_strtod/dtoa wrapper for the double-conversion library will go to zend_strtdo_grisu3.cc
The grisu3 algorithm is optional. By default we still use David Gays algorithm. There is no BC break in the API.
The double-conversion library is written in C++. To compile PHP with grisu3 you will need a C++ compiler.
Proposal and Patch
There is a initial patchset at http://bitbucket.org/segv/php-src
More about Grisu3