Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mpx/lua-cjson.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2012-01-10 17:37:46 +0400
committerMark Pulford <mark@kyne.com.au>2012-03-04 12:24:34 +0400
commit3eee7e3db0ef209427f26be3099a0033deb86cb7 (patch)
treedec80bc861ba6465931ef476c6819db806d9b5df /fpconv.c
parent418ee3fe24150c59c9afa6746aab7f09edcb894c (diff)
Use static strtod() buffer where possible
Use static strtod() buffer where possible to improve performance 5-10% under locales with a comma decimal point.
Diffstat (limited to 'fpconv.c')
-rw-r--r--fpconv.c18
1 files 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;
}