redis 3.2 新数据结构:quicklist、String的embstr与raw编码方式分界点

Redis3.2.0引入了新的quicklist的数据结构做了list的底层存储方案。废弃了原来的两个配置参数,

list-max-ziplist-entries

list-max-ziplist-value

新增了

list-max-ziplist-size

# Lists are also encoded in a special way to save a lot of space.
# The number of entries allowed per internal list node can be specified
# as a fixed maximum size or a maximum number of elements.
# For a fixed maximum size, use -5 through -1, meaning:
# -5: max size: 64 Kb  <-- not recommended for normal workloads
# -4: max size: 32 Kb  <-- not recommended
# -3: max size: 16 Kb  <-- probably not recommended
# -2: max size: 8 Kb   <-- good
# -1: max size: 4 Kb   <-- good
# Positive numbers mean store up to _exactly_ that number of elements
# per list node.
# The highest performing option is usually -2 (8 Kb size) or -1 (4 Kb size),
# but if your use case is unique, adjust the settings as necessary.
list-max-ziplist-size -2

 

 

二、

String的embstr与raw编码方式不再以39字节为界了, 以44为界。

3.2前

embstr由redisObject和sdshdr组成。

其中redisObject占16个字节

当buf内的字符串长度是39时,sdshdr的大小为8+39+1=48,那一个字节是'\0'

16+48=64

typedef struct redisObject {
    unsigned type:4;
    unsigned encoding:4;
    unsigned lru:REDIS_LRU_BITS; /* lru time (relative to server.lruclock) */
    int refcount;
    void *ptr;
} robj;
struct sdshdr { unsigned int len; unsigned int free; char buf[]; };

 

现在

/* Note: sdshdr5 is never used, we just access the flags byte directly.
 * However is here to document the layout of type 5 SDS strings. */
struct __attribute__ ((__packed__)) sdshdr5 {
    unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
    char buf[];
};
struct __attribute__ ((__packed__)) sdshdr8 {
    uint8_t len; /* used */
    uint8_t alloc; /* excluding the header and null terminator */
    unsigned char flags; /* 3 lsb of type, 5 unused bits */
    char buf[];
};
struct __attribute__ ((__packed__)) sdshdr16 {
    uint16_t len; /* used */
    uint16_t alloc; /* excluding the header and null terminator */
    unsigned char flags; /* 3 lsb of type, 5 unused bits */
    char buf[];
};
struct __attribute__ ((__packed__)) sdshdr32 {
    uint32_t len; /* used */
    uint32_t alloc; /* excluding the header and null terminator */
    unsigned char flags; /* 3 lsb of type, 5 unused bits */
    char buf[];
};
struct __attribute__ ((__packed__)) sdshdr64 {
    uint64_t len; /* used */
    uint64_t alloc; /* excluding the header and null terminator */
    unsigned char flags; /* 3 lsb of type, 5 unused bits */
    char buf[];
};

 

 

redis使用jemalloc分配内存,jemalloc会分配8,16,32,64等字节的内存。

embstr最小为16+8+8+1=33,所以最小分配64字节。

当字符数小于39时,都会分配64字节。这个默认39就是这样来的。 

 

参考:

为什么redis小等于39字节的字符串是embstr编码,大于39是raw编码

 

posted @ 2018-06-01 16:06 等风来。。 Views(...) Comments(...) Edit 收藏
------------------------------------------------------------------------------------------------------------ --------------- 欢迎联系 x.guan.ling@gmail.com--------------- ------------------------------------------------------------------------------------------------------------