原理
一个字符串会预先分配一段空间,并描述清楚已用空间和剩余空间,比如如下示例代码:
struct str {
int len; // buf 中已经使用空间
int free;// buf中剩余空闲空间
char buf[]; //数据空间
}
redis 做为一个内存型数据库,其中必然会需要类似的数据结构描述字符串,在此情况下以下问题需要重点考虑:
- 由于存储的数据长度不一,例如个位数长度短字符串,会发现若用int描述长度在大量短字符串实例的情况下其实内存利用率是不高的
- 对于字符串操作,需要有统一的操作接口
对于以上问题点Redis做出以下设计:
- 使用 不同的struct来匹配不同的场景,不同的类型使用flags来识别
- 定义SDS类型,字符串的操作通过sds来实现,内部的差异通过f识别flags来分别处理
数据结构
typedef char *sds;
/* 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[];
};
从以上结构定义看出
- sds就是字符指针
- 所有的数据结构均按字节对齐
- 数据结构的定义可以分成两部分,头部和实际数据部分(char buf[],默认不占空间)
- flages在实际数据前一个字节位置,当前数据类型仅用flags的3个bit
- 在实际的使用过程中sds就是指向了 char buf[] 的首地址
- sdshdr5没有长度和分配空间属性,用flags的高5bit存储数据长度
函数
数据结构类型识别
根据数据结构的描述,redis定义了以下类型
#define SDS_TYPE_5 0
#define SDS_TYPE_8 1
#define SDS_TYPE_16 2
#define SDS_TYPE_32 3
#define SDS_TYPE_64 4
#define SDS_TYPE_MASK 7
#define SDS_TYPE_BITS 3
当在封装函数中需要识别具体类型时,只需找到flags属性,然后查看低3bit的具体值即可,比如下面的长度查询函数
static inline size_t sdslen(const sds s) {
unsigned char flags = s[-1]; //按照数据结构定义,flags永远在实际数据的前面一个字节,故减1
switch(flags&SDS_TYPE_MASK) {
case SDS_TYPE_5:
return SDS_TYPE_5_LEN(flags);
case SDS_TYPE_8:
return SDS_HDR(8,s)->len;
case SDS_TYPE_16:
return SDS_HDR(16,s)->len;
case SDS_TYPE_32:
return SDS_HDR(32,s)->len;
case SDS_TYPE_64:
return SDS_HDR(64,s)->len;
}
return 0;
}
创建字符串
//找出数据结构的起始位置指针的宏
#define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (void*)((s)-(sizeof(struct sdshdr##T)));
//计算出一个具体数据结构的头部空间所占字节
static inline int sdsHdrSize(char type) {
switch(type&SDS_TYPE_MASK) {
case SDS_TYPE_5:
return sizeof(struct sdshdr5);
case SDS_TYPE_8:
return sizeof(struct sdshdr8);
case SDS_TYPE_16:
return sizeof(struct sdshdr16);
case SDS_TYPE_32:
return sizeof(struct sdshdr32);
case SDS_TYPE_64:
return sizeof(struct sdshdr64);
}
return 0;
}
//通过字符串的长度匹配一个适合的结构
static inline char sdsReqType(size_t string_size) {
if (string_size < 1<<5)
return SDS_TYPE_5;
if (string_size < 1<<8)
return SDS_TYPE_8;
if (string_size < 1<<16)
return SDS_TYPE_16;
#if (LONG_MAX == LLONG_MAX)
if (string_size < 1ll<<32)
return SDS_TYPE_32;
return SDS_TYPE_64;
#else
return SDS_TYPE_32;
#endif
}
/* Create a new sds string with the content specified by the 'init' pointer
* and 'initlen'.
* If NULL is used for 'init' the string is initialized with zero bytes.
* If SDS_NOINIT is used, the buffer is left uninitialized;
*
* The string is always null-termined (all the sds strings are, always) so
* even if you create an sds string with:
*
* mystring = sdsnewlen("abc",3);
*
* You can print the string with printf() as there is an implicit \0 at the
* end of the string. However the string is binary safe and can contain
* \0 characters in the middle, as the length is stored in the sds header. */
sds sdsnewlen(const void *init, size_t initlen) {
void *sh;
sds s;
char type = sdsReqType(initlen); // 先识别出需要创建的数据类型
/* Empty strings are usually created in order to append. Use type 8
* since type 5 is not good at this. */
if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8;
int hdrlen = sdsHdrSize(type);
unsigned char *fp; /* flags pointer. */
sh = s_malloc(hdrlen+initlen+1); //分配数据空间,需要考虑头部结构大小,数据长度以及一个结束符空间
if (init==SDS_NOINIT)
init = NULL;
else if (!init)
memset(sh, 0, hdrlen+initlen+1);
if (sh == NULL) return NULL; //判断逻辑应该在前面
s = (char*)sh+hdrlen; //返回sds对象赋值,即buf的地址
fp = ((unsigned char*)s)-1; //找到flags
switch(type) { //依据数据类型初始化头部空间
case SDS_TYPE_5: {
*fp = type | (initlen << SDS_TYPE_BITS);
break;
}
case SDS_TYPE_8: {
SDS_HDR_VAR(8,s);
sh->len = initlen;
sh->alloc = initlen;
*fp = type;
break;
}
case SDS_TYPE_16: {
SDS_HDR_VAR(16,s);
sh->len = initlen;
sh->alloc = initlen;
*fp = type;
break;
}
case SDS_TYPE_32: {
SDS_HDR_VAR(32,s);
sh->len = initlen;
sh->alloc = initlen;
*fp = type;
break;
}
case SDS_TYPE_64: {
SDS_HDR_VAR(64,s);
sh->len = initlen;
sh->alloc = initlen;
*fp = type;
break;
}
}
if (initlen && init)
memcpy(s, init, initlen);
s[initlen] = '\0'; //将结束符写入数据的最后位置
return s;
}
字符串扩容
//获取剩余有效的空间
static inline size_t sdsavail(const sds s) {
unsigned char flags = s[-1]; // 通过flags查询数据结构类型
switch(flags&SDS_TYPE_MASK) {
case SDS_TYPE_5: { //由前面定义可知sdshdr5只分配实际需要的空间
return 0;
}
case SDS_TYPE_8: {
SDS_HDR_VAR(8,s);
return sh->alloc - sh->len; //通过总分配空间减去已用空间得出剩余空间大小
}
case SDS_TYPE_16: {
SDS_HDR_VAR(16,s);
return sh->alloc - sh->len;
}
case SDS_TYPE_32: {
SDS_HDR_VAR(32,s);
return sh->alloc - sh->len;
}
case SDS_TYPE_64: {
SDS_HDR_VAR(64,s);
return sh->alloc - sh->len;
}
}
return 0;
}
/* Enlarge the free space at the end of the sds string so that the caller
* is sure that after calling this function can overwrite up to addlen
* bytes after the end of the string, plus one more byte for nul term.
*
* Note: this does not change the *length* of the sds string as returned
* by sdslen(), but only the free buffer space we have. */
sds sdsMakeRoomFor(sds s, size_t addlen) {
void *sh, *newsh;
size_t avail = sdsavail(s);
size_t len, newlen;
char type, oldtype = s[-1] & SDS_TYPE_MASK;
int hdrlen;
/* Return ASAP if there is enough space left. */
if (avail >= addlen) return s; //有充足的剩余空间则可立刻返回
len = sdslen(s);
sh = (char*)s-sdsHdrSize(oldtype);
newlen = (len+addlen);
//若最终需要空间小于SDS_MAX_PREALLOC则分配空间大小翻倍,否则仅增加SDS_MAX_PREALLOC
if (newlen < SDS_MAX_PREALLOC)
newlen *= 2;
else
newlen += SDS_MAX_PREALLOC;
//评估可容纳最后大小空间的数据结构类型
type = sdsReqType(newlen);
/* Don't use type 5: the user is appending to the string and type 5 is
* not able to remember empty space, so sdsMakeRoomFor() must be called
* at every appending operation. */
if (type == SDS_TYPE_5) type = SDS_TYPE_8;
//计算头部空间大小
hdrlen = sdsHdrSize(type);
if (oldtype==type) {
//类型一致可以使用s_realloc重新分配
newsh = s_realloc(sh, hdrlen+newlen+1);
if (newsh == NULL) return NULL;
s = (char*)newsh+hdrlen;
} else {
/* Since the header size changes, need to move the string forward,
* and can't use realloc */
//类型不一致则需要
// 1. 用s_malloc分配空间,
// 2. 拷贝有效的数据
// 3. 释放旧的空间
newsh = s_malloc(hdrlen+newlen+1);
if (newsh == NULL) return NULL;
memcpy((char*)newsh+hdrlen, s, len+1);
s_free(sh);
s = (char*)newsh+hdrlen;
s[-1] = type; // 设置类型和长度
sdssetlen(s, len);
}
//设置新分配空间的长度
sdssetalloc(s, newlen);
return s;
}
字符串缩减空间
/* Reallocate the sds string so that it has no free space at the end. The
* contained string remains not altered, but next concatenation operations
* will require a reallocation.
*
* After the call, the passed sds string is no longer valid and all the
* references must be substituted with the new pointer returned by the call. */
sds sdsRemoveFreeSpace(sds s) {
void *sh, *newsh;
char type, oldtype = s[-1] & SDS_TYPE_MASK;
int hdrlen, oldhdrlen = sdsHdrSize(oldtype);
size_t len = sdslen(s);
size_t avail = sdsavail(s);//获取剩余空间大小
sh = (char*)s-oldhdrlen;
/* Return ASAP if there is no space left. */
if (avail == 0) return s; //无剩余空间即可直接返回
/* Check what would be the minimum SDS header that is just good enough to
* fit this string. */
type = sdsReqType(len); //获取满足当前字符串有效长度的数据类型和头部空间大小
hdrlen = sdsHdrSize(type);
/* If the type is the same, or at least a large enough type is still
* required, we just realloc(), letting the allocator to do the copy
* only if really needed. Otherwise if the change is huge, we manually
* reallocate the string to use the different header type. */
if (oldtype==type || type > SDS_TYPE_8) {
//若前后数据类型一致,或者是大数据类型则直接使用s_realloc分配空间
newsh = s_realloc(sh, oldhdrlen+len+1);
if (newsh == NULL) return NULL;
s = (char*)newsh+oldhdrlen;
} else {
//需要修改类型的情况下
// 1.使用s_malloc分配空间
// 2. 拷贝有效数据,
// 3.释放旧空间
// 4. 初始化头部信息
newsh = s_malloc(hdrlen+len+1);
if (newsh == NULL) return NULL;
memcpy((char*)newsh+hdrlen, s, len+1);
s_free(sh);
s = (char*)newsh+hdrlen;
s[-1] = type;
sdssetlen(s, len);
}
// 设置可存储数据的最大空间
sdssetalloc(s, len);
return s;
}
释放字符串
/* Free an sds string. No operation is performed if 's' is NULL. */
void sdsfree(sds s) {
if (s == NULL) return;
s_free((char*)s-sdsHdrSize(s[-1]));//依据flags信息找到数据结构的起始指针然后释放空间
}