Table of Contents

Request for Comments: Add grisu3 strtod algorithm to PHP

Introduction

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.

Proposal

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

Common Misconceptions

The grisu3 algorithm is optional. By default we still use David Gays algorithm. There is no BC break in the API.

Caveats

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

http://www.serpentine.com/blog/2011/06/29/here-be-dragons-advances-in-problems-you-didnt-even-know-you-had/

Changelog