当前位置:网站首页>Redis SDS principle
Redis SDS principle
2022-07-06 02:53:00 【FYHannnnnn】
Reprinted from Redis SDS principle _m0_57015193 The blog of -CSDN Blog
redis Use c Language development , But it does not reuse c Native string implementation , I have built a new string implementation :SDS(simple dynamic String),SDS yes redis The default string implementation
SDS principle :
struct sdshdr {
// Record buf The number of bytes used in an array , That is, the length of the used string
int len;
// Record buf The number of unused bytes in an array
int free;
// A character array , To hold strings
char buf[];
};
SDS and Character array comparison :
| C A character array | SDS |
|---|---|
| To get the length of the string, you need to traverse | Go straight back to |
| API unsafe , May cause buffer overflow | API Security , Will not cause buffer overflow |
| Memory reallocation must be called every time the length is modified | Not every time |
| Only text data can be saved | Can save binary |
| You can use all c Library function | Only a part can be reused |
SDS adopt len Property record string length , Therefore, there is no need to traverse the entire character array
out of buffer : The data on the physical address is overwritten
The main reason for buffer overflow is that its length is not recorded : For two consecutive strings on the physical address , The current call strcat When the function links a new string , Later, the string data may be overwritten , Cause buffer overflow .SDS On each modification , First of all, according to the free Property to determine whether there is enough memory , Capacity will be expanded if not enough , Therefore, buffer overflow can be avoided .
For character arrays , Each expansion or contraction depends on memory reallocation , Otherwise, it may cause buffer overflow or memory leak . Due to memory reallocation, system calls need to be executed , Every time the efficiency is very low , In order to avoid expansion becoming redis Performance bottlenecks ,redis Space for time strategy is adopted to reduce the number of memory reallocation :
Space preallocation :SDS At each expansion , Allocate more memory . The length after expansion is less than 1M, Redistribute your own length , Greater than 1M, redistribution 1M
Inert space release : Do not release memory immediately when shrinking , Record first and then free In the attribute , Convenient for subsequent use
SDS The corresponding function is provided to reclaim memory , No memory leakage due to the release of inert space
C Due to various specifications and restrictions, character arrays cannot store binary data , and SDS There are no restrictions on character data , Read and write completely , It won't stop because of an empty string , Therefore, binary data can be saved
SDS Still follow c The property that a character array ends with an empty string , The advantage of doing so is SDS You can reuse a part directly C String function .
It should be noted that ,redis Not all strings in use SDS, For the return value , Log type data still adopts character array
边栏推荐
- Modeling specifications: naming conventions
- JS regular filtering and adding image prefixes in rich text
- Introduction to robotframework (I) brief introduction and use
- XSS challenges bypass the protection strategy for XSS injection
- Prototype design
- 全国大学生信息安全赛创新实践赛初赛---misc(永恒的夜)
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
- Universal crud interface
- MySQL winter vacation self-study 2022 11 (8)
- Reverse repackaging of wechat applet
猜你喜欢

Referenceerror: primordials is not defined error resolution

2345 file shredding, powerful file deletion tool, unbound pure extract version

Network Security Learning - Web vulnerabilities (Part 1)
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 15](/img/72/0fe9cb032339d5f1ccf6f6c24edc57.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 15

Redis delete policy

ERA5再分析资料下载攻略

微服务间通信

主数据管理理论与实践

微软语音合成助手 v1.3 文本转语音工具,真实语音AI生成器

Apt installation ZABBIX
随机推荐
【Unity3D】GUI控件
Microservice registration and discovery
"Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.3 linear algebra_ Learning thinking and exercise answers
Codeworks 5 questions per day (1700 average) - day 6
Six stone management: why should leaders ignore product quality
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
[unity3d] GUI control
codeforces每日5题(均1700)-第六天
[Digital IC manual tearing code] Verilog asynchronous reset synchronous release | topic | principle | design | simulation
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
有没有完全自主的国产化数据库技术
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18
I sorted out a classic interview question for my job hopping friends
[ruoyi] ztree custom icon (iconskin attribute)
MySQL advanced notes
Pat 1084 broken keyboard (20 points) string find
Communication between microservices
Prototype design
How to read excel, PDF and JSON files in R language?
Apt installation ZABBIX