当前位置:网站首页>Redis, do you understand the list
Redis, do you understand the list
2022-07-08 00:53:00 【InfoQ】
Preface
- We have learned before redis Of the five data structures list structure . Its interior is linkedList and zipList Two structures . This is what we have learned . I didn't check it in combination with the operation before . In fact, there is a combination of the two quickList
Structural evolution
- We added a key by zlist The data of . adopt object encoding zlist Viewing the bottom layer is through quicklist To build . Before that ziplist The chapter summarizes what we learned in redis in hash and list The basic data structures all use ziplist Storing data . stay list We do quicklist. Here we explain in advance quicklist Inside is based on ziplist To achieve .
linkedList
- At the beginning quicklist Before, let's simply sort out what we learned before linkedList , He is a commonDouble linked list. Complete the construction of our linked list through two pointers .
C++ The pointer
- redis It runs on memory , And memory is very valuable, so redis After designing the double-line linked list, I feel a little memory consumption . Because the pointer itself also needs to open up space . According to different systems, the pointer occupies different positions . Here I have summarized that a pointer placeholder is a basic bit of system operation
- What does the basic bit mean here ? Joining you is 64 A pointer to the bit system is 64 Bitwise is 8 Bytes . If you are 32 A pointer to the bit system is 32 Bitwise is 4 Bytes
- That is, if I were redis Store... In a medium to two-way linked list N English letters , We also know that a letter should account for 1 Bytes . So this N One element is N Of listNode . So keep N individual listNode In the middle 2*(N-1) A pointer to the . stay 64 In the bit system, that is, we need to open up nearly 129 Times more space to store content . In the above case, we only have N Bytes of content , But I need
2*(N-1)*8+N
Bytes to build listNode.
- With the increasing number of nodes, we waste more and more . therefore redis On the basis of two-way linked list, it combines ziplist Make improvements .
Transition reasons
ziplist
- stay ziplist In the chapter we know ziplist It's a piece of continuous memory , yes redis An improved structure of memory .ziplist High memory utilization is realized !
linkedlist+ziplist benefits
quicklist introduce
- quicklist Is in redis3.2 And then introduced , Here's what I'm using redis6.4 Convenient source code doesn't seem to have quicklist Source code .
- Later, after reading redis6.4 Seems to have cancelled quicklist . structure . So I made another special 3.2 Version of . What is specific here is redis3.2.4 edition !!!
The true face of Lushan Mountain
quicklist
- Through his source code, we can clearly see his internal data structure ! This should be familiar to you .quicklist It can be said that it was before us linkedList structure . The internal is a two-way linked list, but there are a little more attributes
- Through the diagram is not feeling and linkedList equally .
- Now let's see quicklist The meaning of each attribute in
quicklistNode
- quicklist It's just an abstract concept , What is really responsible for data storage is the composition quicklist Members of quicklistNode .
- The role of each attribute
- Through the above properties , We can also learn node The data structure in the node is ziplist . stay ziplist On this basis, it will be compressed to achieve higher memory efficiency !
- We don't need to know much about compression here ! The main purpose is a kind of coding , This kind of coding can't really be used during use redis Will decode . During the decoding operation, it is through recompress Property to mark .
insert
- In understanding quicklist After the basic structure, we are looking at insert What will happen to the structure ! We also mentioned above in redis.conf In profile
list-max-ziplist-size
Property is used to set quicklist In each node in the ziplist The size of the storage is set .
Insert both ends
- The first case is that the data we need to insert is at both ends . As shown in the figure above, we are redis.conf In the configuration file
list-max-ziplist-size: 2
. Represents the internal node ziplist in entry The maximum number is 2 . At this point we head Two contents have been stored in the header node ,tail The tail node stores 1 Nodes !
- At this time, if we want to add an element to the header, it is obj1 . It's conceivable that we can't join , This is the time redis Will recreate a ziplist Structure and contains obj1 , Will create a new ziplist After adding to the head of the linked list
- and obj2 When adding a tail node , Because the number of nodes at the tail node is 1 It hasn't reached its peak yet 2, So I just joined . The final result is as follows
Intermediate insertion
st=>start: Insert
ziplistInsert=>operation: towards ziplist Insert
subziplistInsert=>operation: Will be ziplist Split two ziplist, Add... At the corresponding position
insertNear=>operation: Insert adjacent ziplist in
newZipInsert=>operation: newly build ziplist Insert
cond=>condition: ziplist Whether it can accommodate
headtailCond=>condition: The insertion position is ziplist Both ends
nearheadtailCond=>condition: adjacent ziplist Whether it can accommodate
e=>end: a happy day
st->cond
cond(yes)->ziplistInsert
cond(no)->headtailCond(yes)->nearheadtailCond
headtailCond(no)->subziplistInsert
nearheadtailCond(yes)->insertNear
nearheadtailCond(no)->newZipInsert
summary
reference
边栏推荐
- 【愚公系列】2022年7月 Go教学课程 006-自动推导类型和输入输出
- Codeforces Round #804 (Div. 2)(A~D)
- 51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up
- German prime minister says Ukraine will not receive "NATO style" security guarantee
- Letcode43: string multiplication
- NVIDIA Jetson测试安装yolox过程记录
- Deep dive kotlin collaboration (the end of 23): sharedflow and stateflow
- LeetCode刷题
- Play sonar
- 手写一个模拟的ReentrantLock
猜你喜欢
C # generics and performance comparison
ReentrantLock 公平锁源码 第0篇
RPA cloud computer, let RPA out of the box with unlimited computing power?
Service Mesh介绍,Istio概述
玩转Sonar
3年经验,面试测试岗20K都拿不到了吗?这么坑?
语义分割模型库segmentation_models_pytorch的详细使用介绍
【obs】官方是配置USE_GPU_PRIORITY 效果为TRUE的
Reptile practice (VIII): reptile expression pack
8道经典C语言指针笔试题解析
随机推荐
Password recovery vulnerability of foreign public testing
Deep dive kotlin synergy (XXII): flow treatment
Langchao Yunxi distributed database tracing (II) -- source code analysis
Deep dive kotlin collaboration (the end of 23): sharedflow and stateflow
NVIDIA Jetson test installation yolox process record
8道经典C语言指针笔试题解析
QT establish signal slots between different classes and transfer parameters
【愚公系列】2022年7月 Go教学课程 006-自动推导类型和输入输出
手写一个模拟的ReentrantLock
德总理称乌不会获得“北约式”安全保障
哪个券商公司开户佣金低又安全,又靠谱
jemter分布式
How can CSDN indent the first line of a paragraph by 2 characters?
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
服务器防御DDOS的方法,杭州高防IP段103.219.39.x
Leetcode brush questions
Cancel the down arrow of the default style of select and set the default word of select
攻防世界Web进阶区unserialize3题解
Experience of autumn recruitment in 22 years
Class head up rate detection based on face recognition