济南做网站建网站公司,裕华区建设局网站,开发一个小程序流程,各大网站域名Redis中String 的底层实现是什么#xff1f;
Redis 是基于 C 语言编写的#xff0c;但 Redis 的 String 类型的底层实现并不是 C 语言中的字符串#xff08;即以空字符 \0 结尾的字符数组#xff09;#xff0c;而是自己编写了 SDS#xff08;Simple Dynamic String
Redis 是基于 C 语言编写的但 Redis 的 String 类型的底层实现并不是 C 语言中的字符串即以空字符 \0 结尾的字符数组而是自己编写了 SDSSimple Dynamic String简单动态字符串 来作为底层实现。
SDS 最早是 Redis 作者为日常 C 语言开发而设计的 C 字符串后来被应用到了 Redis 上并经过了大量的修改完善以适合高性能操作。
Redis7.0 的 SDS 的部分源码如下redis/src/sds.h at 7.0 · redis/redis · GitHub: /* 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 共有五种实现方式 SDS_TYPE_5并未用到、SDS_TYPE_8、SDS_TYPE_16、SDS_TYPE_32、SDS_TYPE_64其中只有后四种实际用到。Redis 会根据初始化的长度决定使用哪种类型从而减少内存的使用。
类型字节位sdshdr5 18sdshdr818sdshdr16216sdshdr32432sdshdr64864
对于后四种实现都包含了下面这 4 个属性 len字符串的长度也就是已经使用的字节数 alloc总共可用的字符空间大小alloc-len 就是 SDS 剩余的空间大小 buf[]实际存储字符串的数组 flags低三位保存类型标志
SDS 相比于 C 语言中的字符串有如下提升 可以避免缓冲区溢出C 语言中的字符串被修改比如拼接时一旦没有分配足够长度的内存空间就会造成缓冲区溢出。SDS 被修改时会先根据 len 属性检查空间大小是否满足要求如果不满足则先扩展至所需大小再进行修改操作。 获取字符串长度的复杂度较低C 语言中的字符串的长度通常是经过遍历计数来实现的时间复杂度为 O(n)。SDS 的长度获取直接读取 len 属性即可时间复杂度为 O(1)。 减少内存分配次数为了避免修改增加/减少字符串时每次都需要重新分配内存C 语言的字符串是这样的SDS 实现了空间预分配和惰性空间释放两种优化策略。当 SDS 需要增加字符串时Redis 会为 SDS 分配好内存并且根据特定的算法分配多余的内存这样可以减少连续执行字符串增长操作所需的内存重分配次数。当 SDS 需要减少字符串时这部分内存不会立即被回收会被记录下来等待后续使用支持手动释放有对应的 API。 二进制安全C 语言中的字符串以空字符 \0 作为字符串结束的标识这存在一些问题像一些二进制文件比如图片、视频、音频就可能包括空字符C 字符串无法正确保存。SDS 使用 len 属性判断字符串是否结束不存在这个问题。 多提一嘴很多文章里 SDS 的定义是下面这样的 struct sdshdr {unsigned int len;unsigned int free;char buf[];
};
这个也没错Redis 3.2 之前就是这样定义的。后来由于这种方式的定义存在问题len 和 free 的定义用了 4 个字节造成了浪费。Redis 3.2 之后Redis 改进了 SDS 的定义将其划分为了现在的 5 种类型。