Redis learning notes (V) compressed list

Time:2021-9-26

Compressed list is one of the underlying implementations of list key and hash key. When a list key contains only a small number of list items, and each list item is either a small integer value or a short string, redis will use the compressed list as the underlying implementation of the list key.

Compressed list is developed to save memory. It is a sequential data structure composed of a series of special coded continuous memory blocks. A compressed list can contain any number of nodes, and each node can hold a byte array or an integer value.

A compressed table can contain:

1. Byte array with length less than or equal to 63 bytes

2. Byte array with length less than 16383 bytes

3. Byte array with length less than or equal to 4294967295 bytes

4. 4-bit unsigned integer

5. Signed integer of 1 byte length

6. Signed integer 3 bytes long

7. Integer of type int16

8. Integer of type int32

9. Integer of type Int64

 

Each compressed list node is represented by previous_ entry_ It consists of length, encoding and content

Description: Previous_ entry_ Length saves the length of the previous node. If the length of the previous node is less than 254 nodes, then previous_ entry_ The length attribute requires 1 byte of space to store the length value; If it exceeds 254, a space of 5 bytes is required to save this length.

 

Chain update

Because it is a continuous memory fragment, when an element is inserted in the middle,

 

Previous of E1 node_ entry_ The length attribute is only 1 byte long. When the new node is set as the front node, due to the previous of E1_ entry_ The length of the new node cannot be saved when the length of length is 1, so the length needs to be extended to 5 bytes, so the space of the list needs to be reallocated. Similarly, if E2 and E3 are triggered… This operation is called chain update.

In the worst case, chain update needs to reallocate the compressed list n times. The worst complexity of each space reallocation is O (n), so the worst complexity of chain update is O (n)2)。

 

——– end ——–

Learn a little every day, there will always be gains.

Note: respect the author’s intellectual property rights. Refer to redis design and Implementation for the content in this article. Just learn here and share with you.

 

 

Recommended Today

Seven Python code review tools recommended

althoughPythonLanguage is one of the most flexible development languages at present, but developers often abuse its flexibility and even violate relevant standards. So PythoncodeThe following common quality problems often occur: Some unused modules have been imported Function is missing arguments in various calls The appropriate format indentation is missing Missing appropriate spaces before and after […]