The basic data structure of Redis is a dynamic array
Adynamic array of C language
First look at the general dynamic array structure
struct MyData { int nlen; Char data[0];};
This is a common technique commonly used to form buffers. There are advantages to using an empty array compared to pointers:
1.不需要初始化,数组名直接就是所在的偏移
2.不占任何空间,指针需要占用
int
长度空间,空数组不占任何空间。
This array does not occupy any memory, meaning that such a structure saves space;
The memory address of the array is the same as the address of the element behind him, meaning there is no initialization, the array name is the address of the subsequent element and can be used directly as a pointer.
This is best for making dynamic buffer. Because you can allocate space like this: malloc (sizeof (struct XXX) + Buff_len); Does it look good? The structure of the buffer is directly allocated with a block of buffers. It is also very convenient to use, because now the empty array has actually become an array of buff_len lengths. The advantage is that one allocation solves the problem and saves a lot of trouble. It is known that in order to prevent memory leaks, if the second malloc fails, the first allocated struct must be rolled back if it is divided by two allocations (struct and buffer). This brings a coding problem.
Second, after assigning the second buffer, if the structure uses a pointer, the pointer is also assigned a value. Also, in the free buffer, use the pointer two times free. If an empty array is used, all problems are resolved at once.
Second, we know that the management of small memory is very difficult, if the pointer, the struct part of the buffer is a small memory, in the system is more and more will inevitably seriously affect the performance of memory management. There is no problem if the struct and the actual data buffer are allocated large chunks at once using an empty array. Thus, using an empty array simplifies coding and solves the problem of small memory fragmentation that improves performance, he le? should be widely adopted.
Ii.redis data type definition
SDS (Simple Dynamic Strings) is the most basic underlying data structure in Redis, which is the bottom-level implementation of the Redis String type and the cornerstone of implementing composite types such as Hash, List, and Set.
In addition, SDS is the type of string used by the Redis internal implementation.
//There are two data types related to SDS implementations and one is SDS://alias for string typetypedefChar*SDS;//the other is SDSHDR://structure of the holding SDSstructSDSHDR {intLen//number of string spaces that have been used in the BUF intFree//number of reserved string spaces in buf CharBuf[];//where the strings are actually stored};
There are two data types related to SDS implementations and one is SDS:
Alias for String type
typedef char *SDS;
The other is SDSHDR:
Structure of the holding SDS
struct SDSHDR
{
int Len; Number of string spaces that have been used in the BUF
int free; Number of reserved string spaces in buf
Char buf[]; Where the strings are actually stored
};
(2) The basic data structure of Redis is dynamic array