当前位置:网站首页>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
边栏推荐
- MySQL winter vacation self-study 2022 11 (8)
- How to accurately identify master data?
- 有没有完全自主的国产化数据库技术
- Taobao focus map layout practice
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
- [ruoyi] set theme style
- 微服务间通信
- Shell script updates stored procedure to database
- JS events (add, delete) and delegates
- Redis delete policy
猜你喜欢
米家、涂鸦、Hilink、智汀等生态哪家强?5大主流智能品牌分析
[matlab] access of variables and files
XSS challenges绕过防护策略进行 XSS 注入
Pure QT version of Chinese chess: realize two-man, man-machine and network games
Deeply analyze the chain 2+1 mode, and subvert the traditional thinking of selling goods?
如何精准识别主数据?
Introduction to robotframework (II) app startup of appui automation
Redis delete policy
2345文件粉碎,文件强力删除工具无捆绑纯净提取版
2022.02.13
随机推荐
4. File modification
RobotFramework入门(三)WebUI自动化之百度搜索
MySQL learning notes-10-tablespace recycling
CSP numeric sort
"Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.5 automatic differentiation_ Learning thinking and exercise answers
codeforces每日5题(均1700)-第六天
How does yyds dry inventory deal with repeated messages in the consumption process?
Apt installation ZABBIX
纯Qt版中国象棋:实现双人对战、人机对战及网络对战
JS events (add, delete) and delegates
PMP practice once a day | don't get lost in the exam -7.5
Communication between microservices
MySQL winter vacation self-study 2022 11 (9)
【若依(ruoyi)】设置主题样式
My C language learning record (blue bridge) -- on the pointer
07 singleton mode
Solve 9 with C language × 9 Sudoku (personal test available) (thinking analysis)
Codeforces 5 questions par jour (1700 chacune) - jour 6
Gifcam v7.0 minimalist GIF animation recording tool Chinese single file version
Zhang Lijun: penetrating uncertainty depends on four "invariants"