首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >为什么redis中hashmap的负载因子高达5

为什么redis中hashmap的负载因子高达5
EN

Stack Overflow用户
提问于 2020-01-02 17:23:04
回答 2查看 230关注 0票数 3

在算法类和授权书籍中,负载因子小于1,因为Java的默认值是0.75。但是在redis源代码中,负载因子是5。

代码语言:javascript
运行
AI代码解释
复制
54 /* Using dictEnableResize() / dictDisableResize() we make possible to
55  * enable/disable resizing of the hash table as needed. This is very important
56  * for Redis, as we use copy-on-write and don't want to move too much memory
57  * around when there is a child performing saving operations.
58  *
59  * Note that even when dict_can_resize is set to 0, not all resizes are
60  * prevented: a hash table is still allowed to grow if the ratio between
61  * the number of elements and the buckets > dict_force_resize_ratio. */
62 static int dict_can_resize = 1;
63 static unsigned int dict_force_resize_ratio = 5;

为什么会这样呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-02 18:04:50

开始重新散列的负载因子是~1。dict_force_resize_ratio值是一种安全措施,因此即使禁用了重新散列,一旦达到该负载因子,它也会强制重新散列。

您可以在dict.c_dictExpandIfNeeded(dict *d)中看到这一点

代码语言:javascript
运行
AI代码解释
复制
/* If we reached the 1:1 ratio, and we are allowed to resize the hash
 * table (global setting) or we should avoid it but the ratio between
 * elements/buckets is over the "safe" threshold, we resize doubling
 * the number of buckets. */
if (d->ht[0].used >= d->ht[0].size &&
    (dict_can_resize ||
     d->ht[0].used/d->ht[0].size > dict_force_resize_ratio))
{
    return dictExpand(d, d->ht[0].used*2);
}

Redis允许~1开始重新散列,因为重新散列不是一下子完成的。它是通过维护两个哈希表逐步完成的。

请参阅dict.h

代码语言:javascript
运行
AI代码解释
复制
/* This is our hash table structure. Every dictionary has two of this as we
 * implement incremental rehashing, for the old to the new table. */
typedef struct dictht {
    dictEntry **table;
    unsigned long size;
    unsigned long sizemask;
    unsigned long used;
} dictht;

typedef struct dict {
    dictType *type;
    void *privdata;
    dictht ht[2];
    long rehashidx; /* rehashing not in progress if rehashidx == -1 */
    unsigned long iterators; /* number of iterators currently running */
} dict;

dict.c

代码语言:javascript
运行
AI代码解释
复制
/* Performs N steps of incremental rehashing. Returns 1 if there are still
 * keys to move from the old to the new hash table, otherwise 0 is returned.
 *
 * Note that a rehashing step consists in moving a bucket (that may have more
 * than one key as we use chaining) from the old to the new hash table, however
 * since part of the hash table may be composed of empty spaces, it is not
 * guaranteed that this function will rehash even a single bucket, since it
 * will visit at max N*10 empty buckets in total, otherwise the amount of
 * work it does would be unbound and the function may block for a long time. */
