In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "why Redis is fast". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Redis is developed by C language, and C language has its own character types, but Redis does not directly use C language string types, but constructs its own abstract types of dynamic strings (SDS).
Just like a command like this, I actually created two SDS in Redis, one is Key SDS named aobing, the other is Value SDS named cool, even if it is a List of character type, it is Key and Value made up of a lot of SDS.
SDS is not only used as a string in Redis, but also used as a buffer (buffer), so everyone is still a little confused, C language string is not good, why use SDS? What does SDS look like? What are the advantages?
For this reason, I went to find the source code of Redis, and I can see that the result of the SDS value is about this. The source code is open source on GitHub.
Struct sdshdr {int len; int free; char buf [];}
Back to the original question, why did Redis use its newly developed SDS instead of C strings? All right, let's go see the difference.
The difference between SDS and C string
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
The counting methods are different.
The C language calculates the length of a string entirely from traversal, traversing from beginning to end, and then stops until an empty character is found, so that the length of the string is counted, so the time complexity of obtaining the length is 0 (n), something like this:
But this counting method leaves a hidden trouble, so Redis does not use C strings, which I will mention later.
And Redis I have shown you the structure above, he himself saved the length of the information, so we get the length of the time complexity of 0 (1), did we find a little bit of Redis fast details? It's not over. It's more than that.
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Eliminate buffer overflow
String concatenation is a common operation that we often do, and it is also a common operation in C and Redis, but here comes the problem. C does not record the length of strings. Once we call the concatenation function, if the memory is not calculated in advance, there will be a cache overflow.
For example, the original string looks like this:
You need to splice at the back now, but if you don't calculate the memory, the result may be like this:
Is this the result you want? Obviously, no, your result has been modified unexpectedly. If you put the system on the line, isn't it over? So how did Redis avoid this?
As we all know, his structure stores the current length, as well as the unused length of free. That's simple. Now that you have done the splicing operation, I will judge whether some of them can be put down. If the length is long enough, it will be executed directly. If not, then I will expand the capacity.
You can see the corresponding API in the Redis source code. Later, I will not post the source code one by one. If you are interested, you can see a wave by yourself. You need a little bit of C language foundation.
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Reduce the number of memory redistributions when modifying a string
The bottom layer of the C language string is also an array. Each time it is created, it creates a Number1-length character, and the extra 1 is to save the empty character. This empty character is also a pit, but it is not the content discussed in this link.
Redis is a cache database, if we need to frequently concatenate and truncate strings, and if we write code that forgets to reallocate memory, it may cause buffer overflows and memory leaks.
The memory allocation algorithm is time-consuming, not to mention whether you will forget to reallocate memory, even if you remember it all, such overhead should be avoided for a cached database.
In order to avoid defects such as C string, Redis adopts two solutions to maximize performance and space utilization:
Space pre-allocation: when we expand SDS, Redis allocates memory for SDS, and allocates extra free space and extra 1byte space according to a specific formula (this 1byte is also for storing empty characters), so that we can avoid the memory allocation consumption caused by continuous string addition.
For example, there is a character like this:
We call the concatenation function, the string side is long, and Redis will calculate a free value according to the algorithm to give him a spare:
As we continue to splice, you will find that the spare free is used, eliminating this memory reallocation:
Inert space release: it was just mentioned that extra space will be pre-allocated, and many friends will worry about memory leakage or waste. Don't worry, Redis boss also helped us think that when we finished performing a string reduction operation, redis will not immediately withdraw our space, because it can prevent you from continuing to add operations, which can reduce the consumption of allocated space. But when you do not use the extra space again, Redis will still reclaim the space to prevent the waste of memory.
It's the same string:
When we call the truncated function, the free space is not immediately released:
If we need to continue to add this space, we can use it, reducing the memory reallocation, and if the space is not needed, just delete it by calling the function:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Binary security
If you look at it carefully, you will see that I mentioned the null character more than once, that is, the '0blank character' C language judges the empty character to judge the length of a character, but there are many data structures that are often interspersed with empty characters in the middle, such as pictures, audio, video, binary data of compressed files, such as the following word, which can only recognize the preceding characters but not the latter characters. For us developers, the result is obviously not whether what we want is right or not.
Redis does not have this problem, he does not save the length of the string, he does not judge the empty character, he will judge whether the length is correct, so redis is often used by us to save all kinds of binary data, anyway, I use very high, often used to save the binary of small files.
This is the end of "Why Redis is Fast". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.