- Arrays and linked lists
- Linked list
1. Arrays and linked lists
Array will open up a continuous space in memory to store data, which has both advantages and disadvantages. When obtaining data, the corresponding elements can be obtained directly through the subscript value, and the time complexity is
O(1). But if you add or delete data, it will move a lot of data, and the time complexity is
The expansion mechanism of array is as follows:If the array space is insufficient, a new space address will be created first, and the original array will be copied into the new array.
Linked list does not need to open up continuous memory space, it connects all the data through the pointer. When adding or deleting, you only need to modify the address pointed by the pointer. The time complexity is
O(1). But the time complexity of the query is
2. Linked list
2.1 two way linked list
Bidirectional linked list is the logical relationship between each node, which is bidirectional.
The nodes in the two-way linked list are composed of:
prior：To the front node of the current node,
data：Data stored in the current node.
next：A post node that points to the current node.
2.2. Compressed linked list
- Compressed linked list is developed to save memory.
- Ziplist is a special two-way linked list, which does not maintain the two-way pointer prev next. Instead, it stores the length of the previous entry and the current entry, and calculates where the next element is by the length.
- At the expense of reading performance, efficient storage space is obtained, because the storage pointer costs more memory than the storage entry length, which is a typical time for space.
- Official website introduction:
A doubly linked list of ziplists A generic doubly linked quicklist implementation
QuickList is a two-way linked list, and it is a two-way linked list of ziplist. Ziplist itself is a list that maintains the order of data items, and the data items are stored in a continuous memory block.
3.1 two way linked list
- Double ended linked list is easy to push and pop at both ends of the table, but its memory overhead is relatively large.
- In addition to the data to be saved, there are two additional pointers on each node of the double ended list.
- Each node of the double ended list is a separate memory block, the address is not continuous, more nodes are easy to produce memory fragmentation.
3.2 compressed list
- Ziplist is a continuous memory, so it has high storage efficiency.
- Ziplist is not conducive to modification operation, and every data change will cause a realloc of memory.
- When the length of ziplist is very long, a realloc may lead to a large number of data copies, further reducing the performance.
- The tradeoff between space efficiency and time efficiency.
- It combines the advantages of double ended list and compressed list.
redis 3.2The previous version used
Bidirectional linked list and compressed linked listTwo, because the bidirectional linked list takes up more memory than the compressed linked list, it will be created first when creating the linked list
Compressed linked listAt the right time, the opportunity turns into
Two way linked list。
redis 3.2And then we use
QuickList linked list。