In Redis
earlier versions, the storage list
time list structure, if the list of elements ranging from the use of compression ziplist
, otherwise use a doubly linked listlinkedlist
// list node struct listnode <T> { listnode * PREV; listnode * Next; T value; } listnode; // list struct {List listnode * head; // table pointer listNode * tail; // table tail pointer long len; // chain length } list;
For the list, we have the following characteristics:
-
Double Ended: with nodes
prev
andnext
pointers to get front, rear node -
Acyclic: header
prev
tail table andtail
directedNULL
-
With the tail pointer table header: Get Header table tail node complexity
O(1)
-
With chain length counter: obtaining a degree of complexity nodes
O(1)
-
Polymorphism: used
void*
to store the value of the node, different types of values can be stored
Quick List
Linked lists additional space is relatively high, because the 64bit
system is a pointer 8
bytes, prev
and the next
pointer must occupy 16
bytes, list node and separately allocated in memory, memory fragmentation exacerbated affect the efficiency of memory management.
Considering the above disadvantages linked list, Redis
subsequent versions of the transform list data structure, used quicklist
in place of ziplist
and linkedlist
.
// list of nodes quickly struct {quicklistNode quicklistNode * PREV; quicklistNode * Next; ziplist * ZL; // list of point compression int32 size; // ziplist total number of bytes int16 count; // ziplist the number of elements int2 encoding; // store form , or represents a primary array of bytes stored in a compressed LZF ... } quicklistNode; // quick list struct {QuickList quicklistNode * head; quicklistNode * Next; Long COUNT; // number of elements int nodes; // number ziplist node int compressDepth ; // LZF compression algorithm depth } QuickList;
As can be seen from the code,
quicklist
Internal default single ziplist
length 8k
byte, will exceed the number of bytes from a new ziplist
store.
In the quicklist
interior, to further save space, but also the use of LZF
algorithms to ziplist
compress storage.
By default, quicklist
the compression depth 0
i.e. no compression, the actual compression depth by a configuration list-compress-depth
decision.
To support fast pop/push
, quicklist
and last two ziplist
without compression, compression case depth 1
, depth 2
it means the first and second end to end ziplist