当前位置:网站首页>Redis 3.0源码分析-数据结构与对象 SDS LIST DICT
Redis 3.0源码分析-数据结构与对象 SDS LIST DICT
2022-07-28 18:40:00 【51CTO】
SDS
Redis定义的简单动态字符串(SDS)
从下面的测试中可以看出,该结构的大小为8字节。len指定buf数组中已使用字节的数量等于SDS所保存字符串的长度,不包括字符串末尾存放的\0,在分配内存时会为\0多分配一个内存单元,如sh = zmalloc(sizeof(struct sdshdr)+initlen+1);。

参数s指向sdshdr的buf,而s-(sizeof(struct sdshdr))指向sdshdr的首地址,因为sizeof(struct sdshdr)是8字节。

内存分配
内存分配主要在文件zmalloc.c和zmalloc.h中,在下面的系列中分析,这里开个头。
两种分配方式:zmalloc和zcalloc

zmalloc是由malloc函数来分配内存的

zfree释放内存

zrealloc

jemalloc
tcmalloc
tc_malloc_size
https://yq.aliyun.com/articles/6045
链表
存储在adlist.c中的,主要3种结构体:list、listIter、listNode。这些结构都在堆中使用zmalloc申请内存。

哈希表


哈希表节点使用dictEntry结构表示,每个dictEntry结构都保存着一个键值对

添加键值对


rehash
随着操作的不断执行,哈希表保存的键值对会逐渐地增多或者减少,为了让哈希表的负载因子(load factor)维持在一个合理的范围之内,当哈希表保存的键值对数量太多或者太少时,程序需要对哈希表执行rehash的步如下:
- 为字典的ht[1]哈希表分配空间,这个哈希表的空间大小取决于要执行的操作,以及ht[0]当前包含的键值对数量(也即是ht[0].used属性的值):如果执行的是扩展操作,那么ht[1]的大小为第一个大于等于ht[0].used*2的2n。如果执行的是收缩操作,那么ht[1]的大小为第一个大于等于ht[0].use的2n。
- 将保存在ht[0]中的所有键值对rehash到ht[1]上面:rehash指的是重新计算键的哈希值和索引值,然后将键值对放置到ht[1]哈希表的指定位置上。
- 将ht[0]包含的所有键值对都迁移到了ht[1]之后(ht[0]变为空表),释放ht[0],将ht[1]设置为ht[0],并在ht[1]新创建一个空白哈希表,为下一次rehash做准备。
边栏推荐
- 读取json配置文件,实现数据驱动测试
- CNN convolution neural network learning process (weight update)
- [detailed use of doccano data annotation]
- 根据openGauss/MogDB的lwtid查看线程堆栈。
- Teach you unity scene switching progress bar production hand in hand
- Yum package management
- Wust-ctf2021-re school match WP
- Use of DDR3 (axi4) in Xilinx vivado (3) module packaging
- TCP.IP
- Linxu [basic instructions]
猜你喜欢

太空射击第13课: 爆炸效果

One article makes you understand what typescript is

卡通js射击小游戏源码

同质化代币与 NFT 结合,如何使治理结构设计更灵活?

Other IPS cannot connect to the local redis problem solving and redis installation

C language data 3 (2)

[dynamic link library (DLL) initialization example program failed "problem]

Tree row expression

弹出模态框
![[C language] comprehensively analyze the pointer and sort out the pointer knowledge points](/img/71/f4138b68c27d6447fbd5b5e0762929.png)
[C language] comprehensively analyze the pointer and sort out the pointer knowledge points
随机推荐
Common instructions of vim software
Raspberry pie 4B deploy yolov5 Lite using ncnn
About IP address
MySQL startup error 1607 unexpected process termination
Shanghai Jiaotong University joined hands with Taobao to set up a media computing laboratory: promoting the development of key technologies such as video super score
Solve the cookie splitting problem (DP)
Simple example of C language 1
CNN convolutional neural network structure
LVS load balancing cluster
UE4.25 Slate源码解读
LVM logical volume
Teach you how to draw a map with ArcGIS [thermal map]
Raspberry pie 4B uses MNN to deploy yolov5 Lite
Durham High Lord (classic DP)
3D激光SLAM:LeGO-LOAM论文解读---简介部分
读取json配置文件,实现数据驱动测试
一碰撞就自燃,谁来关心电池安全?上汽通用有话说
太空射击第16课: 道具(Part 2)
LeetCode_位运算_中等_260.只出现一次的数字 III
File lookup and file permissions