From f86df924b01db43eb68f5c4b4cac4c44c1507390 Mon Sep 17 00:00:00 2001 From: oranagra Date: Thu, 23 Feb 2017 03:04:08 -0800 Subject: [PATCH] add SDS_NOINIT option to sdsnewlen to avoid unnecessary memsets. this commit also contains small bugfix in rdbLoadLzfStringObject a bug that currently has no implications. --- src/aof.c | 2 +- src/networking.c | 4 ++-- src/object.c | 4 +++- src/rdb.c | 12 ++++++------ src/sds.c | 7 ++++++- src/sds.h | 1 + 6 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/aof.c b/src/aof.c index f73da1e25..ebe3e3a47 100644 --- a/src/aof.c +++ b/src/aof.c @@ -699,7 +699,7 @@ int loadAppendOnlyFile(char *filename) { } if (buf[0] != '$') goto fmterr; len = strtol(buf+1,NULL,10); - argsds = sdsnewlen(NULL,len); + argsds = sdsnewlen(SDS_NOINIT,len); if (len && fread(argsds,len,1,fp) == 0) { sdsfree(argsds); fakeClient->argc = j; /* Free up to j-1. */ diff --git a/src/networking.c b/src/networking.c index 343a910e2..eba0d3a62 100644 --- a/src/networking.c +++ b/src/networking.c @@ -1234,7 +1234,7 @@ int processMultibulkBuffer(client *c) { sdsIncrLen(c->querybuf,-2); /* remove CRLF */ /* Assume that if we saw a fat argument we'll see another one * likely... */ - c->querybuf = sdsnewlen(NULL,c->bulklen+2); + c->querybuf = sdsnewlen(SDS_NOINIT,c->bulklen+2); sdsclear(c->querybuf); pos = 0; } else { @@ -1477,7 +1477,7 @@ sds getAllClientsInfoString(void) { listNode *ln; listIter li; client *client; - sds o = sdsnewlen(NULL,200*listLength(server.clients)); + sds o = sdsnewlen(SDS_NOINIT,200*listLength(server.clients)); sdsclear(o); listRewind(server.clients,&li); while ((ln = listNext(&li)) != NULL) { diff --git a/src/object.c b/src/object.c index 08c9ad956..741466dd3 100644 --- a/src/object.c +++ b/src/object.c @@ -98,7 +98,9 @@ robj *createEmbeddedStringObject(const char *ptr, size_t len) { sh->len = len; sh->alloc = len; sh->flags = SDS_TYPE_8; - if (ptr) { + if (ptr == SDS_NOINIT) + sh->buf[len] = '\0'; + else if (ptr) { memcpy(sh->buf,ptr,len); sh->buf[len] = '\0'; } else { diff --git a/src/rdb.c b/src/rdb.c index 2689b172d..eb37a82f6 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -254,7 +254,7 @@ void *rdbLoadIntegerObject(rio *rdb, int enctype, int flags, size_t *lenptr) { char buf[LONG_STR_SIZE], *p; int len = ll2string(buf,sizeof(buf),val); if (lenptr) *lenptr = len; - p = plain ? zmalloc(len) : sdsnewlen(NULL,len); + p = plain ? zmalloc(len) : sdsnewlen(SDS_NOINIT,len); memcpy(p,buf,len); return p; } else if (encode) { @@ -343,10 +343,10 @@ void *rdbLoadLzfStringObject(rio *rdb, int flags, size_t *lenptr) { /* Allocate our target according to the uncompressed size. */ if (plain) { val = zmalloc(len); - if (lenptr) *lenptr = len; } else { - val = sdsnewlen(NULL,len); + val = sdsnewlen(SDS_NOINIT,len); } + if (lenptr) *lenptr = len; /* Load the compressed representation and uncompress it to target. */ if (rioRead(rdb,c,clen) == 0) goto err; @@ -471,7 +471,7 @@ void *rdbGenericLoadStringObject(rio *rdb, int flags, size_t *lenptr) { if (len == RDB_LENERR) return NULL; if (plain || sds) { - void *buf = plain ? zmalloc(len) : sdsnewlen(NULL,len); + void *buf = plain ? zmalloc(len) : sdsnewlen(SDS_NOINIT,len); if (lenptr) *lenptr = len; if (len && rioRead(rdb,buf,len) == 0) { if (plain) @@ -482,8 +482,8 @@ void *rdbGenericLoadStringObject(rio *rdb, int flags, size_t *lenptr) { } return buf; } else { - robj *o = encode ? createStringObject(NULL,len) : - createRawStringObject(NULL,len); + robj *o = encode ? createStringObject(SDS_NOINIT,len) : + createRawStringObject(SDS_NOINIT,len); if (len && rioRead(rdb,o->ptr,len) == 0) { decrRefCount(o); return NULL; diff --git a/src/sds.c b/src/sds.c index eafa13c29..c25fd55b5 100644 --- a/src/sds.c +++ b/src/sds.c @@ -39,6 +39,8 @@ #include "sds.h" #include "sdsalloc.h" +const char *SDS_NOINIT = "SDS_NOINIT"; + static inline int sdsHdrSize(char type) { switch(type&SDS_TYPE_MASK) { case SDS_TYPE_5: @@ -72,6 +74,7 @@ static inline char sdsReqType(size_t string_size) { /* Create a new sds string with the content specified by the 'init' pointer * and 'initlen'. * If NULL is used for 'init' the string is initialized with zero bytes. + * If SDS_NOINIT is used, the buffer is left uninitialized; * * The string is always null-termined (all the sds strings are, always) so * even if you create an sds string with: @@ -92,7 +95,9 @@ sds sdsnewlen(const void *init, size_t initlen) { unsigned char *fp; /* flags pointer. */ sh = s_malloc(hdrlen+initlen+1); - if (!init) + if (init==SDS_NOINIT) + init = NULL; + else if (!init) memset(sh, 0, hdrlen+initlen+1); if (sh == NULL) return NULL; s = (char*)sh+hdrlen; diff --git a/src/sds.h b/src/sds.h index 394f8b52e..16e85ce04 100644 --- a/src/sds.h +++ b/src/sds.h @@ -34,6 +34,7 @@ #define __SDS_H #define SDS_MAX_PREALLOC (1024*1024) +const char *SDS_NOINIT; #include #include