当前位置:网站首页>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做准备。
边栏推荐
猜你喜欢

Use of DDR3 (axi4) in Xilinx vivado (2) read write design

Storage of C language data in memory (1)

Linux Installation MySQL (pit filling version)

Unity package exe to read and write excel table files

Related concepts of multitasking programming

一文让你搞懂什么是TypeScript

Dsactf July re

PXE_ KS unattended system

Classes and objects (medium)

Scheduled backup of MySQL database under Windows system
随机推荐
产品力大幅提升 新款福特探险者发布
[pytorch] LSTM neural network
LVS deployment Dr cluster
How to make the design of governance structure more flexible when the homogenization token is combined with NFT?
Residual network RESNET source code analysis - pytoch version
Raspberry pie 4B parsing PWM
CM4 development cross compilation tool chain production
MySQL error: specified key was too long; max key length is 767 bytes
Use of DDR3 (axi4) in Xilinx vivado (2) read write design
Pytorch model parameter assignment - tensor.copy_ () method
[C language] 5000 word super detailed explanation of various operations of the sequence table
Raspberry pie 4B ffmpeg RTMP streaming
太空射击第11课: Sound and Music
Linxu [basic instructions]
Tree row expression
Practice of real-time push demo of three web messages: long polling, iframe and SSE
Voice controlled robot based on ROS (II): implementation of upper computer
leetcode:2141. 同时运行 N 台电脑的最长时间【最值考虑二分】
Commands related to obtaining administrator permissions
js win7透明桌面切换背景开始菜单js特效