当前位置: 首页 > news >正文

Redis中String 的底层实现是什么?

Redis中String 的底层实现是什么?

Redis 是基于 C 语言编写的,但 Redis 的 String 类型的底层实现并不是 C 语言中的字符串(即以空字符 \0 结尾的字符数组),而是自己编写了 SDS(Simple 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< 1<8
sdshdr818
sdshdr16216
sdshdr32432
sdshdr64864

对于后四种实现都包含了下面这 4 个属性:

  • len:字符串的长度也就是已经使用的字节数

  • alloc:总共可用的字符空间大小,alloc-len 就是 SDS 剩余的空间大小

  • buf[]:实际存储字符串的数组

  • flags:低三位保存类型标志

SDS 相比于 C 语言中的字符串有如下提升:

  1. 可以避免缓冲区溢出:C 语言中的字符串被修改(比如拼接)时,一旦没有分配足够长度的内存空间,就会造成缓冲区溢出。SDS 被修改时,会先根据 len 属性检查空间大小是否满足要求,如果不满足,则先扩展至所需大小再进行修改操作。

  2. 获取字符串长度的复杂度较低:C 语言中的字符串的长度通常是经过遍历计数来实现的,时间复杂度为 O(n)。SDS 的长度获取直接读取 len 属性即可,时间复杂度为 O(1)。

  3. 减少内存分配次数:为了避免修改(增加/减少)字符串时,每次都需要重新分配内存(C 语言的字符串是这样的),SDS 实现了空间预分配和惰性空间释放两种优化策略。当 SDS 需要增加字符串时,Redis 会为 SDS 分配好内存,并且根据特定的算法分配多余的内存,这样可以减少连续执行字符串增长操作所需的内存重分配次数。当 SDS 需要减少字符串时,这部分内存不会立即被回收,会被记录下来,等待后续使用(支持手动释放,有对应的 API)。

  4. 二进制安全:C 语言中的字符串以空字符 \0 作为字符串结束的标识,这存在一些问题,像一些二进制文件(比如图片、视频、音频)就可能包括空字符,C 字符串无法正确保存。SDS 使用 len 属性判断字符串是否结束,不存在这个问题。

🤐 多提一嘴,很多文章里 SDS 的定义是下面这样的:

struct sdshdr {unsigned int len;unsigned int free;char buf[];
};

这个也没错,Redis 3.2 之前就是这样定义的。后来,由于这种方式的定义存在问题,lenfree 的定义用了 4 个字节,造成了浪费。Redis 3.2 之后,Redis 改进了 SDS 的定义,将其划分为了现在的 5 种类型。

http://www.lryc.cn/news/475496.html

相关文章:

  • 像mysql一样查询es
  • SpringBoot中@Validated或@Valid注解校验的使用
  • HashMap为什么线程不安全?
  • 类加载器及反射
  • aws boto3 下载文件
  • 3DDFA-V3——基于人脸分割几何信息指导下的三维人脸重建
  • 求串长(不使用任何字符串库函数)
  • 第02章 MySQL环境搭建
  • linux系统编程 man查看manual.stat
  • 从网络到缓存:在Android中高效管理图片加载
  • 【数据结构】链表详解:数据节点的链接原理
  • 使用AWS Redshift从AWS MSK中读取数据
  • 从0开始学统计-数据类别与测量层次
  • 使用AIM对SAP PO核心指标的自动化巡检监控
  • C++——unordered_map和unordered_set的封装
  • 微信小程序scroll-view吸顶css样式化表格的表头及iOS上下滑动表头的颜色覆盖、z-index应用及性能分析
  • 【高中数学】数列
  • 数字媒体技术基础:AMF(ACES 元数据文件 )
  • Apache Dubbo (RPC框架)
  • LeetCode 3226. 使两个整数相等的位更改次数
  • 面试经典 150 题:189、383
  • Python模拟真人动态生成鼠标滑动路径
  • 如何压缩pdf文件的大小?5分钟压缩pdf的方法推荐
  • 【SQL】[2BP01] ERROR: cannot drop table course because other objects depend on it
  • gbase8s之spring框架用druid中间件报语法错误
  • 【网络安全】|nessus使用
  • CSRA2的LINUX操作系统24年11月2日上午上课笔记
  • 通过分解质因数求若干个数的最小公倍数
  • 数据库三范式(1NF、2NF、3NF)
  • C语言_数据结构_顺序表