Redis源码分析: String(SDS)容量调整分析
整体思路:
1 惰性缩容。不释放空间,留给到期释放等机制释放。
2 加倍扩容。在需要空间达1M之前按新空间两倍分配空间,否则按新空间大小+1M分配。注意,1M=1024*1024*Char。Char可以是5bits/8bits/16bits/32bits/64bits
具体代码块:
惰性缩容:https://github.com/antirez/sds/blob/master/sds.c line374-390
/* Grow the sds to have the specified length. Bytes that were not part of
* the original length of the sds will be set to zero.
*
* if the specified length is smaller than the current length, no operation
* is performed. */
sds sdsgrowzero(sds s, size_t len) {
size_t curlen = sdslen(s); if (len <= curlen) return s;
s = sdsMakeRoomFor(s,len-curlen);
if (s == NULL) return NULL; /* Make sure added region doesn't contain garbage */
memset(s+curlen,,(len-curlen+)); /* also set trailing \0 byte */
sdssetlen(s, len);
return s;
}
加倍扩容: https://github.com/antirez/sds/blob/master/sds.c line204-220
/* Enlarge the free space at the end of the sds string so that the caller
* is sure that after calling this function can overwrite up to addlen
* bytes after the end of the string, plus one more byte for nul term.
*
* Note: this does not change the *length* of the sds string as returned
* by sdslen(), but only the free buffer space we have. */
sds sdsMakeRoomFor(sds s, size_t addlen) {
void *sh, *newsh;
size_t avail = sdsavail(s);
size_t len, newlen;
char type, oldtype = s[-] & SDS_TYPE_MASK;
int hdrlen;
/* Return ASAP if there is enough space left. */
if (avail >= addlen) return s;
len = sdslen(s);
sh = (char*)s-sdsHdrSize(oldtype);
newlen = (len+addlen);
if (newlen < SDS_MAX_PREALLOC)
newlen *= ;
else
newlen += SDS_MAX_PREALLOC;
其中:https://github.com/antirez/sds/blob/master/sds.h line36
#define SDS_MAX_PREALLOC (1024*1024)
源码见:
https://github.com/antirez/sds
Redis源码分析: String(SDS)容量调整分析的更多相关文章
- [Redis源码阅读]sds字符串实现
初衷 从开始工作就开始使用Redis,也有一段时间了,但都只是停留在使用阶段,没有往更深的角度探索,每次想读源码都止步在阅读书籍上,因为看完书很快又忘了,这次逼自己先读代码.因为个人觉得写作需要阅读文 ...
- Redis源码之String操作
0.前言 String操作是Redis操作中最基本的类型,包含get,set,mget,mset,append等等.下面我们会具体分析下一些命令的详细流程,特么简单的命令没有列出. 1.SET命令 2 ...
- Redis源码分析系列
0.前言 Redis目前热门NoSQL内存数据库,代码量不是很大,本系列是本人阅读Redis源码时记录的笔记,由于时间仓促和水平有限,文中难免会有错误之处,欢迎读者指出,共同学习进步,本文使用的Red ...
- Redis源码分析(intset)
源码版本:4.0.1 源码位置: intset.h:数据结构的定义 intset.c:创建.增删等操作实现 1. 整数集合简介 intset是Redis内存数据结构之一,和之前的 sds. skipl ...
- Redis源码分析(dict)
源码版本:redis-4.0.1 源码位置: dict.h:dictEntry.dictht.dict等数据结构定义. dict.c:创建.插入.查找等功能实现. 一.dict 简介 dict (di ...
- Redis 源码简洁剖析 02 - SDS 字符串
C 语言的字符串函数 C 语言 string 函数,在 C 语言中可以使用 char* 字符数组实现字符串,C 语言标准库 string.h 中也定义了多种字符串操作函数. 字符串使用广泛,需要满足: ...
- Redis源码阅读笔记(1)——简单动态字符串sds实现原理
首先,sds即simple dynamic string,redis实现这个的时候使用了一个技巧,并且C99将其收录为标准,即柔性数组成员(flexible array member),参考资料见这里 ...
- Redis源码分析:serverCron - redis源码笔记
[redis源码分析]http://blog.csdn.net/column/details/redis-source.html Redis源代码重要目录 dict.c:也是很重要的两个文件,主要 ...
- Redis源码阅读一:简单动态字符串SDS
源码阅读基于Redis4.0.9 SDS介绍 redis 127.0.0.1:6379> SET dbname redis OK redis 127.0.0.1:6379> GET dbn ...
- redis源码分析之事务Transaction(下)
接着上一篇,这篇文章分析一下redis事务操作中multi,exec,discard三个核心命令. 原文地址:http://www.jianshu.com/p/e22615586595 看本篇文章前需 ...
随机推荐
- 对RLC重排序窗口大小的一点讨论
在LTE协议栈的PDCP层和RLC层,都有一个重排序窗口(reordering window),主要用来保证数据的可靠传输,PDCP层的重排序窗口主要用于handover时保证数据的可靠传输,这里暂且 ...
- IPSec 传输模式下ESP报文的装包与拆包过程 - 择日而终的博客
一.IPsec简介 IPSec ( IP Security )是IETF(Internet Engineering Task Force,Internet工程任务组)的IPSec小组建立的一组IP安全 ...
- 2019DDCTF部分Writeup
-- re Windows Reverse1 通过DIE查壳发现存在upx,在linux上upx -d脱壳即可,拖入IDA,通过关键字符串找到关键函数: main函数中也没有什么,将输入的字符串带到s ...
- 第一章 感受mac之美-换一种方式用电脑,开启新历程
感谢关注我的读者一直以来的追随与信任.去年到今年以来大环境都不是很好.裁员,机构优化,工厂倒闭,公司破产,贸易战等消息传来,不少还是身边发生的.今年开年以来更是有病毒横行,天降蝗灾等灾害.愿大家都好好 ...
- DotNet Core 使用 StackExchange.Redis 简单封装和实现分布式锁
前言 公司的项目以前一直使用 CSRedis 这个类库来操作 Redis,最近增加了一些新功能,会存储一些比较大的数据,内测的时候发现其中有两台服务器会莫名的报错 Unexpected respons ...
- CSS 实现元素较宽不能被完全展示时将其隐藏
首发于本人的博客 varnull.cn 遇到一个需求,需要实现的样式是固定宽度的容器里一排显示若干个标签,数量不定,每个标签的长度也不定.当到了某个标签不能被完全展示下时则不显示.大致效果如下,标签只 ...
- PC端如何下载B站里面的视频?
此随笔只是记录一下: PC端下载B站的视频,在blibli前面加上一个i 然后在视频上鼠标右键,视频另存为+路径即可 PS:网上其他的方法,比如在blibli前面加上kan,后面加上jj等,这些方 ...
- nowcoder 135F 圆
链接:https://www.nowcoder.com/acm/contest/135/F来源:牛客网 圆(circle) 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 3276 ...
- 在centos7使用docker下搭建elasticsearch集群
一 .docker的安装 https://www.cnblogs.com/ghostdot/p/12410242.html 二.创建相关映射文件 cd /home/ mkdir node cd nod ...
- Redis面试题集锦(精选)
1.什么是 Redis?简述它的优缺点? Redis的全称是:Remote Dictionary.Server,本质上是一个Key-Value 类型的内存数据库,很像memcached,整个数据库统统 ...