当前位置:网站首页>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
边栏推荐
- Is it safe to open an account on the official website of Huatai Securities?
- NVIDIA Jetson test installation yolox process record
- Course of causality, taught by Jonas Peters, University of Copenhagen
- 丸子官网小程序配置教程来了(附详细步骤)
- 【obs】Impossible to find entrance point CreateDirect3D11DeviceFromDXGIDevice
- SDNU_ACM_ICPC_2022_Summer_Practice(1~2)
- 【obs】Impossible to find entrance point CreateDirect3D11DeviceFromDXGIDevice
- 炒股开户怎么最方便,手机上开户安全吗
- 基于微信小程序开发的我最在行的小游戏
- Stock account opening is free of charge. Is it safe to open an account on your mobile phone
猜你喜欢
RPA cloud computer, let RPA out of the box with unlimited computing power?
Qt不同类之间建立信号槽,并传递参数
SDNU_ ACM_ ICPC_ 2022_ Summer_ Practice(1~2)
【obs】官方是配置USE_GPU_PRIORITY 效果为TRUE的
Kubernetes Static Pod (静态Pod)
jemter分布式
QT adds resource files, adds icons for qaction, establishes signal slot functions, and implements
Codeforces Round #804 (Div. 2)(A~D)
v-for遍历元素样式失效
国外众测之密码找回漏洞
随机推荐
Reentrantlock fair lock source code Chapter 0
【测试面试题】页面很卡的原因分析及解决方案
NVIDIA Jetson test installation yolox process record
ABAP ALV LVC模板
基于微信小程序开发的我最在行的小游戏
测试流程不完善,又遇到不积极的开发怎么办?
Qt不同类之间建立信号槽,并传递参数
8道经典C语言指针笔试题解析
How to add automatic sorting titles in typora software?
玩轉Sonar
ReentrantLock 公平锁源码 第0篇
5g NR system messages
Is it safe to speculate in stocks on mobile phones?
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
AI遮天传 ML-初识决策树
大二级分类产品页权重低,不收录怎么办?
C language 001: download, install, create the first C project and execute the first C language program of CodeBlocks
1293_FreeRTOS中xTaskResumeAll()接口的实现分析
SDNU_ ACM_ ICPC_ 2022_ Summer_ Practice(1~2)
搭建ADG过程中复制报错 RMAN-03009 ORA-03113