当前位置:网站首页>Redis source code and design analysis -- 7. Quick list
Redis source code and design analysis -- 7. Quick list
2022-07-23 10:49:00 【JunesFour】
Redis Quick list
List of articles
1. Introduce
Previously, we introduced the linked list structure and the compressed list structure , They are the underlying implementation of list keys , But the additional space of the linked list is a little high , because prev and next The pointer will take up part of the space (64 Bit system occupancy 8 + 8 = 16 byte ). And each node of the linked list allocates memory separately , Will increase the fragmentation of memory .
So in redis-3.2 Version start ,Redis Use quicklist As the underlying implementation of the list key .
2. quicklist Realization
quicklist It's actually zipList and linkedList Mixture , It is the zipList Put it in linkedList In each node of , Achieve compact storage .

2.1 quicklist Head structure
typedef struct quicklist {
// Point to the head ( Leftmost )quicklist Pointer to node
quicklistNode *head;
// Point to the tail ( Far right )quicklist Pointer to node
quicklistNode *tail;
// ziplist Node counter
unsigned long count;
// quicklist Node counter
unsigned int len;
// preservation ziplist Size , Profile settings , Occupy 16bits
int fill : 16;
// Node compression , Profile settings , Occupy 16bits,0 Means no compression
unsigned int compress : 16;
} quicklist;
among fill and compress Properties are configurable , stay redis.conf In file .
fillThe configuration corresponding to the property :list-max-ziplist-size -2- When the number is negative , Means :
-1Represent each quicklistNode Node ziplist The byte size cannot exceed4kb.-2Represent each quicklistNode Node ziplist The byte size cannot exceed8kb( Default ).-3Represent each quicklistNode Node ziplist The byte size cannot exceed16kb.-4Represent each quicklistNode Node ziplist The byte size cannot exceed32kb.-5Represent each quicklistNode Node ziplist The byte size cannot exceed64kb.
- When the number is positive , Means :
- Express ziplist The structure can contain
entryMaximum number of , The maximum value is2 ^ 15.
- Express ziplist The structure can contain
compressThe configuration corresponding to the property :list-compress-depth 00Means no compression ( Default ).1Express quicklist At both ends of the list1Nodes are not compressed , Middle node compression .2Express quicklist At both ends of the list2Nodes are not compressed , Middle node compression .3Express quicklist At both ends of the list3Nodes are not compressed , Middle node compression .- The maximum value is
2 ^ 16.
- When the number is negative , Means :
2.2 quicklist The node structure
typedef struct quicklistNode {
struct quicklistNode *prev; // Precursor node pointer
struct quicklistNode *next; // Subsequent node pointer
// When the compressed data parameter is not set, it points to a ziplist structure , Set the compressed data parameter to quicklistLZF structure
unsigned char *zl;
// Compressed list ziplist The total length of
unsigned int sz;
// ziplist Number of nodes in , Occupy 16 bits length
unsigned int count : 16;
// Indicates whether to use LZF Compression algorithm compression quicklist node ,1 It means not to use ,2 Said the , Occupy 2 bits length
unsigned int encoding : 2;
// It means a quicklistNode Whether nodes adopt ziplist Structure holds data ,2 Said the ,1 It means not to use , The default is 2, Occupy 2bits length
unsigned int container : 2;
// Mark quicklist Whether it has been compressed , Occupy 1bit length
// If recompress by 1, Wait to be compressed again
unsigned int recompress : 1;
// Used during testing
unsigned int attempted_compress : 1;
// Extra extension bits , Occupy 10bits length
unsigned int extra : 10;
} quicklistNode;
2.3 Compressed ziplist
typedef struct quicklistLZF {
// Said by LZF After the algorithm compresses ziplist Size
unsigned int sz;
// Save the compressed ziplist Array of , Flexible array
char compressed[];
} quicklistLZF;
2.4 quicklistEntry
// management quicklist in quicklistNode In nodes ziplist The structure of information
typedef struct quicklistEntry {
// Point to the quicklist The pointer to
const quicklist *quicklist;
// Point to the quicklistNode Pointer to node
quicklistNode *node;
// Point to the present ziplist Pointer to structure
unsigned char *zi;
// Point to the present ziplist String of structure vlaue member
unsigned char *value;
// Point to the present ziplist Integer of structure value member
long long longval;
// Save the current ziplist The number and size of bytes of the structure
unsigned int sz;
// Save relative ziplist The offset
int offset;
} quicklistEntry;
3. Common operations
3.1 Insert
quicklist You can choose to insert in the head or tail , Corresponding API yes quicklistPushHead and quicklistPushTail:
int quicklistPushHead(quicklist *quicklist, void *value, size_t sz) {
// Backup header node address
quicklistNode *orig_head = quicklist->head;
// If ziplist You can insert entry node
if (likely(
_quicklistNodeAllowInsert(quicklist->head, quicklist->fill, sz))) {
quicklist->head->zl =
// The nodes push To the head
ziplistPush(quicklist->head->zl, value, sz, ZIPLIST_HEAD);
// to update quicklistNode Record ziplist The size of sz
quicklistNodeUpdateSz(quicklist->head);
} else {
// If you cannot insert entry Node to ziplist
// Create a new quicklistNode node
quicklistNode *node = quicklistCreateNode();
// take entry node push To the newly created quicklistNode In nodes
node->zl = ziplistPush(ziplistNew(), value, sz, ZIPLIST_HEAD);
// to update ziplist Size sz
quicklistNodeUpdateSz(node);
// Insert the newly created node in front of the head node
_quicklistInsertNodeBefore(quicklist, quicklist->head, node);
}
// to update quicklistNode Counter
quicklist->count++;
// to update entry Counter
quicklist->head->count++;
// If you change the header node pointer, it returns 1, Otherwise return to 0
return (orig_head != quicklist->head);
}
int quicklistPushTail(quicklist *quicklist, void *value, size_t sz) {
quicklistNode *orig_tail = quicklist->tail;
// If ziplist You can insert entry node
if (likely(
_quicklistNodeAllowInsert(quicklist->tail, quicklist->fill, sz))) {
quicklist->tail->zl =
// The nodes push To the tail
ziplistPush(quicklist->tail->zl, value, sz, ZIPLIST_TAIL);
// to update quicklistNode Record ziplist The size of sz
quicklistNodeUpdateSz(quicklist->tail);
} else {
// Create a new quicklistNode node
quicklistNode *node = quicklistCreateNode();
// take entry node push To the newly created quicklistNode In nodes
node->zl = ziplistPush(ziplistNew(), value, sz, ZIPLIST_TAIL);
// to update ziplist Size sz
quicklistNodeUpdateSz(node);
// Insert the newly created node after the tail node
_quicklistInsertNodeAfter(quicklist, quicklist->tail, node);
}
// to update quicklistNode Counter
quicklist->count++;
// to update entry Counter
quicklist->tail->count++;
// If you change the tail node pointer, it returns 1, Otherwise return to 0
return (orig_tail != quicklist->tail);
}
Reference material :
《Redis Design and implementation 》
边栏推荐
- C EventHandler observer mode
- Redis源码与设计剖析 -- 5.整数集合
- C语言基础知识梳理(一)
- Selenium JD crawler
- Interest rate in installment payment
- MySQL index operation
- openvino_ datawhale
- JMeter record the BeanShell written into excel instance caused by an automatic data generation
- 美团8年经验之谈,测试工程师如何进阶(自动化、性能、测开)
- Chapter 4: runtime data area - shared space
猜你喜欢

