diff options
author | antirez <antirez@gmail.com> | 2015-07-23 09:16:47 +0200 |
---|---|---|
committer | antirez <antirez@gmail.com> | 2015-07-23 16:10:08 +0200 |
commit | ea9bd243ecf02760ac7a5e9a25bd2d067b71ee84 (patch) | |
tree | 98b6bdb70b063557b57f6e74d316ee7b10e82913 /src | |
parent | cf68f4ee6a4c466b893fbb269f6aff14c7c75e6a (diff) | |
download | redis-ea9bd243ecf02760ac7a5e9a25bd2d067b71ee84.tar.gz |
SDS: use type 8 if we are likely to append to the string.sds
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/sds.c | 11 |
1 files changed, 11 insertions, 0 deletions
@@ -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); |