Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between SDS and C strings in Redis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces what is the difference between SDS and C string in Redis. It is very detailed and has certain reference value. Friends who are interested must finish it!

Instead of being represented by a "C string" at the bottom of the redis, the default string is represented by the "SDS abstract type" that you build.

C string

The data stored in the C string ends with an empty character. For example, for example, "redis" actually takes the form of'R','E','D','I', S',\ 0'.

SDS (dynamic string)

SDS is an abstract type built by redis, which is mainly used to store the default string representation of redis, the AOF buffer in the AOF module, and the client state input buffer.

The contents of SDS abstract types are:

Int len, used to record the length of a string

Int free, used to record the number of unused bytes in the buf array

Char buf [], byte array is used to hold strings

The SDS structure is shown in the following figure

Difference

1. When calculating the length

The C string has to be traversed to know the length complexity of the string O (n).

SDS only needs to access the internal len attribute to achieve time complexity O (1).

2. Buffer overflow problem

The C string sets S1 as "redis", but at the bottom there is a neighboring S2 as "abc". Then here if S1 is concatenated with S3 "ccc" through the function strcat, and then the final result should be "redisccc", but if S1 is not set up with enough memory, it will cause the adjacent S2 to be modified.

SDS here will first expand the space to a sufficient location according to whether there is enough space, and will add more unused space in free with len length, such as the length of the redis string is 5, and then the space will be pre-allocated with the same length of 5, and finally the actual space length is free + len + 1 is 10.

3. String memory allocation

C string, when adding or decreasing data to a string, memory is re-requested, but if too many requests are bound to lead to performance degradation, change N times and allocate N times.

SDS uses two mechanisms for inert space release and space pre-allocation

Space pre-allocation:

Space pre-allocation: when we make a space allocation, we will allocate more space of len length on the original basis.

Here, when the SDS length is less than 1m, it is free = len. For example, if the SDS length is 6byte, the actual space is 6byte + 6byte + 1byte.

Only 1m more will be allocated when it is greater than 1m. Free = 1m, for example, if the length of SDS is 60m, the actual space is 60m + 1m + 1byte.

Release of inert space

When we reduce a string, the program does not immediately use memory reallocation to recover the shortened bytes, but records it through free for subsequent use. SDS also provides the corresponding API to prevent inert space from causing memory waste.

4. Binary security

The c string ends with a null character, but if some special data requires a null character, it can cause the data not to be saved and cause the data before the first null character to be recognized in advance. SDS ensures that the data is correct because SDS identifies strings based on their length.

5. Compatible with some functions in C language, because SDS also follows C's ending with null characters, it can use some functions in C.

Summary

The above is all the content of the article "what is the difference between SDS and C strings in Redis". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report