Redis – Chapter 10 – linked list

Time:2021-3-6

catalog

1. Arrays and linked lists

Array:
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 isO(1). But if you add or delete data, it will move a lot of data, and the time complexity isO(n)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:
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 isO(1). But the time complexity of the query isO(n)

2. Linked list

2.1 two way linked list

Redis - Chapter 10 - 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.

2.3. QuickList

  • Official website introduction:
    A doubly linked list of ziplists
    A generic doubly linked quicklist implementation
  • Introduction:

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. Contrast

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.

3.3. QuickList

  • The tradeoff between space efficiency and time efficiency.
  • It combines the advantages of double ended list and compressed list.

4. Summary

stayredis 3.2The previous version usedBidirectional 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 listCompressed linked listAt the right time, the opportunity turns intoTwo way linked listredis 3.2And then we useQuickList linked list

Recommended Today

Third party calls wechat payment interface

Step one: preparation 1. Wechat payment interface can only be called if the developer qualification has been authenticated on wechat open platform, so the first thing is to authenticate. It’s very simple, but wechat will charge 300 yuan for audit 2. Set payment directory Login wechat payment merchant platform( pay.weixin.qq . com) — > Product […]