mirror of
https://codeberg.org/redict/redict.git
synced 2025-01-23 16:48:27 -05:00
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.
This commit is contained in:
parent
95883313b5
commit
f86df924b0
@ -699,7 +699,7 @@ int loadAppendOnlyFile(char *filename) {
|
|||||||
}
|
}
|
||||||
if (buf[0] != '$') goto fmterr;
|
if (buf[0] != '$') goto fmterr;
|
||||||
len = strtol(buf+1,NULL,10);
|
len = strtol(buf+1,NULL,10);
|
||||||
argsds = sdsnewlen(NULL,len);
|
argsds = sdsnewlen(SDS_NOINIT,len);
|
||||||
if (len && fread(argsds,len,1,fp) == 0) {
|
if (len && fread(argsds,len,1,fp) == 0) {
|
||||||
sdsfree(argsds);
|
sdsfree(argsds);
|
||||||
fakeClient->argc = j; /* Free up to j-1. */
|
fakeClient->argc = j; /* Free up to j-1. */
|
||||||
|
@ -1234,7 +1234,7 @@ int processMultibulkBuffer(client *c) {
|
|||||||
sdsIncrLen(c->querybuf,-2); /* remove CRLF */
|
sdsIncrLen(c->querybuf,-2); /* remove CRLF */
|
||||||
/* Assume that if we saw a fat argument we'll see another one
|
/* Assume that if we saw a fat argument we'll see another one
|
||||||
* likely... */
|
* likely... */
|
||||||
c->querybuf = sdsnewlen(NULL,c->bulklen+2);
|
c->querybuf = sdsnewlen(SDS_NOINIT,c->bulklen+2);
|
||||||
sdsclear(c->querybuf);
|
sdsclear(c->querybuf);
|
||||||
pos = 0;
|
pos = 0;
|
||||||
} else {
|
} else {
|
||||||
@ -1477,7 +1477,7 @@ sds getAllClientsInfoString(void) {
|
|||||||
listNode *ln;
|
listNode *ln;
|
||||||
listIter li;
|
listIter li;
|
||||||
client *client;
|
client *client;
|
||||||
sds o = sdsnewlen(NULL,200*listLength(server.clients));
|
sds o = sdsnewlen(SDS_NOINIT,200*listLength(server.clients));
|
||||||
sdsclear(o);
|
sdsclear(o);
|
||||||
listRewind(server.clients,&li);
|
listRewind(server.clients,&li);
|
||||||
while ((ln = listNext(&li)) != NULL) {
|
while ((ln = listNext(&li)) != NULL) {
|
||||||
|
@ -98,7 +98,9 @@ robj *createEmbeddedStringObject(const char *ptr, size_t len) {
|
|||||||
sh->len = len;
|
sh->len = len;
|
||||||
sh->alloc = len;
|
sh->alloc = len;
|
||||||
sh->flags = SDS_TYPE_8;
|
sh->flags = SDS_TYPE_8;
|
||||||
if (ptr) {
|
if (ptr == SDS_NOINIT)
|
||||||
|
sh->buf[len] = '\0';
|
||||||
|
else if (ptr) {
|
||||||
memcpy(sh->buf,ptr,len);
|
memcpy(sh->buf,ptr,len);
|
||||||
sh->buf[len] = '\0';
|
sh->buf[len] = '\0';
|
||||||
} else {
|
} else {
|
||||||
|
12
src/rdb.c
12
src/rdb.c
@ -254,7 +254,7 @@ void *rdbLoadIntegerObject(rio *rdb, int enctype, int flags, size_t *lenptr) {
|
|||||||
char buf[LONG_STR_SIZE], *p;
|
char buf[LONG_STR_SIZE], *p;
|
||||||
int len = ll2string(buf,sizeof(buf),val);
|
int len = ll2string(buf,sizeof(buf),val);
|
||||||
if (lenptr) *lenptr = len;
|
if (lenptr) *lenptr = len;
|
||||||
p = plain ? zmalloc(len) : sdsnewlen(NULL,len);
|
p = plain ? zmalloc(len) : sdsnewlen(SDS_NOINIT,len);
|
||||||
memcpy(p,buf,len);
|
memcpy(p,buf,len);
|
||||||
return p;
|
return p;
|
||||||
} else if (encode) {
|
} else if (encode) {
|
||||||
@ -343,10 +343,10 @@ void *rdbLoadLzfStringObject(rio *rdb, int flags, size_t *lenptr) {
|
|||||||
/* Allocate our target according to the uncompressed size. */
|
/* Allocate our target according to the uncompressed size. */
|
||||||
if (plain) {
|
if (plain) {
|
||||||
val = zmalloc(len);
|
val = zmalloc(len);
|
||||||
if (lenptr) *lenptr = len;
|
|
||||||
} else {
|
} else {
|
||||||
val = sdsnewlen(NULL,len);
|
val = sdsnewlen(SDS_NOINIT,len);
|
||||||
}
|
}
|
||||||
|
if (lenptr) *lenptr = len;
|
||||||
|
|
||||||
/* Load the compressed representation and uncompress it to target. */
|
/* Load the compressed representation and uncompress it to target. */
|
||||||
if (rioRead(rdb,c,clen) == 0) goto err;
|
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 (len == RDB_LENERR) return NULL;
|
||||||
if (plain || sds) {
|
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 (lenptr) *lenptr = len;
|
||||||
if (len && rioRead(rdb,buf,len) == 0) {
|
if (len && rioRead(rdb,buf,len) == 0) {
|
||||||
if (plain)
|
if (plain)
|
||||||
@ -482,8 +482,8 @@ void *rdbGenericLoadStringObject(rio *rdb, int flags, size_t *lenptr) {
|
|||||||
}
|
}
|
||||||
return buf;
|
return buf;
|
||||||
} else {
|
} else {
|
||||||
robj *o = encode ? createStringObject(NULL,len) :
|
robj *o = encode ? createStringObject(SDS_NOINIT,len) :
|
||||||
createRawStringObject(NULL,len);
|
createRawStringObject(SDS_NOINIT,len);
|
||||||
if (len && rioRead(rdb,o->ptr,len) == 0) {
|
if (len && rioRead(rdb,o->ptr,len) == 0) {
|
||||||
decrRefCount(o);
|
decrRefCount(o);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -39,6 +39,8 @@
|
|||||||
#include "sds.h"
|
#include "sds.h"
|
||||||
#include "sdsalloc.h"
|
#include "sdsalloc.h"
|
||||||
|
|
||||||
|
const char *SDS_NOINIT = "SDS_NOINIT";
|
||||||
|
|
||||||
static inline int sdsHdrSize(char type) {
|
static inline int sdsHdrSize(char type) {
|
||||||
switch(type&SDS_TYPE_MASK) {
|
switch(type&SDS_TYPE_MASK) {
|
||||||
case SDS_TYPE_5:
|
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
|
/* Create a new sds string with the content specified by the 'init' pointer
|
||||||
* and 'initlen'.
|
* and 'initlen'.
|
||||||
* If NULL is used for 'init' the string is initialized with zero bytes.
|
* 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
|
* The string is always null-termined (all the sds strings are, always) so
|
||||||
* even if you create an sds string with:
|
* 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. */
|
unsigned char *fp; /* flags pointer. */
|
||||||
|
|
||||||
sh = s_malloc(hdrlen+initlen+1);
|
sh = s_malloc(hdrlen+initlen+1);
|
||||||
if (!init)
|
if (init==SDS_NOINIT)
|
||||||
|
init = NULL;
|
||||||
|
else if (!init)
|
||||||
memset(sh, 0, hdrlen+initlen+1);
|
memset(sh, 0, hdrlen+initlen+1);
|
||||||
if (sh == NULL) return NULL;
|
if (sh == NULL) return NULL;
|
||||||
s = (char*)sh+hdrlen;
|
s = (char*)sh+hdrlen;
|
||||||
|
Loading…
Reference in New Issue
Block a user