From ea9bd243ecf02760ac7a5e9a25bd2d067b71ee84 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 23 Jul 2015 09:16:47 +0200 Subject: SDS: use type 8 if we are likely to append to the string. When empty strings are created, or when sdsMakeRoomFor() is called, we are likely into an appending pattern. Use at least type 8 SDS strings since TYPE 5 does not remember the free allocation size and requires to call sdsMakeRoomFor() at every new piece appended. --- src/sds.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/sds.c b/src/sds.c index 8283ec438..c9a6583ee 100644 --- a/src/sds.c +++ b/src/sds.c @@ -80,6 +80,9 @@ sds sdsnewlen(const void *init, size_t initlen) { void *sh; sds s; char type = sdsReqType(initlen); + /* Empty strings are usually created in order to append. Use type 8 + * since type 5 is not good at this. */ + if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8; int hdrlen = sdsHdrSize(type); unsigned char *fp; /* flags pointer. */ @@ -193,7 +196,9 @@ sds sdsMakeRoomFor(sds s, size_t addlen) { char type, oldtype = s[-1] & SDS_TYPE_MASK; int hdrlen; + /* Return ASAP if there is enough space left. */ if (avail >= addlen) return s; + len = sdslen(s); sh = (char*)s-sdsHdrSize(oldtype); newlen = (len+addlen); @@ -203,6 +208,12 @@ sds sdsMakeRoomFor(sds s, size_t addlen) { newlen += SDS_MAX_PREALLOC; type = sdsReqType(newlen); + + /* Don't use type 5: the user is appending to the string and type 5 is + * not able to remember empty space, so sdsMakeRoomFor() must be called + * at every appending operation. */ + if (type == SDS_TYPE_5) type = SDS_TYPE_8; + hdrlen = sdsHdrSize(type); if (oldtype==type) { newsh = zrealloc(sh, hdrlen+newlen+1); -- cgit v1.2.1