Redis source code analysis (1) -- Redis Data Structure-string SDS, redissds

Source: Internet
Author: User

Redis source code analysis (1) -- Redis Data Structure-string SDS, redissds

1. SDS Overview
  • The strings used in Redis are Simple Dynamic strings (SDS.
  • SDS is packaged Based on the C string to make it more suitable for Redis use scenarios.
  • In Redis, the C string is used only in some places that do not need to be modified, such as log printing; other places where Strings need to be used basically use SDS.
2. Data Structure
struct sdshdr{  int len;  int free;  char buf[];};
  • len: The actual usage of strings in the buf array.
  • free: Idle amount in the buf array.
  • buf: An array of characters.
3. Advantages of SDS

Redis uses the C language, while Redis does not use the C language string. The SDS string in Redis has the following advantages over the C string.

3.1 high efficiency in obtaining string lengths

C-language strings do not record the length of a string. Therefore, each time you obtain the length of a string, you must traverse the character array. the time complexity is O (n ).
In SDS, len is used to record the length of the current string. Therefore, the time complexity of String Length statistics is O (1), which is higher than that of string C.

3.2 avoid Buffer Overflow 3.2.1 what is "buffer overflow 』?

When usingstrcat(char *dest, char *src)When splicing two strings,strcatBy default, there is sufficient space behind the first character array. It will directly copy the characters in the second character array one by one to the back of the first character array.
The problem arises. If the memory space of the two character arrays is close to each other, the second character array will be overwritten when strcat is executed. This is the buffer overflow.
Therefore, before using strcat to splice two strings, you must first determine whether there is sufficient memory space behind the first string. If not, you must manually expand the size. Therefore, this series of judgment + resizing operations must be done by the programmer himself, which is somewhat troublesome.

3.2.2 how does Redis avoid buffer overflow?

All the APIS provided by SDS that modify strings will determine whether memory overflow will occur after the modification. If memory overflow occurs, it will help you resize the memory.
Therefore, for SDS, this series of operations are all done by it, without the need for programmers to manually determine.

3.3 reduce the number of times of memory reallocation when modifying strings 3.3.1 what is "memory reallocation 』?
  • When we use append to expand the string, we first need to expand the memory of the current character array, and then copy the values in the second character array one by one, otherwise, "buffer overflow" may occur 』. This process is "memory reallocation 』.
  • After intercepting a string, We need to release the memory space that is no longer in use. Otherwise, the memory may leak 』. This process is also "memory reallocation 』.

The memory redistribution process involves complex algorithms and system calls, which are time consuming. If the memory is re-allocated every time you modify a string like the C string, the efficiency is superb, therefore, SDS uses space pre-allocation and inert space release to reduce the redistribution frequency and improve efficiency.

3.3.2 how does SDS reduce the number of memory reallocation? 3.4 binary Security binary-safe3.4.1 what is "binary security 』?

The so-called "binary Security" refers to what data is stored in the SDS and what data is obtained. SDS does not modify, restrict, or filter the stored data.

3.4.2 how does SDS ensure binary security?

The C string has strict requirements on the stored strings:
1. A certain encoding (such as ASKII) must be met)
2. spaces are not allowed.

SDS has no restrictions on the stored data, so it is called "binary Security 』.

3.5 compatible with C strings

The C string requires that the end of the character array must be \ 0, marking the end of the string. The character array in SDS also complies with this specification, so it can still use C string-related functions, thus avoiding repeated code.

Top
1
Step on
0
View comments

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.