mirror of
https://codeberg.org/redict/redict.git
synced 2025-01-22 16:18:28 -05:00
Avoid excessive malloc and free in copyCommand robj creation (#8067)
Avoid multiple conditional judgments Avoid allocating robj->ptr when we're gonna replace it right after.
This commit is contained in:
parent
7e5a6313f0
commit
25f457c7f6
27
src/t_hash.c
27
src/t_hash.c
@ -515,29 +515,17 @@ robj *hashTypeDup(robj *o) {
|
|||||||
|
|
||||||
serverAssert(o->type == OBJ_HASH);
|
serverAssert(o->type == OBJ_HASH);
|
||||||
|
|
||||||
switch (o->encoding) {
|
|
||||||
case OBJ_ENCODING_ZIPLIST:
|
|
||||||
hobj = createHashObject();
|
|
||||||
break;
|
|
||||||
case OBJ_ENCODING_HT:
|
|
||||||
hobj = createHashObject();
|
|
||||||
hashTypeConvert(hobj, OBJ_ENCODING_HT);
|
|
||||||
dict *d = o->ptr;
|
|
||||||
dictExpand(hobj->ptr, dictSize(d));
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
serverPanic("Wrong encoding.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(o->encoding == OBJ_ENCODING_ZIPLIST){
|
if(o->encoding == OBJ_ENCODING_ZIPLIST){
|
||||||
unsigned char *zl = o->ptr;
|
unsigned char *zl = o->ptr;
|
||||||
size_t sz = ziplistBlobLen(zl);
|
size_t sz = ziplistBlobLen(zl);
|
||||||
unsigned char *new_zl = zmalloc(sz);
|
unsigned char *new_zl = zmalloc(sz);
|
||||||
memcpy(new_zl, zl, sz);
|
memcpy(new_zl, zl, sz);
|
||||||
zfree(hobj->ptr);
|
hobj = createObject(OBJ_HASH, new_zl);
|
||||||
hobj->ptr = new_zl;
|
hobj->encoding = OBJ_ENCODING_ZIPLIST;
|
||||||
} else if(o->encoding == OBJ_ENCODING_HT){
|
} else if(o->encoding == OBJ_ENCODING_HT){
|
||||||
|
dict *d = dictCreate(&hashDictType, NULL);
|
||||||
|
dictExpand(d, dictSize((const dict*)o->ptr));
|
||||||
|
|
||||||
hi = hashTypeInitIterator(o);
|
hi = hashTypeInitIterator(o);
|
||||||
while (hashTypeNext(hi) != C_ERR) {
|
while (hashTypeNext(hi) != C_ERR) {
|
||||||
sds field, value;
|
sds field, value;
|
||||||
@ -549,9 +537,12 @@ robj *hashTypeDup(robj *o) {
|
|||||||
newvalue = sdsdup(value);
|
newvalue = sdsdup(value);
|
||||||
|
|
||||||
/* Add a field-value pair to a new hash object. */
|
/* Add a field-value pair to a new hash object. */
|
||||||
dictAdd(hobj->ptr,newfield,newvalue);
|
dictAdd(d,newfield,newvalue);
|
||||||
}
|
}
|
||||||
hashTypeReleaseIterator(hi);
|
hashTypeReleaseIterator(hi);
|
||||||
|
|
||||||
|
hobj = createObject(OBJ_HASH, d);
|
||||||
|
hobj->encoding = OBJ_ENCODING_HT;
|
||||||
} else {
|
} else {
|
||||||
serverPanic("Unknown hash encoding");
|
serverPanic("Unknown hash encoding");
|
||||||
}
|
}
|
||||||
|
@ -202,14 +202,13 @@ robj *listTypeDup(robj *o) {
|
|||||||
|
|
||||||
switch (o->encoding) {
|
switch (o->encoding) {
|
||||||
case OBJ_ENCODING_QUICKLIST:
|
case OBJ_ENCODING_QUICKLIST:
|
||||||
lobj = createQuicklistObject();
|
lobj = createObject(OBJ_LIST, quicklistDup(o->ptr));
|
||||||
|
lobj->encoding = OBJ_ENCODING_QUICKLIST;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
serverPanic("Wrong encoding.");
|
serverPanic("Wrong encoding.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
zfree(lobj->ptr);
|
|
||||||
lobj->ptr = quicklistDup(o->ptr);
|
|
||||||
return lobj;
|
return lobj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
24
src/t_set.c
24
src/t_set.c
@ -275,27 +275,17 @@ robj *setTypeDup(robj *o) {
|
|||||||
serverAssert(o->type == OBJ_SET);
|
serverAssert(o->type == OBJ_SET);
|
||||||
|
|
||||||
/* Create a new set object that have the same encoding as the original object's encoding */
|
/* Create a new set object that have the same encoding as the original object's encoding */
|
||||||
switch (o->encoding) {
|
if (o->encoding == OBJ_ENCODING_INTSET) {
|
||||||
case OBJ_ENCODING_INTSET:
|
|
||||||
set = createIntsetObject();
|
|
||||||
break;
|
|
||||||
case OBJ_ENCODING_HT:
|
|
||||||
set = createSetObject();
|
|
||||||
dict *d = o->ptr;
|
|
||||||
dictExpand(set->ptr, dictSize(d));
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
serverPanic("Wrong encoding.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (set->encoding == OBJ_ENCODING_INTSET) {
|
|
||||||
intset *is = o->ptr;
|
intset *is = o->ptr;
|
||||||
size_t size = intsetBlobLen(is);
|
size_t size = intsetBlobLen(is);
|
||||||
intset *newis = zmalloc(size);
|
intset *newis = zmalloc(size);
|
||||||
memcpy(newis,is,size);
|
memcpy(newis,is,size);
|
||||||
zfree(set->ptr);
|
set = createObject(OBJ_SET, newis);
|
||||||
set->ptr = newis;
|
set->encoding = OBJ_ENCODING_INTSET;
|
||||||
} else if (set->encoding == OBJ_ENCODING_HT) {
|
} else if (o->encoding == OBJ_ENCODING_HT) {
|
||||||
|
set = createSetObject();
|
||||||
|
dict *d = o->ptr;
|
||||||
|
dictExpand(set->ptr, dictSize(d));
|
||||||
si = setTypeInitIterator(o);
|
si = setTypeInitIterator(o);
|
||||||
while (setTypeNext(si, &elesds, &intobj) != -1) {
|
while (setTypeNext(si, &elesds, &intobj) != -1) {
|
||||||
setTypeAdd(set, elesds);
|
setTypeAdd(set, elesds);
|
||||||
|
24
src/t_zset.c
24
src/t_zset.c
@ -1567,30 +1567,18 @@ robj *zsetDup(robj *o) {
|
|||||||
serverAssert(o->type == OBJ_ZSET);
|
serverAssert(o->type == OBJ_ZSET);
|
||||||
|
|
||||||
/* Create a new sorted set object that have the same encoding as the original object's encoding */
|
/* Create a new sorted set object that have the same encoding as the original object's encoding */
|
||||||
switch (o->encoding) {
|
if (o->encoding == OBJ_ENCODING_ZIPLIST) {
|
||||||
case OBJ_ENCODING_ZIPLIST:
|
|
||||||
zobj = createZsetZiplistObject();
|
|
||||||
break;
|
|
||||||
case OBJ_ENCODING_SKIPLIST:
|
|
||||||
zobj = createZsetObject();
|
|
||||||
zs = o->ptr;
|
|
||||||
new_zs = zobj->ptr;
|
|
||||||
dictExpand(new_zs->dict,dictSize(zs->dict));
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
serverPanic("Wrong encoding.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {
|
|
||||||
unsigned char *zl = o->ptr;
|
unsigned char *zl = o->ptr;
|
||||||
size_t sz = ziplistBlobLen(zl);
|
size_t sz = ziplistBlobLen(zl);
|
||||||
unsigned char *new_zl = zmalloc(sz);
|
unsigned char *new_zl = zmalloc(sz);
|
||||||
memcpy(new_zl, zl, sz);
|
memcpy(new_zl, zl, sz);
|
||||||
zfree(zobj->ptr);
|
zobj = createObject(OBJ_ZSET, new_zl);
|
||||||
zobj->ptr = new_zl;
|
zobj->encoding = OBJ_ENCODING_ZIPLIST;
|
||||||
} else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {
|
} else if (o->encoding == OBJ_ENCODING_SKIPLIST) {
|
||||||
|
zobj = createZsetObject();
|
||||||
zs = o->ptr;
|
zs = o->ptr;
|
||||||
new_zs = zobj->ptr;
|
new_zs = zobj->ptr;
|
||||||
|
dictExpand(new_zs->dict,dictSize(zs->dict));
|
||||||
zskiplist *zsl = zs->zsl;
|
zskiplist *zsl = zs->zsl;
|
||||||
zskiplistNode *ln;
|
zskiplistNode *ln;
|
||||||
sds ele;
|
sds ele;
|
||||||
|
Loading…
Reference in New Issue
Block a user