Redis study note 2 (Simple Dynamic string)

Source: Internet
Author: User

The basic data structure of redis is a dynamic array.

I,C language Dynamic Array

First look at the general dynamic array structure

struct MyData {    int nLen;    char data[0];};  

This is a widely used and common technique used to form a buffer zone. Compared with pointers, null arrays have the following advantages:

1. Initialization is not required. The array name is directly the offset. 2. No space is occupied, and the pointer needs to be occupied int Length space. Empty arrays do not occupy any space.

This array does not occupy any memory, which means this structure saves space;

The memory address of the array is the same as the address of the element following it, which means no Initialization is required. The array name is the address of the element following it and can be directly used as a pointer.

 

This method is most suitable for creating dynamic buffer. Because space can be allocated as follows:
Malloc (sizeof (struct XXX) + buff_len );
Are there any advantages? The structure of the buffer and the buffer are directly allocated. It is also very convenient to use, because the empty array is actually an array of buff_len length.
The following benefits:
One allocation solves the problem, saving a lot of trouble. We all know that in order to prevent memory leakage, if it is divided into two distributions (struct and buffer), if the second malloc fails, you must roll back and release the first allocated struct. This causes coding trouble.

Second, after the second buffer is allocated, if a pointer is used in the structure, the pointer must be assigned a value. Similarly, when the buffer is free, the pointer is free twice. If an empty array is used, all problems are solved once.

Secondly, we know that it is very difficult to manage small memory. If a pointer is used, the struct part of the buffer is small memory, if there is more in the system, it will seriously affect the memory management performance. If you use an empty array to allocate large blocks of struct and the actual data buffer at one time, this problem will not occur.
 
In this case, using an empty array not only simplifies encoding, but also solves the problem of Small memory fragments, which improves performance. Why not? It should be widely used.

 

II,Redis Data Type Definition

Simple Dynamic strings (SDS) is the most basic underlying data structure in redis. It is not only the underlying implementation of redis's string type, but also the cornerstone of implementing hash, list, set, and other composite types.

In addition, SDS is also the string type used by the internal implementation of redis,

// There are two data types related to SDS implementation. One is SDS: // the alias typedef char * SDS of the string type; // The other is sdshdr: // hold the structure struct sdshdr {int Len; // The number of used string spaces in the Buf int free; // the number of reserved string spaces in the Buf char Buf []; // The place where the string is actually stored };

There are a lot of functions in SDS. C to process the sdshdr struct, which is not described here. For details, refer to the code.

 

However, SDS has two optimization policies: Space pre-allocation policy and inert space release policy.

1) Space pre-allocation policy

It is mainly used to reserve part of the space when allocating space. If the size is smaller than 1 Mz, the program allocation is the same as the reserved space.

2) Release of inert space

It is mainly used to assign the truncation position to '\ 0' instead of releasing space when truncating strings'

 

Here we can look at the redis Design and Implementation of the source code analysis, read the source code with the book to see it is easy to understand

Redis study note 2 (Simple Dynamic string)

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.