当前位置:网站首页>Redis-SDS
Redis-SDS
2022-06-26 00:23:00 【云胡不喜呀~】
简单动态字符串(SDS)
SDS定义
在C语言中字符串是以空字符结尾的字符数组
在redis中SDS的实现与C语言类似但是没有直接使用C语言的字符串类型 伪代码如下:
SDS {
//字符串长度
int len;
//数组中剩余长度
int free;
//字节数组 用于保存字符串
//结尾为'/0'可以方便找到插入位置
char[] buf[]
}
SDS与C区别
C语言字符串获取长度时间复杂度为O(n)SDS直接获取len复杂度为O(1)
C语言在扩容或产生缓冲区溢出问题 SDS会先检查空间是否足够如果不足够会进行扩容
| C字符串 | SDS |
|---|---|
| 获取长度复杂度为O(N) | 获取长度复杂度为O(1) |
| API是不安全的,可能会造成缓冲区溢出 | API是安全的,不会造成缓冲区溢出 |
| 修改字符串N次必然执行N次内存分配 | 最多执行N次内存分配 |
| 只能 保存文本数据 | 可以保存文本和二进制数据 |
| 可以只用所有<string.h>库中的函数 | 部分使用 |
SDS的空间分配策略
空间预分配策略
- 在进行修改时判断是否足够不够的话 会扩容一倍 如修改之后为10字节 会扩容到10+10+1 字节
- SDS长度大于1MB每次修改会增加1MB+1字节
- 使得增长N次字符串最多增长N次
惰性空间释放
- 在SDS缩短时会不会真正的释放空间而是增加free 方便下次增加使用
- 如果需要真正的释放需要调用相关API
边栏推荐
- GUN make (7) 执行make
- 物联网亿万级通信一站式解决方案EMQ
- 关于strlen与sizeof的区别
- Chinese and English instructions of collagen enzyme Worthington
- Data analysis - similarities and differences between C-end and b-end data analysis
- cyclegan:unpaired image-to-image translation using cycle-consistent adversarial network
- Rollback Protection
- OTA trigger
- 判定积分给业务带来价值的两个指标
- Abnova丨CSV 单克隆抗体解决方案
猜你喜欢

丨EGFR FISH 探针解决方案

论文阅读 Exploring Temporal Information for Dynamic Network Embedding

蒟蒻初学单片机的一丢丢笔记

秀场精灵陈梓桐 受邀担任第六季完美童模全球总决赛首席体验官

About vs scanf, 'scanf' appears: this function or variable may be unsafe Solutions to the problem of consumer usi

CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕

Mot - clé C facile à comprendre statique

On the difference between strlen and sizeof

前置++,后置++与前置--与后置--(++a,a++与--a,a--)

Differences and functions of TOS cos DSCP
随机推荐
wifi 相关知识详解
Chinese and English instructions of collagen enzyme Worthington
Principle of voice wake-up
recvmsg & sendmsg
Steps of program compilation precompile compilation assembly connection
PTA class a simulated seventh bomb: 1160-1163
Interpretation of script corresponding to postman assertion
readv & writev
元气少女王钰洁 受邀担任第六季完美童模全球总决赛代言人
Gun make (7) execute make
Detailed explanation of memory leak check tools
UN make (6) makefile的条件执行
轻轻松松理解指针
Assertion of postman interface test
Energetic girl wangyujie was invited to be the spokesperson for the global finals of the sixth season perfect children's model
OTA trigger
甜酷少女金书伊 受邀担任第六季完美童模全球总决赛代言人
PTA class a simulated first bomb: 1132-1135
CyCa children's physical etiquette Yueqing City training results assessment successfully concluded
What happens from entering a web address in the browser's input box to seeing the contents of the web page?