From 3eee7e3db0ef209427f26be3099a0033deb86cb7 Mon Sep 17 00:00:00 2001 From: Mark Pulford Date: Wed, 11 Jan 2012 00:07:46 +1030 Subject: Use static strtod() buffer where possible Use static strtod() buffer where possible to improve performance 5-10% under locales with a comma decimal point. --- fpconv.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/fpconv.c b/fpconv.c index 3bcef41..9f8cb87 100644 --- a/fpconv.c +++ b/fpconv.c @@ -81,6 +81,7 @@ static int strtod_buffer_size(const char *s) * character. Guaranteed to be called at the start of any valid number in a string */ double fpconv_strtod(const char *nptr, char **endptr) { + char localbuf[FPCONV_G_FMT_BUFSIZE]; char *buf, *endbuf, *dp; int buflen; double value; @@ -97,10 +98,16 @@ double fpconv_strtod(const char *nptr, char **endptr) } /* Duplicate number into buffer */ - buf = malloc(buflen + 1); - if (!buf) { - fprintf(stderr, "Out of memory"); - abort(); + if (buflen >= FPCONV_G_FMT_BUFSIZE) { + /* Handle unusually large numbers */ + buf = malloc(buflen + 1); + if (!buf) { + fprintf(stderr, "Out of memory"); + abort(); + } + } else { + /* This is the common case.. */ + buf = localbuf; } memcpy(buf, nptr, buflen); buf[buflen] = 0; @@ -112,7 +119,8 @@ double fpconv_strtod(const char *nptr, char **endptr) value = strtod(buf, &endbuf); *endptr = (char *)&nptr[endbuf - buf]; - free(buf); + if (buflen >= FPCONV_G_FMT_BUFSIZE) + free(buf); return value; } -- cgit v1.2.3-55-g6feb