Unverified Commit 90223759 authored by Ariel Shtul's avatar Ariel Shtul Committed by GitHub
Browse files

[PERF] use snprintf once in addReplyDouble (#11093)

The previous implementation calls `snprintf` twice, the second time used to
'memcpy' the output of the first, which could be a very large string.
The new implementation reserves space for the protocol header ahead
of the formatted double, and then prepends the string length ahead of it.

Measured improvement of simple ZADD of some 25%.
parent 407b5c91
...@@ -840,13 +840,29 @@ void addReplyDouble(client *c, double d) { ...@@ -840,13 +840,29 @@ void addReplyDouble(client *c, double d) {
d > 0 ? 6 : 7); d > 0 ? 6 : 7);
} }
} else { } else {
char dbuf[MAX_LONG_DOUBLE_CHARS+3], char dbuf[MAX_LONG_DOUBLE_CHARS+32];
sbuf[MAX_LONG_DOUBLE_CHARS+32]; int dlen = 0;
int dlen, slen;
if (c->resp == 2) { if (c->resp == 2) {
dlen = snprintf(dbuf,sizeof(dbuf),"%.17g",d); /* In order to prepend the string length before the formatted number,
slen = snprintf(sbuf,sizeof(sbuf),"$%d\r\n%s\r\n",dlen,dbuf); * but still avoid an extra memcpy of the whole number, we reserve space
addReplyProto(c,sbuf,slen); * for maximum header `$0000\r\n`, print double, add the resp header in
* front of it, and then send the buffer with the right `start` offset. */
int dlen = snprintf(dbuf+7,sizeof(dbuf) - 7,"%.17g",d);
int digits = digits10(dlen);
int start = 4 - digits;
dbuf[start] = '$';
/* Convert `dlen` to string, putting it's digits after '$' and before the
* formatted double string. */
for(int i = digits, val = dlen; val && i > 0 ; --i, val /= 10) {
dbuf[start + i] = "0123456789"[val % 10];
}
dbuf[5] = '\r';
dbuf[6] = '\n';
dbuf[dlen+7] = '\r';
dbuf[dlen+8] = '\n';
addReplyProto(c,dbuf+start,dlen+9-start);
} else { } else {
dlen = snprintf(dbuf,sizeof(dbuf),",%.17g\r\n",d); dlen = snprintf(dbuf,sizeof(dbuf),",%.17g\r\n",d);
addReplyProto(c,dbuf,dlen); addReplyProto(c,dbuf,dlen);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment