当前位置:网站首页>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
边栏推荐
- Codeforces 5 questions par jour (1700 chacune) - jour 6
- 微软语音合成助手 v1.3 文本转语音工具,真实语音AI生成器
- Six stone management: why should leaders ignore product quality
- [ruoyi] ztree custom icon (iconskin attribute)
- 技术分享 | undo 太大了怎么办
- RobotFramework入门(二)appUI自动化之app启动
- Prototype design
- 不赚钱的科大讯飞,投资价值该怎么看?
- codeforces每日5题(均1700)-第六天
- Déduisez la question d'aujourd'hui - 729. Mon emploi du temps I
猜你喜欢
纯Qt版中国象棋:实现双人对战、人机对战及网络对战
JS regular filtering and adding image prefixes in rich text
Li Kou today's question -729 My schedule I
How to accurately identify master data?
MySQL winter vacation self-study 2022 11 (9)
CobaltStrike-4.4-K8修改版安装使用教程
【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 15
【指针训练——八道题】
Master data management theory and Practice
随机推荐
Taobao focus map layout practice
全国大学生信息安全赛创新实践赛初赛---misc(永恒的夜)
My C language learning records (blue bridge) -- files and file input and output
Redis installation
Day 50 - install vsftpd on ceontos6.8
GifCam v7.0 极简GIF动画录制工具中文单文件版
Elimination games
My C language learning record (blue bridge) -- on the pointer
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
I sorted out a classic interview question for my job hopping friends
Apt installation ZABBIX
Modeling specifications: naming conventions
2.13 simulation summary
Shell script updates stored procedure to database
Reverse repackaging of wechat applet
JS events (add, delete) and delegates
【若依(ruoyi)】设置主题样式
Large scale DDoS attacks take Myanmar offline
力扣今日題-729. 我的日程安排錶 I
Pat 1084 broken keyboard (20 points) string find