int dictRehash(dict *d, int n) {...

对于activerehashing设置,在redis.conf中还有一些额外的见解。

代码语言:javascript
运行
AI代码解释
复制
# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in
# order to help rehashing the main Redis hash table (the one mapping top-level
# keys to values). The hash table implementation Redis uses (see dict.c)
# performs a lazy rehashing: the more operation you run into a hash table
# that is rehashing, the more rehashing "steps" are performed, so if the
# server is idle the rehashing is never complete and some more memory is used
# by the hash table.
#
# The default is to use this millisecond 10 times every second in order to
# actively rehash the main dictionaries, freeing memory when possible.
#
# If unsure:
# use "activerehashing no" if you have hard latency requirements and it is
# not a good thing in your environment that Redis can reply from time to time
# to queries with 2 milliseconds delay.
#
# use "activerehashing yes" if you don't have such hard requirements but
# want to free memory asap when possible.
activerehashing yes
票数 4
EN

Stack Overflow用户

发布于 2020-12-03 08:53:54

因为Redis中load_factor的计算公式是:ht[0].used/d->ht[0].sizeused表示哈希表中元素的个数,size表示底层数组的大小,所以如果发生哈希冲突,元素会存储在链表中,链表的大小不包含在ht[0].size中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59567447

复制
相关文章
react native使用WebView加载本地html部署方案
前言 最近自己编写的react native安卓程序准备部署一下,发现调用的webview是本地的html文件,即url的格式是: http://localhost:8081/..这样的, 所以打包之
十里桃花舞丶
2018/05/17
2.9K0
爬虫利器:jsDOM
需求:之前使用node做爬虫的时候,使用puppeteer来模拟浏览器 然后抓取信息 但是这样的效率和消耗太大了, 所以需要一种更为效率的方法:直接使用axios来请求对应的url 然后通过jsDom,渲染成一个虚拟的html然后进行取值 废话不多说直接上代码: 先安装jsdom npm i jsdom 然后写下面实例化 const jsdom = require("jsdom"); const { JSDOM } = jsdom; const dom = new JSDOM(`<!DOCTYPE html
biaoblog.cn 个人博客
2022/08/11
3130
Javascript文件加载:LABjs和RequireJS
传统上,加载Javascript文件都是使用<script>标签。 就像下面这样:   <script type="text/javascript" src="example.js"></scrip
ruanyf
2018/04/12
1.4K0
Javascript文件加载:LABjs和RequireJS
JavaScript 动态加载脚本和样式
3大点: 1.元素位置 2.动态脚本 3.动态样式 一.元素位置 getBoundingClientRect()。这个方法返回一个矩形对象,包含四个属性:left、top、right和bottom。分别表示元素各边与页面上边和左边的距离。 var box = document.getElementById('box');//获取元素 alert(box.getBoundingClientRect().top);//元素上边距离页面上边的距离 alert(box.getBoundingClientRect()
汤高
2018/01/11
1.4K0
Javascript文件加载 ——LABjs和RequireJS
<script>标签很方便,只要加入网页,浏览器就会读取并运行。但是,它存在一些严重的缺陷。
javascript.shop
2019/09/04
1K0
Javascript文件加载 ——LABjs和RequireJS
iOS加载本地HTML、pdf、doc、excel文件 & HTML字符串与富文本互转
如果你有需求在手机端打开本地html的需求,又觉得使用其他方法麻烦或者不管用的时候,推荐你可以自己写个简单的app进行打开。
公众号iOS逆向
2021/07/05
2K0
HTML加载顺序
完成了若干个基于WEB的项目, 也了解了从前端的js,css,html到后端python/php等, 二者如何交互, 最终浏览器如何执行, 这些在心里也已经很明确了. 不过一个问题一直萦绕在心中,那就是:
用户7657330
2020/08/14
1.9K0
WebGL加载本地模型
大部分的webgl框架,比如threejs和babylon等,都可以加载obj和gltf模型。 我们的引擎,基于three封装,同样有加载模型的loader,因此加载obj和gltf模型也是很简单就可以实现的。
用户3158888
2022/05/11
1.9K0
WebGL加载本地模型
【JS 逆向百例】网洛者反爬练习平台第六题:JS 加密,环境模拟检测
本文章中所有内容仅供学习交流,抓包内容、敏感网址、数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,若有侵权,请联系我立即删除!
K哥爬虫
2021/12/28
8030
【JS 逆向百例】网洛者反爬练习平台第六题:JS 加密,环境模拟检测
javascript本地分页
注意:本地分页适用于数据量小的地方,如果数据量大,不建议使用本地分页 var iTable = document.getElementById("iTable"); var rows = iTable.rows.length; var pageSize = 3;//每页显示条数 var pageNum = 0;//总页数 var current = 1; if(rows/pageSize > parseInt(rows/pageSize)){ pageNum = parseInt(rows/pa
Petrochor
2022/06/07
3400
HTML中传递和引用JavaScript变量
http://ivantian2008.blog.51cto.com/622133/1127456
明哥的运维笔记
2019/01/30
5.6K0
WebView浏览本地html
index.html放在Assets文件夹下,供WebChromeClient调用
提莫队长
2019/02/21
1.4K0
Gravatar 头像无法加载
由于众所周知的原因,头像服务器多数都无法正常访问,不管是http还是https。为了解决头像问题有两个办法:
obaby
2023/02/22
1.6K0
JavaScript HTML DOM - 改变 HTML
今天的日期是: Mon Jul 19 2021 15:52:44 GMT+0800 (中国标准时间)
陈不成i
2021/07/20
4.3K0
Html5 学习系列(六)Html5本地存储和本地数据库
本文介绍了如何使用HTML5中的WebSQL API来实现客户端数据库的访问,并通过一个具体的应用场景示例展示了如何使用该API来实现客户端的数据库操作。同时,本文还介绍了WebSQL API的常见用法以及其相关的兼容性情况。
老马
2018/01/05
2.4K0
Html5 学习系列(六)Html5本地存储和本地数据库
JavaScript加载速度
解决JS加载速度慢的问题 传统形式加载js文件 <script type="text/javascript" src="js调用地址"></script> 高速加载js文件 <script type="text/javascript"> /* 请不要删除这段代码,因为这段代码起到了加速JS加载作用 */ document.write("<scr"+"ipt src=\"你的js调用地址"></sc"+"ript>"); </script>
闲花手札
2021/08/24
7420
go : gin 加载html
本文介绍使用 LoadHTMLGlob() or LoadHTMLFiles() 加载html资源
IT工作者
2022/07/25
1.5K0
利用特殊协议加载本地文件, 绕过 HTML5 沙箱, 打开弹窗诸事
原文链接:https://www.brokenbrowser.com/abusing-of-protocols/ 原作者:Manuel Caballero 译:Holic (知道创宇404安全实验室) 在 10 月 25 日,研究员 @MSEdgeDev twitter 了一个链接,成功引起了我的注意,因为我点击那个链接的时候(在 Chrome 上),Windows 应用商店会自动打开。这对你来说也许不足为奇,但它足以让我感到惊讶。 在我印象中,Chrome 有这样一个健康的习惯,在打开外部程序之前询问用户
Seebug漏洞平台
2018/03/30
2.5K0
UILabel加载html文本
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/53163142
用户1451823
2018/09/13
3K0
点击加载更多

相似问题

如何用JSDOM加载本地JavaScript文件?

20

NPM和jsdom错误加载

13

无法加载脚本HTML和javascript

26

使用jsdom和nodeJS加载nodeJS

11

无法将本地jquery.js加载到node.js (NPM jsdom)

15
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文