当前位置:网站首页>[redis]-[redis underlying data structure]-sds
[redis]-[redis underlying data structure]-sds
2022-06-21 07:51:00 【Pacifica_】
Preface
Redis No direct use C Language string , Instead, they built their own model called Simple dynamic string ( S i m p l e D y n a m i c S t r i n g Simple\ Dynamic\ String Simple Dynamic String,SDS)
Structure definition
struct sdshdr{
int len; // Record buf The number of bytes used in an array , That is, the length of the string saved by the variable
int free; // Record buf Number of unused bytes in the array
char buf[]; // The byte array is used to hold the actual contents of the string
};
SDS follow C The string is empty ‘0’ Convention ending with , But this character does not count in the string length , namely len In attributes . For the operation of this null character ,SDS The interfaces of have been encapsulated , This null character is transparent to the user ; The advantage of keeping this Convention is , You can use it directly C Some functions in the string library
SDS Characteristics
Constant complexity gets the length of the string
C The string itself does not record its length , Each time you get the length of a string, you need to traverse the entire string , The complexity is O(N);
and SDS Use a variable len Maintain the length of the string contents , Each time you get the length, you only need to access len The value of the property , The complexity is O(1)
The time complexity of the operation to obtain the length of the string is ensured from O(N) Reduced to O(1), Ensure that this operation is not a bottleneck affecting performance , Especially in the case of frequently obtaining string length
Buffer overflow is eliminated
because C The string does not record its own length , Easy to cause out of buffer The problem of . for example ,<String.h>/strcat function , When splicing one string to the end of another string , Because I don't know the length of the target string , Executing the function will default that the space occupied by the target string is enough to splice the spliced strings , Therefore, the spliced string will be directly connected to the subsequent space of the target string , This may cause the contents in the subsequent space of the original target string to be overwritten by the concatenated string , That is, there is a buffer overflow problem
and SDS Records the length of the string itself , When performing a similar splicing operation , First, it will judge whether the space occupied by the target string is enough to splice the spliced strings , If not enough , You will first expand the space size of the target string and then perform the splicing operation
Reduce the number of memory reallocation when modifying the string
A contain N A character C Strings always occupy N + 1 An array of characters , So increase or decrease one at a time C character string , Will perform a reallocation operation on the array of this string
Memory reallocation takes some time , If this operation does not occur frequently , It is acceptable to reallocate the memory every time you modify the string ; but Redis As a database , It is often used with strict speed requirements , Scenarios with frequent data modification , Each modification takes a certain amount of time to reallocate memory, which will have a certain impact on performance
SDS Used Unused space Methods , namely buf Arrays can contain unused bytes , The number of bytes is determined by free Attribute maintenance . adopt Unused space ,SDS Realized Space preallocation and Inert space release Two optimization strategies
Space preallocation
When it's necessary to SDS When you do spatial expansion , Not only will the program be SDS Allocate the necessary space for modification , Also for the SDS Allocate additional unused space :
- If the SDS After modification SDS The length of (len attribute ) take Less than 1 MB, So program allocation and len Property of the same size of unused space
- If the SDS After modification SDS The length of will be Greater than or equal to 1 MB, Then the program will assign 1 MB Of unused space
adopt Space preallocation Strategy , You can reduce the number of memory reallocations required for continuous string growth operations , Before we do that , Will first determine whether the unused space is sufficient , Use unused space directly if enough
Through this strategy ,SDS Will continue to grow N The number of memory reallocations required for the secondary string from Certainly N Time Reduced to most N Time
Inert space release
Lazy space release for optimization SDS String shortening operation : When you need to shorten SDS Save the string , The program does not immediately use memory reallocation to reclaim the extra bytes after shortening , But use free Property records the number of these bytes , And wait for future use
Through this strategy ,SDS It avoids the memory reallocation needed to shorten the string , And provides optimization for possible future growth operations : When you need to grow the string later , If the space required for growth is less than the size of unused space , Then you can directly perform the growth operation without memory reallocation to expand the space of the string
Binary security
C The characters in the string must match some encoding ; And besides the end of the string , The string cannot contain empty characters , Otherwise, the contents saved in the string will not be as expected . These properties make C String does not conform to the nature of binary security
and SDS Of API It's all binary safe ,API Will be handled in a binary way SDS Store in buf The contents of the array , There will be no restrictions on the data in it , Filter , Or suppose , What data looks like when it's written , What happens when it is read , There is nothing here “ Special characters ” The concept of , All characters are just themselves . in addition ,SDS Use len Property to determine whether the string ends , Instead of using empty characters , This avoids the impact of empty characters
With the features of binary security ,Redis Not only can text data be stored , It can also save binary data in any format
Compatible with the part C String function
SDS follow C The Convention of ending a string with a null character , This makes it possible to reuse some functions of the string library , Avoid unnecessary code duplication
边栏推荐
- mysql分页查询如何优化
- 2021-06-18 STM32F103 DMA and DMA serial port code using firmware library
- WordPress website security in 2022
- Yyds dry goods inventory junit5 learning 3: assertions class
- China inorganic fiber market trend report, technical innovation and market forecast
- An improvement of the code in the article "Walkthrough: creating paged data access using web form pages" in MSDN
- 卧槽,一行代码就可将网页直接转pdf保存下来(pdfkit)
- 文件下载 二进制流的形式构造url和base64下载
- What are the differences between SQL and MySQL
- A table to easily understand the prefix and suffix of increment and decrement operators
猜你喜欢

What is a multi domain SSL certificate?

24 parameter estimation interval estimation of two population parameters

Firefox users are down, Mozilla foundation is at a crossroads

2021-06-17 STM32F103 USART serial port code using firmware library

2021-06-18 STM32F103 DMA 与 DMA串口代码 使用固件库

You can't use typescript generics after reading it. Come to me for yyds dry inventory

25 parameter estimation - Determination of sample size

Detailed explanation of deep learning technology for building an image search engine that can find similar images

图解 Google V8 # 14:字节码(二):解释器是如何解释执行字节码的?

Blank screen of virtual machine browser
随机推荐
Learning Tai Chi maker esp8226 (IX) JSON data communication III
[untitled]
How to use lerna to manage multiple packages
Matlab 3D diagram (unconventional)
16 general measurement of data skewness and kurtosis
How to view the MySQL installation path
Rdkit | fragment decomposition of drug molecules
1005 Spell It Right (20 分)(测试点3)
1006 Sign In and Sign Out (25 分)
Illustration Google V8 14: bytecode (2): how does the interpreter interpret and execute bytecode?
Why do smart cities need digital twins?
Research Report on inorganic copper fungicide industry - market status analysis and development prospect forecast
A table to easily understand the prefix and suffix of increment and decrement operators
为什么智慧城市需要数字孪生?
In order to thoroughly understand the problem of garbled code, I dug up the history of the character set in a rage
RISC-V 的MMU
学习太极创客 — ESP8226 (九)JSON 数据通讯 三
Permission management
unity里现实摄像头运镜并LookAt到物体前方 基于Dotween
RPA(影刀)无需写代码抓取某东的商品信息