[Redis study notes (1)] simple dynamic string SDS, linked list

This article is published by the official account [Developing Pigeon]! Welcome to follow! ! !


Old Rules-Sister Town House:

One. Simple dynamic string

(I. Overview

        Redis does not directly use the traditional string representation of the C language (character array ending with a null character), but instead constructs an abstract type called Simple Dynamic String (SDS) as the default string representation. In Redis, C string arrays are only used as string literals in places where there is no need to modify string values, such as printing logs. For modifiable string values, SDS is used to represent string values, such as key-value pairs that contain string values.


(2) Definition of SDS

The definition of SDS is as follows:

struct sdshdr{
    
    
	int len;
	int free;
	char buf[];
};

        The len attribute represents the length of the string stored in the SDS, that is, the number of bytes used in the buf array;

        The free attribute represents the number of unused bytes in the buf array;

        The buf attribute represents the byte array that holds the string;

        SDS still maintains the management of ending with a null character, because a part of the functions in the C string function library can be directly reused.

(3) The difference between SDS and C string array

1. Get the length of a string

        The C string array does not record its own length information, so in order to obtain the length, it is necessary to traverse the entire string until it encounters a null character. The complexity is O(N);

        The len attribute is saved in the SDS, and the length information of the SDS can be directly obtained, and the complexity is O(1)

2. Eliminate buffer overflow

        The C string array does not record the length, which can easily cause buffer overflow. Some string functions have allocated enough memory by default when adding characters to the string array. Once the assumption is not established, buffer overflow will occur;

        SDS does not have this situation. When the SDS API needs to modify the SDS, first check whether the SDS space meets the requirements for the modification. If it does not meet the requirements, the API will automatically expand the SDS space to perform the modification. the size of

3. Reduce the number of memory reallocations that modify strings

        For a C string array, the underlying implementation of an array containing N characters is always an N+1 character long array, so every time a C string is increased or shortened, the array must be re-allocated. ;

        For SDS, the two strategies of space pre-allocation and lazy space release are realized through unused space. For string growth operations, additional unused space will be allocated to the SDS. If the length of the SDS is less than 1MB, the same size of free space as the len attribute will be allocated, otherwise 1MB of free space will be allocated, which can reduce the continuous execution of string growth operations The number of memory reallocations required. For string shortening operations, the extra bytes after shortening will not be recovered immediately, but the free space will be used to record the number of these bytes and wait for future use, reducing the number of memory reallocations.

4. Binary Security

        The characters in the C string array must conform to a certain encoding, and apart from the end of the string, the string cannot contain empty characters, because the C string array can only store text data, not pictures, audio and other binary data;

        In order to make Redis applicable to various scenarios, the SDS APIs are binary safe, and the data stored in the buf array of the SDS will be processed in a binary manner. The buf array is used to store the binary data.


two. Linked list

(I. Overview

        The linked list provides efficient node rearrangement capabilities and sequential node access methods, and the length of the linked list can be flexibly adjusted by adding and deleting nodes. Redis is written in C language, and there is no linked list implementation in C language, so Redis has built its own linked list implementation. Linked lists are widely used in Redis. For example, one of the underlying implementations of list keys is linked lists. When a list key contains more elements, or the elements contained in the list are all relatively long strings, Redis does Will use the linked list as the underlying implementation of the list key.

(2) The realization of linked list and linked list nodes

        The linked list is made up of linked list nodes, and each linked list node is represented by the listNode structure:

typedef struct listNode{
    
    
	struct listNode *prev;
	struct listNode *next;
	void *value;
}listNode;

        Prev is the pointer of the pre-node, next is the pointer of the post-node, and value is the value of the node. Multiple listNodes can form a double-ended linked list through the front and back pointers.

The structure of the linked list is represented by list:

typedef struct list{
    
    
	listNode *head;
	listNode *tail;
	unsigned long len;
	void *(*dup)(void *ptr);
	void (*free)(void *ptr);
	int (*match)(void *ptr, void *key);
}list;

        Head pointer head, tail pointer tail, linked list length counter len, and the following node value copy function dup, node value release function free, node value comparison function match, note that these functions use void* pointers to save nodes Value, you can save a variety of different types of values.

Guess you like

Origin blog.csdn.net/Mrwxxxx/article/details/113874541