mirror of
https://github.com/fluencelabs/redis
synced 2025-06-13 17:21:20 +00:00
Introduction of a new string encoding: EMBSTR
Previously two string encodings were used for string objects: 1) REDIS_ENCODING_RAW: a string object with obj->ptr pointing to an sds stirng. 2) REDIS_ENCODING_INT: a string object where the obj->ptr void pointer is casted to a long. This commit introduces a experimental new encoding called REDIS_ENCODING_EMBSTR that implements an object represented by an sds string that is not modifiable but allocated in the same memory chunk as the robj structure itself. The chunk looks like the following: +--------------+-----------+------------+--------+----+ | robj data... | robj->ptr | sds header | string | \0 | +--------------+-----+-----+------------+--------+----+ | ^ +-----------------------+ The robj->ptr points to the contiguous sds string data, so the object can be manipulated with the same functions used to manipulate plan string objects, however we need just on malloc and one free in order to allocate or release this kind of objects. Moreover it has better cache locality. This new allocation strategy should benefit both the memory usage and the performances. A performance gain between 60 and 70% was observed during micro-benchmarks, however there is more work to do to evaluate the performance impact and the memory usage behavior.
This commit is contained in:
13
src/rdb.c
13
src/rdb.c
@ -325,7 +325,7 @@ int rdbSaveStringObject(rio *rdb, robj *obj) {
|
||||
if (obj->encoding == REDIS_ENCODING_INT) {
|
||||
return rdbSaveLongLongAsStringObject(rdb,(long)obj->ptr);
|
||||
} else {
|
||||
redisAssertWithInfo(NULL,obj,obj->encoding == REDIS_ENCODING_RAW);
|
||||
redisAssertWithInfo(NULL,obj,sdsEncodedObject(obj));
|
||||
return rdbSaveRawString(rdb,obj->ptr,sdslen(obj->ptr));
|
||||
}
|
||||
}
|
||||
@ -795,7 +795,7 @@ robj *rdbLoadObject(int rdbtype, rio *rdb) {
|
||||
/* If we are using a ziplist and the value is too big, convert
|
||||
* the object to a real list. */
|
||||
if (o->encoding == REDIS_ENCODING_ZIPLIST &&
|
||||
ele->encoding == REDIS_ENCODING_RAW &&
|
||||
sdsEncodedObject(ele) &&
|
||||
sdslen(ele->ptr) > server.list_max_ziplist_value)
|
||||
listTypeConvert(o,REDIS_ENCODING_LINKEDLIST);
|
||||
|
||||
@ -869,9 +869,8 @@ robj *rdbLoadObject(int rdbtype, rio *rdb) {
|
||||
if (rdbLoadDoubleValue(rdb,&score) == -1) return NULL;
|
||||
|
||||
/* Don't care about integer-encoded strings. */
|
||||
if (ele->encoding == REDIS_ENCODING_RAW &&
|
||||
sdslen(ele->ptr) > maxelelen)
|
||||
maxelelen = sdslen(ele->ptr);
|
||||
if (sdsEncodedObject(ele) && sdslen(ele->ptr) > maxelelen)
|
||||
maxelelen = sdslen(ele->ptr);
|
||||
|
||||
znode = zslInsert(zs->zsl,score,ele);
|
||||
dictAdd(zs->dict,ele,&znode->score);
|
||||
@ -903,10 +902,10 @@ robj *rdbLoadObject(int rdbtype, rio *rdb) {
|
||||
/* Load raw strings */
|
||||
field = rdbLoadStringObject(rdb);
|
||||
if (field == NULL) return NULL;
|
||||
redisAssert(field->encoding == REDIS_ENCODING_RAW);
|
||||
redisAssert(sdsEncodedObject(field));
|
||||
value = rdbLoadStringObject(rdb);
|
||||
if (value == NULL) return NULL;
|
||||
redisAssert(field->encoding == REDIS_ENCODING_RAW);
|
||||
redisAssert(sdsEncodedObject(value));
|
||||
|
||||
/* Add pair to ziplist */
|
||||
o->ptr = ziplistPush(o->ptr, field->ptr, sdslen(field->ptr), ZIPLIST_TAIL);
|
||||
|
Reference in New Issue
Block a user