众所周知,在哈希表中,负载因子对于控制冲突很重要。
In Java/HashMap, the default load factor is 0.75, And in CPython/dict, the load factor is set to 2 / 3
但是,在redis/dict中,是 1.0 (启用dict_can_resize时),为什么?
/* 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);
}
在我看来,负载因子应该小于1。由于可能的高冲突率,高负载因子可能会增加查找成本。
最佳答案
高负载因子也能提高内存效率。 Redis 是一个内存数据库,它需要内存高效。我认为 Redis 的作者已经做了一些基准测试来平衡内存使用和性能。
关于hash - 为什么redis dict中的load factor设置为1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54810023/