From f49f0a6f729720cc1462d30d036c8ba2baef361b Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 2 Oct 2019 11:27:21 +0200 Subject: [PATCH] SDS: make sdscatfmt() faster by pre-allocating a bit. --- src/sds.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/sds.c b/src/sds.c index cd60946bd..98bd2e77f 100644 --- a/src/sds.c +++ b/src/sds.c @@ -603,6 +603,10 @@ sds sdscatfmt(sds s, char const *fmt, ...) { long i; va_list ap; + /* To avoid continuous reallocations, let's start with a buffer that + * can hold at least two times the format string itself. It's not the + * best heuristic but seems to work in practice. */ + s = sdsMakeRoomFor(s, initlen + strlen(fmt)*2); va_start(ap,fmt); f = fmt; /* Next format specifier byte to process. */ i = initlen; /* Position of the next byte to write to dest str. */