hash
Contents
由浅到深,入门Go语言Map实现原理 golang map底层实现
the core to design a map:
- how to get hash value: hash funcitonn
- how to handle collision.
- how to rehash: grow,
hash function
what: 将输入数据经过一系列计算, 输出固定长度的数据
types:
- 加密: 低碰撞,难复原
- 非加密
why md5 is cryptographic:
- one-way funciton
- collision-resistant,
hash function excample
myhash:
|
|
djb:
|
|
handle collision
-
open addressing;开放寻址法; 继续在原数组上探测一下空位置;
-
separate chaining;分离的链表
使用额外的空间存储冲突;
- 链表;
- 树: 二叉树,红黑树;
rehash
grow
-
what? 填充度:load_factor= used_buceket/total_bucket 填充度太大或者太小的时候进行调整;
-
when? grow: 填充度 >=1; shrink: 填充度 <=0.1
-
how?
创建两组字典;
- create h[1], h[1].buckets.len = h[2].buckets * 2
- 渐进式迁移
- set is hash = true
- rehashIndex++;
- rehashstep: 被动
- dictRehashMilliseconds:定时任务