redis 3.2 新數據結構:quicklist、String的embstr與raw編碼方式分界點

Redis3.2.0引入了新的quicklist的數據結構作了list的底層存儲方案。廢棄了原來的兩個配置參數,node

list-max-ziplist-entriesredis

list-max-ziplist-value數據結構

新增了ui

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

 

 

2、spa

String的embstr與raw編碼方式再也不以39字節爲界了, 以44爲界。code

3.2前orm

embstr由redisObject和sdshdr組成。server

其中redisObject佔16個字節blog

當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編碼

相關文章
相關標籤/搜索