REDIS_ENCODING_EMBSTR_SIZE_LIMIT set to 39.
比如:
redis 127.0.0.1:2050> set massage "hello_world"
OK
redis 127.0.0.1:2050> object encoding massage
"raw"
为什么这段字符串小于 39 编码却是 raw?另外,小于 39 字节 embstr 编码,大于 39 raw 编码的缘由是什么?
ringa_lee2017-04-22 08:58:25
This is related to the redis version.
Looking at the object.c file of redis-3.0 and the latest version, you can find that when creating StringObject, it will be compared with REIDS_ENCODING_EMBSTR_SIZE_LIMIT. The default value of this is 39.
Looking at the source code of the redis-2.8 version, I found no comparison, but created it directly.
So I guess this embstr encoding only appeared in version 3.0 or above.
As for why it is 39, this is more complicated to explain, so I will explain it slowly.
embstr is a continuous memory area composed of redisObject and sdshdr. Among them, redisObject occupies 16 bytes. When the string length in buf is 39, the size of sdshdr is 8+39+1=48. Which byte is'
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[];
};
Starting from version 2.4, redis starts to use jemalloc memory allocator. This is much better than glibc's malloc and saves memory. It can be simply understood here that jemalloc will allocate 8, 16, 32, 64 bytes of memory. The minimum embstr is 16+8+8+1=33, so the minimum allocation is 64 bytes. When the number of characters is less than 39, 64 bytes are allocated. This is how the default value of 39 comes from.