当前位置:网站首页>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
边栏推荐
- ERA5再分析资料下载攻略
- 解决:AttributeError: ‘str‘ object has no attribute ‘decode‘
- 2345文件粉碎,文件强力删除工具无捆绑纯净提取版
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
- C # create self host webservice
- 不赚钱的科大讯飞,投资价值该怎么看?
- Microsoft speech synthesis assistant v1.3 text to speech tool, real speech AI generator
- Pure QT version of Chinese chess: realize two-man, man-machine and network games
- What is the investment value of iFLYTEK, which does not make money?
- DDoS attacks - are we really at war?
猜你喜欢

PMP practice once a day | don't get lost in the exam -7.5

Microsoft speech synthesis assistant v1.3 text to speech tool, real speech AI generator

C language - Blue Bridge Cup - promised score

JS regular filtering and adding image prefixes in rich text
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6](/img/38/51797fcdb57159b48d0e0a72eeb580.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6

纯Qt版中国象棋:实现双人对战、人机对战及网络对战
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19](/img/7c/f728e88ca36524f92c56213370399b.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19

Redis delete policy
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23](/img/72/a80ee7ee7b967b0afa6018070d03c9.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23

1. Dynamic parameters of function: *args, **kwargs
随机推荐
RobotFramework入门(二)appUI自动化之app启动
建模规范:命名规范
How to read excel, PDF and JSON files in R language?
Network Security Learning - Web vulnerabilities (Part 1)
My C language learning record (blue bridge) -- on the pointer
[ruoyi] set theme style
ERA5再分析资料下载攻略
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
Yyds dry inventory comparison of several database storage engines
Modeling specifications: naming conventions
MySQL winter vacation self-study 2022 11 (7)
Li Kou today's question -729 My schedule I
[kubernetes series] learn the exposed application of kubernetes service security
Atcoder beginer contest 233 (a~d) solution
微服务注册与发现
Large scale DDoS attacks take Myanmar offline
[matlab] access of variables and files
[Chongqing Guangdong education] higher mathematics I reference materials of Southwest Petroleum University
Redis delete policy
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18