Simple dynamic string SDS for REDIS data structures

Source: Internet
Author: User

Several concepts
The 1:key object database stores the key of a key-value pair, always a string object.
The 2:value object database stores the value of a key-value pair, which can be a string object, a list object, a hash object, a set object, and a sorted set object.
For example:
Set MSG "Hello World" Redis creates a new key-value pair in the database, both the key and the value are a string object, and the underlying implementation is a SDS object.
Rpush fruits "apple" "Banana" "cherry" Redis creates a new key-value pair in the database, the key is a string object, the underlying implementation is a SDS object, the value is a list object, and the list object contains three string objects. Implemented by three SDS objects.
3:sds (sample dynamic String)
Defined:
struct sdshdr{
Record the number of bytes used in the BUF array
equals the length of the string saved by SDS
int Len;
Record the number of unused bytes in the BUF array
int free;
byte array to hold the string
Char buf[];
}
4: The simple string representation of the C language does not meet the security, efficiency, and functionality requirements of redis for strings, so reference the SDS string
4.1 SDS string Gets the string length more efficiently, the time complexity is O (1), and the C string is O (n), guaranteeing that getting the length of the work does not become a performance bottleneck for Redis. (Updating SDS length is done automatically by SDS API at execution time);
4.2 Eliminate buffer Overflow S1 If you do not allocate enough memory space, the S1 data will overflow into the next adjacent S2 space, and when the SDS space is allocated, the API will first check if the SDS space meets the required requirements for the modification, if not satisfied, The API automatically expands the SDS space to the size required to perform the modification, and then performs the modification operation;
4.3 Reduce the number of memory reallocation caused by modifying a string
1) C string to perform an extended operation, if you forget to reallocate memory buffer overflow, if you do a short operation, if you forget to reallocate memory memory leak, because Redis is a database, the speed is very demanding, if the data changes frequently, using C string requires frequent memory allocation, Performing memory allocations takes up most of the time of the modified string, and if it happens frequently, it also affects performance, so Redis uses the SDS string as the underlying implementation.
2) SDS string BUF array length is not necessarily equal to the number of characters plus one, the array contains unused bytes, this length is stored on the free property
3) Space pre-allocation: Optimize the SDS string growth operation, when the API expands the SDS, the program will not only modify the required space for SDS, but also allocate an additional unused space formula for the SDS: less than 1m after the expansion, the allocation and the Len attribute the same unused space, The unused space is allocated 1m greater than or equal to 1m. This allows the API to check if the unused space is sufficient before performing the scaling operation, and to reduce the memory allocation number if it is sufficient to use unused space directly, and based on this policy, the number of re-allocated memory is reduced from N to n times when the N-Modify operation is performed.
4) Lazy Space release: Optimize the string to shorten the operation, the program will not immediately reallocate memory recovery after shortening the extra bytes, but use the free property to record, waiting for future use, if there is a growth operation can be used directly. Of course, SDS also provides an API to release SDS unused space, so there's no need to worry about memory waste caused by lazy releases.
4.4 Binary Security C string must conform to a certain encoding, out of the end of the string, cannot contain null characters, so the C string can only save text data, can not store pictures, audio, video and other binary data, SDS API will be binary processing of the data in the BUF array. Enables Redis to save binary in any format.
4.5 compatible part of the C function SDS follows the convention of NULL character endings, the API will set the end of the SDS save data to null characters, and will allocate a space when the BUF allocates more than one byte, so that the SDS to save text data reuse some functions defined by the String.h library.

Summary:
C string Gets the length complexity O (n), SDS is O (1)
API insecurity can cause buffer overflow, API security does not cause buffer overflow
Modify string length n times to execute n memory allocations, up to n times
Only text data can be saved, text or binary data may be saved
All String.h library functions can be used, only partial functions are used

Simple dynamic string SDS for REDIS data structures

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.