当前位置:网站首页>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
边栏推荐
- Master data management theory and Practice
- My C language learning record (blue bridge) -- on the pointer
- Apt installation ZABBIX
- [Yu Yue education] basic reference materials of digital electronic technology of Xi'an University of Technology
- Trends in DDoS Attacks
- Deeply analyze the chain 2+1 mode, and subvert the traditional thinking of selling goods?
- ERA5再分析资料下载攻略
- [pointer training - eight questions]
- Redis installation
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 12
猜你喜欢

Introduction to robotframework (II) app startup of appui automation

一个复制也能玩出花来

主数据管理(MDM)的成熟度

My C language learning record (blue bridge) -- under the pointer

MySQL advanced notes
![[Chongqing Guangdong education] higher mathematics I reference materials of Southwest Petroleum University](/img/0f/520242492524522c887b6576463566.jpg)
[Chongqing Guangdong education] higher mathematics I reference materials of Southwest Petroleum University

Referenceerror: primordials is not defined error resolution

MySQL advanced notes
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16](/img/c3/f3746b161012acc3751b2bd0b8f663.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8](/img/16/33f5623625ba817e6e022b5cb7ff5d.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8
随机推荐
Maturity of master data management (MDM)
MySQL winter vacation self-study 2022 11 (7)
The difference between sizeof and strlen in C language
2.12 simulation
【Unity3D】GUI控件
Single instance mode of encapsulating PDO with PHP in spare time
Prototype design
【Kubernetes 系列】一文学会Kubernetes Service安全的暴露应用
XSS challenges绕过防护策略进行 XSS 注入
张丽俊:穿透不确定性要靠四个“不变”
PMP practice once a day | don't get lost in the exam -7.5
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
Solve 9 with C language × 9 Sudoku (personal test available) (thinking analysis)
GifCam v7.0 极简GIF动画录制工具中文单文件版
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
My C language learning records (blue bridge) -- files and file input and output
力扣今日題-729. 我的日程安排錶 I
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20
[ruoyi] set theme style
Differences and usage scenarios between TCP and UDP