China Economic Net: "Yuan universe" is hot

Redis源码与设计剖析 -- 14.数据库实现

Error in na.fail.default(list(Purchase = c(“CH“, “CH“, “CH“, “MM“, “CH“, : missing values in obj

Question 300 Leçon 6 type quadratique

Ultra Fast Deep Lane Detection with Hybrid Anchor Driven Ordinal Classification论文解读

Redis源码与设计剖析 -- 9.字符串对象

李宏毅机器学习2022-HW1

Comprehensive experiment of realizing private network interworking under mGRE environment

云徙科技CTO李楠:技术和技术人,与数字化共同进化

The topic pub instruction of ros2 appears: failed to populate field: 'vector3' object has no attribute 'x:1' error
随机推荐
MGRE环境下实现私网互通综合实验
Redis源码与设计剖析 -- 10.列表对象
Add trust list
C# EventHandler观察者模式
XSSGAME小游戏(XSS学习)level1-15
300 题 第六讲 二次型
【视觉SLAM】ORB-SLAM: Tracking and Mapping Recognizable Features
软件测试基本概念篇
When flutter runs flutter pub get, it reports an error: "the client does not have the required privileges“
【Warning】YOLOV5训练时的ignoring corrupt image/label: [Errno 2].....,无法全部训练数据集,快速带你解决它
Redis源码与设计剖析 -- 8.对象系统
PMP每日一练 | 考试不迷路-7.22
C language explanation series - understanding of functions (1) library functions, user-defined functions
SVG, canvas, drawing line segments and filling polygon, rectangle, curve drawing and filling
Chapter 3 Standard Input
openvino_datawhale
mysql的索引的操作
美团8年经验之谈,测试工程师如何进阶(自动化、性能、测开)
【ROS进阶篇】第八讲 URDF文件的语法详解
交换机Exchanges