Golang中map的实现原理

前言

在Go语言中,一个map就是一个哈希表的引用。它是一个无序的key/value对的集合,其中,所有的key都是不同的。然后通过给定的key可以在常数时间复杂度内检索、更新或删除对应的value
在map中的元素不是一个变量,因此不能对map的元素进行取址操作。因为map可能随着元素数量的增加而重新分配内存更大的内存空间,从而导致之前的地址失效

map实现原理

注意:我会把源码中每个方法的作用都注释出来,可以参考注释进行理解。

数据结构

我们先来看一下map数据结构

runtime/map.go/hmap

type hmap struct {
    
    
	count     int // map 中的元素个数, 内置的 len 函数会从这里读取
	flags     uint8
	B         uint8  // 指示bucket数组的大小
	noverflow uint16 // 溢出桶的个数
	hash0     uint32 // hash种子
	buckets    unsafe.Pointer // 2^B 大小的数组,如果 count == 0 的话,可能是 nil,指向bmap结构指针
	oldbuckets unsafe.Pointer // 之前的 bucket数组,只有在扩容(growing)过程中是非 nil
	nevacuate  uintptr        // 已经搬迁的bucket个数
	extra *mapextra // 额外字段,当k,v中不包含指针时用到
}
type mapextra struct {
    
    
	//如果key和value都不包含指针并且可以被 inline(size 都小于 128 字节的情况下),则我们将存储桶类型标记为不包含指针。 这样可以避免扫描此类地图。
	//但是,bmap.overflow是一个指针。 为了使溢出桶保持活动状态,我们将指向所有溢出桶的指针存储在//hmap.extra.overflow和hmap.extra.oldoverflow中。
	//仅当键和值不包含指针时,才使用overflow和oldoverflow。
	//overflow包含hmap.buckets的溢出桶。
	// oldoverflow包含hmap.oldbuckets的溢出桶。
	overflow    *[]*bmap
	oldoverflow *[]*bmap

	//指向空闲的 overflow bucket 的指针
	nextOverflow *bmap
}

再来看一下bucket结构体

type bmap struct {
    
    
	// tophash generally contains the top byte of the hash value
	// for each key in this bucket. If tophash[0] < minTopHash,
	// tophash[0] is a bucket evacuation state instead.
	tophash [bucketCnt]uint8 // hash 值的高 8 位
	
}

初始化

例如:

var m=make(map[string]string,hint)

在 hint <= 8(bucketSize) 时,会调用 makemap_small 来进行初始化,如果 hint > 8,则调用 makemap。
不提供 hint 的时候,编译器始终会调用 makemap_small 来初始化。

func makemap_small() *hmap {
    
    
	h := new(hmap)
	h.hash0 = fastrand()
	return h
}
// makemap为make(map [k] v,hint)实现Go创建。
//如果编译器确定该映射或第一个存储桶可以在堆栈上创建,h和/或bucket可以为非零。
//如果h!= nil,可以直接在 h 内创建 map
//如果h.buckets!= nil,则指向的存储桶可以用作第一个存储桶。
func makemap(t *maptype, hint int, h *hmap) *hmap {
    
    
	//需要申请内存的大小
	mem, overflow := math.MulUintptr(uintptr(hint), t.bucket.size)
	if overflow || mem > maxAlloc {
    
    
		hint = 0
	}

	// 初始化hmap
	if h == nil {
    
    
		h = new(hmap)
	}
	//hash值
	h.hash0 = fastrand()

	// 按照提供的元素个数,找一个可以放得下这么多元素的 B 值
	B := uint8(0)
	for overLoadFactor(hint, B) {
    
    
		B++
	}
	h.B = B

	//分配初始哈希表
	//如果B == 0,则稍后延迟分配buckets字段(在mapassign中)
	//如果提示很大,则将该内存清零可能需要一段时间。
	if h.B != 0 {
    
    
		var nextOverflow *bmap
		h.buckets, nextOverflow = makeBucketArray(t, h.B, nil)
		if nextOverflow != nil {
    
    
			h.extra = new(mapextra)
			h.extra.nextOverflow = nextOverflow
		}
	}

	return h
}

当然,实际选用哪个函数不只要看 hint,还要看逃逸分析结果,比如下面这段代码,在生成的汇编中,你是找不到 makemap 的踪影的:

package main

func main() {
    
    
	var m = make(map[string]int, 3)
	m["1"] = 1
}

插入值

插入值的流程我们主要看一下mapassign函数

func mapassign(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer {
    
    
	//map为空时,不能赋值
	if h == nil {
    
    
		panic(plainError("assignment to entry in nil map"))
	}
	//扫描
	if raceenabled {
    
    
		callerpc := getcallerpc()
		pc := funcPC(mapassign)
		racewritepc(unsafe.Pointer(h), callerpc, pc)
		raceReadObjectPC(t.key, key, callerpc, pc)
	}
	if msanenabled {
    
    
		msanread(key, t.key.size)
	}
	if h.flags&hashWriting != 0 {
    
    
		throw("concurrent map writes")
	}
	//调用hash算法
	alg := t.key.alg
	hash := alg.hash(key, uintptr(h.hash0))

	//调用alg.hash之后设置hashWriting,因为alg.hash可能会出panic,
	//在这种情况下,我们实际上并没有执行写操作。
	h.flags ^= hashWriting
	
	//分配第一个bucket
	if h.buckets == nil {
    
    
		h.buckets = newobject(t.bucket) // newarray(t.bucket, 1)
	}

again:
	//计算低 B 位 hash,根据计算出的 bucketMask 选择对应的 bucket
	//如果b=5 ==>bucketMask(h.B)=11111
	//为了计算在buckets数组中的位置
	bucket := hash & bucketMask(h.B)
	//判断是否存在扩容迁移未完成的
	if h.growing() {
    
    
		growWork(t, h, bucket)
	}
	//计算出存储的 bucket 的内存位置
    // pos = start + bucket * bucetsize
	b := (*bmap)(unsafe.Pointer(uintptr(h.buckets) + bucket*uintptr(t.bucketsize)))
	//hash高8位
	//为了计算在bucket中的位置
	top := tophash(hash)

	var inserti *uint8
	var insertk unsafe.Pointer
	var val unsafe.Pointer
bucketloop:
	for {
    
    
		//遍历bucket中的8个元素
		//bucketCnt是常量,默认8个
		for i := uintptr(0); i < bucketCnt; i++ {
    
    
			//
			if b.tophash[i] != top {
    
    
				//如果当前位置为空,记录下来
				//因为delete操作后,会出现中间有空位置的情况
				//再次插入时,需要先将前面空的位子补上,再才依次插入后面的位置
				if isEmpty(b.tophash[i]) && inserti == nil {
    
    
					inserti = &b.tophash[i]
					insertk = add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize))
					val = add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.valuesize))
				}
				//如果bucket里面没有值。则跳出bucketloop循环
				if b.tophash[i] == emptyRest {
    
    
					break bucketloop
				}
				continue
			}
			k := add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize))
			if t.indirectkey() {
    
    
				k = *((*unsafe.Pointer)(k))
			}
			// 如果相同的 hash 位置的 key 和要插入的 key 字面上不相等
            // 如果两个 key 的首八位后最后八位哈希值一样,就会进行其值比较
            // 也就是hash冲突
			if !alg.equal(key, k) {
    
    
				continue
			}
			// 对应的位置已经有 key 了,直接更新就行
			if t.needkeyupdate() {
    
    
				typedmemmove(t.key, k, key)
			}
			val = add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.valuesize))
			goto done
		}
		// bucket 的 8 个槽没有满足条件的能插入或者能更新的,去 overflow 里继续找
		ovf := b.overflow(t)
		if ovf == nil {
    
    
			break
		}
		//将链表的下一个元素赋值,继续循环
		b = ovf
	}

	// 没有找到 key,分配新的空间

	// 如果我们达到最大负载率或overflow buckets桶过多,
	// 并且这个时刻没有在进行 growing ,那么就开始 growing
	//扩容
	if !h.growing() && (overLoadFactor(h.count+1, h.B) || tooManyOverflowBuckets(h.noverflow, h.B)) {
    
    
		hashGrow(t, h)
		// hashGrow 的时候会把当前的 bucket 放到 oldbucket 里
        // 但还没有开始分配新的 bucket,所以需要到 again 重试一次
        // 重试的时候在 growWork 里会把这个 key 的 bucket 优先分配好
		goto again // Growing the table invalidates everything, so try again
	}

	if inserti == nil {
    
    
		// 前面在桶里找的时候,没有找到能塞这个 tophash 的位置
        // 说明当前所有 buckets 都是满的,分配一个新的 bucket
		newb := h.newoverflow(t, b)
		inserti = &newb.tophash[0]
		insertk = add(unsafe.Pointer(newb), dataOffset)
		val = add(insertk, bucketCnt*uintptr(t.keysize))
	}

	//把新的 key 和 value 存储到应插入的位置
	//key > 128 字节时,indirectkey = true
	if t.indirectkey() {
    
    
		kmem := newobject(t.key)
		*(*unsafe.Pointer)(insertk) = kmem
		insertk = kmem
	}
	//value > 128 字节时,indirectvalue = true
	if t.indirectvalue() {
    
    
		vmem := newobject(t.elem)
		*(*unsafe.Pointer)(val) = vmem
	}
	typedmemmove(t.key, insertk, key)
	*inserti = top
	h.count++

done:
	if h.flags&hashWriting == 0 {
    
    
		throw("concurrent map writes")
	}
	h.flags &^= hashWriting
	if t.indirectvalue() {
    
    
		val = *((*unsafe.Pointer)(val))
	}
	return val
}

其中,需要注意的几点就是:

  1. 每个bucket最多包含8个k,v
  2. Go map使用链地址法来解决hash冲突

扩容

// 如果达到负载系数(load factor),则增大尺寸 
// 如果还没到阈值,那么只需要保持相同数量的 bucket,横向“增长”。
func hashGrow(t *maptype, h *hmap) {
    
    
	
	bigger := uint8(1)
	//判断是增量扩容还是等量扩容
	if !overLoadFactor(h.count+1, h.B) {
    
    
		bigger = 0
		h.flags |= sameSizeGrow
	}
	//赋值,准备迁移
	oldbuckets := h.buckets
	newbuckets, nextOverflow := makeBucketArray(t, h.B+bigger, nil)

	flags := h.flags &^ (iterator | oldIterator)
	if h.flags&iterator != 0 {
    
    
		flags |= oldIterator
	}
	// 提交扩容(atomic wrt gc)
	h.B += bigger
	h.flags = flags
	h.oldbuckets = oldbuckets
	h.buckets = newbuckets
	h.nevacuate = 0
	h.noverflow = 0

	if h.extra != nil && h.extra.overflow != nil {
    
    
		//将当前的oldoverflow 提升到老一代
		if h.extra.oldoverflow != nil {
    
    
			throw("oldoverflow is not nil")
		}
		h.extra.oldoverflow = h.extra.overflow
		h.extra.overflow = nil
	}
	if nextOverflow != nil {
    
    
		if h.extra == nil {
    
    
			h.extra = new(mapextra)
		}
		h.extra.nextOverflow = nextOverflow
	}
	// 实际的哈希表元素的拷贝工作是在 growWork 和 evacuate 中增量慢慢地进行的
}
func growWork(t *maptype, h *hmap, bucket uintptr) {
    
    
	// 确保我们移动的 oldbucket 对应的是我们马上就要用到的那一个
	evacuate(t, h, bucket&h.oldbucketmask())

	 // 如果还在 growing 状态,再多移动一个 oldbucket
	if h.growing() {
    
    
		evacuate(t, h, h.nevacuate)
	}
}
func evacuate(t *maptype, h *hmap, oldbucket uintptr) {
    
    
	b := (*bmap)(add(h.oldbuckets, oldbucket*uintptr(t.bucketsize)))
	newbit := h.noldbuckets()
	if !evacuated(b) {
    
    
		// TODO: reuse overflow buckets instead of using new ones, if there
		// is no iterator using the old buckets.  (If !oldIterator.)

		// xy 包含的是移动的目标
        // x 表示新 bucket 数组的前(low)半部分
        // y 表示新 bucket 数组的后(high)半部分
		var xy [2]evacDst
		x := &xy[0]
		x.b = (*bmap)(add(h.buckets, oldbucket*uintptr(t.bucketsize)))
		x.k = add(unsafe.Pointer(x.b), dataOffset)
		x.v = add(x.k, bucketCnt*uintptr(t.keysize))
		
		//如果不是等量扩容,即负载因子不大于6.5
		//与之相反的就是增量扩容,即负载因子大于6.5,即bucket数组是原来的一倍
		if !h.sameSizeGrow() {
    
    
			// 如果 map 大小(hmap.B)增大了,那么我们只计算 y
            // 否则 GC 可能会看到损坏的指针
			y := &xy[1]
			y.b = (*bmap)(add(h.buckets, (oldbucket+newbit)*uintptr(t.bucketsize)))
			y.k = add(unsafe.Pointer(y.b), dataOffset)
			y.v = add(y.k, bucketCnt*uintptr(t.keysize))
		}

		for ; b != nil; b = b.overflow(t) {
    
    
			k := add(unsafe.Pointer(b), dataOffset)
			v := add(k, bucketCnt*uintptr(t.keysize))
			for i := 0; i < bucketCnt; i, k, v = i+1, add(k, uintptr(t.keysize)), add(v, uintptr(t.valuesize)) {
    
    
				top := b.tophash[i]
				if isEmpty(top) {
    
    
					b.tophash[i] = evacuatedEmpty
					continue
				}
				if top < minTopHash {
    
    
					throw("bad map state")
				}
				k2 := k
				if t.indirectkey() {
    
    
					k2 = *((*unsafe.Pointer)(k2))
				}
				var useY uint8
				if !h.sameSizeGrow() {
    
    
					// 计算哈希,以判断我们的数据要转移到哪一部分的 bucket
                    // 可能是 x 部分,也可能是 y 部分
					hash := t.key.alg.hash(k2, uintptr(h.hash0))
					if h.flags&iterator != 0 && !t.reflexivekey() && !t.key.alg.equal(k2, k2) {
    
    
						//if key type is reflexive, don't call equal(k, k)
                        // key != key,只有在 float 数的 NaN 时会出现
                        // 比如:
                        // n1 := math.NaN()
                        // fmt.Println(n1 == n1) false
                        // 这种情况下 n1 和 n2 的哈希值也完全不一样
                        // 但是对于这种 key 我们也可以随意对其目标进行发配
                        // 同时 tophash 对于 NaN 也没啥意义
                        // 还是按正常的情况下算一个随机的 tophash
                        // 然后公平地把这些 key 平均分布到各 bucket 就好
						useY = top & 1//让这个 key 50% 概率去 Y 半区
						top = tophash(hash)
					} else {
    
    
						// xxx1xxx & 1000 > 0
						//说明这个元素在扩容后一定会去上半区
						if hash&newbit != 0 {
    
    
							useY = 1
						}
					}
				}

				if evacuatedX+1 != evacuatedY || evacuatedX^1 != evacuatedY {
    
    
					throw("bad evacuatedN")
				}

				b.tophash[i] = evacuatedX + useY // evacuatedX + 1 == evacuatedY
				dst := &xy[useY]                 // evacuation destination

				if dst.i == bucketCnt {
    
    
					dst.b = h.newoverflow(t, dst.b)
					dst.i = 0
					dst.k = add(unsafe.Pointer(dst.b), dataOffset)
					dst.v = add(dst.k, bucketCnt*uintptr(t.keysize))
				}
				dst.b.tophash[dst.i&(bucketCnt-1)] = top // mask dst.i as an optimization, to avoid a bounds check
				if t.indirectkey() {
    
    
					*(*unsafe.Pointer)(dst.k) = k2 // 拷贝指针
				} else {
    
    
					typedmemmove(t.key, dst.k, k) // copy value
				}
				if t.indirectvalue() {
    
    
					*(*unsafe.Pointer)(dst.v) = *(*unsafe.Pointer)(v)
				} else {
    
    
					typedmemmove(t.elem, dst.v, v)
				}
				dst.i++
				// These updates might push these pointers past the end of the
				// key or value arrays.  That's ok, as we have the overflow pointer
				// at the end of the bucket to protect against pointing past the
				// end of the bucket.
				dst.k = add(dst.k, uintptr(t.keysize))
				dst.v = add(dst.v, uintptr(t.valuesize))
			}
		}
		//取消链接overflow buckets并清除键/值以帮助GC。
		if h.flags&oldIterator == 0 && t.bucket.kind&kindNoPointers == 0 {
    
    
			b := add(h.oldbuckets, oldbucket*uintptr(t.bucketsize))
			//保留b.tophash,因为保存扩容状态
			ptr := add(b, dataOffset)
			n := uintptr(t.bucketsize) - dataOffset
			memclrHasPointers(ptr, n)
		}
	}

	if oldbucket == h.nevacuate {
    
    
		advanceEvacuationMark(h, t, newbit)
	}
}

扩容有两点原因:

  1. 是不是已经到了 load factor 的临界点,即元素个数 >= 桶个数 * 6.5,这时候说明大部分的桶可能都快满了,如果插入新元素,有大概率需要挂在 overflow 的桶上。

    负载因子 = 键数量/bucket数量

  2. overflow buckets是不是太多了,当 bucket 总数 < 2 ^ 15 时,如果 overflow 的 bucket 总数 >= bucket 的总数,那么我们认为 overflow 的桶太多了。即当overflow 的 bucket >= 2 ^ 15 时,则认为溢出桶太多了。

而Go语言针对这两个原因设置了两个不同的增量方式:

  1. 增量扩容:针对第一种情况。将原bucket数组扩容至两倍,将扩容后的数组分为X,Y两部分,然后旧bucket数据搬迁到新的bucket。
    考虑到如果map存储了数以亿计的key-value,一次性搬迁将会造成比较大的延时,Go采用逐步搬迁策略,即每次访问map时都会触发一次搬迁,每次搬迁2个键值对。当oldbuckets中的键值对全部搬迁完毕后,删除oldbuckets。
  2. 等量扩容:针对第二种情况。并不是扩大容量,bucket数组长度不变,重新做一遍类似增量扩容的搬迁动作,把松散的键值对重新排列一次,以使bucket的使用率更高,进而保证更快的存取。

查找

func mapaccess2(t *maptype, h *hmap, key unsafe.Pointer) (unsafe.Pointer, bool) {
    
    
	//扫描
	if raceenabled && h != nil {
    
    
		callerpc := getcallerpc()
		pc := funcPC(mapaccess2)
		racereadpc(unsafe.Pointer(h), callerpc, pc)
		raceReadObjectPC(t.key, key, callerpc, pc)
	}
	if msanenabled && h != nil {
    
    
		msanread(key, t.key.size)
	}
	// map 为空,或者元素数为 0,直接返回未找到
	if h == nil || h.count == 0 {
    
    
		if t.hashMightPanic() {
    
    
			t.key.alg.hash(key, 0) // see issue 23734
		}
		return unsafe.Pointer(&zeroVal[0]), false
	}
	if h.flags&hashWriting != 0 {
    
    
		throw("concurrent map read and map write")
	}
	// 不同类型的 key,所用的 hash 算法是不一样的
	alg := t.key.alg
	hash := alg.hash(key, uintptr(h.hash0))
	m := bucketMask(h.B)
	b := (*bmap)(unsafe.Pointer(uintptr(h.buckets) + (hash&m)*uintptr(t.bucketsize)))
	//表示该map正在执行扩容,所以有一些老的元素还在oldbuckets里面
	if c := h.oldbuckets; c != nil {
    
    
		if !h.sameSizeGrow() {
    
    
			//	增量扩容
			// 说明之前只有一半的 bucket,需要除 2
			m >>= 1
		}
		oldb := (*bmap)(unsafe.Pointer(uintptr(c) + (hash&m)*uintptr(t.bucketsize)))
		if !evacuated(oldb) {
    
    
			b = oldb
		}
	}
	//取高八位
	top := tophash(hash)
bucketloop:
	//一个bucket最多存放8个元素,存在hash冲突的都会新建一个bucket,挂在原来的 bucket 的 overflow 指针成员上
	for ; b != nil; b = b.overflow(t) {
    
    
		for i := uintptr(0); i < bucketCnt; i++ {
    
    
			if b.tophash[i] != top {
    
    
				if b.tophash[i] == emptyRest {
    
    
					break bucketloop
				}
				continue
			}
			k := add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize))
			if t.indirectkey() {
    
    
				k = *((*unsafe.Pointer)(k))
			}
			if alg.equal(key, k) {
    
    
				v := add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.valuesize))
				if t.indirectvalue() {
    
    
					v = *((*unsafe.Pointer)(v))
				}
				return v, true
			}
		}
	}
	return unsafe.Pointer(&zeroVal[0]), false
}

查找过程比较简单,主要归结如下:

  1. 跟据key值算出哈希值
  2. 取哈希值低位与hmpa.B取模确定bucket位置
  3. 如果当前处于搬迁过程,则优先从oldbuckets查找
  4. 取哈希值高位在tophash数组中查询
  5. 如果tophash[i]中存储值也哈希值相等,则去找到该bucket中的key值进行比较
  6. 当前bucket没有找到,则继续从下个overflow的bucket中查找
  7. 如果查找不到,也不会返回空值,而是返回相应类型的0值

总结

  • map遍历顺序是随机的
  • map底层由hmap+bmap(bucket)两个结构体组成
  • 每个bucket最多存放8个K,V键值对,多的,则用类似于链表的方式将bucket连接起来,指示下一个bucket指针称为overflow bucket,挂在上一个的 bucket 的 overflow 指针成员上。
  • hash冲突采用链地址法解决
  • 扩容过程不是一次搬迁,而是采用了逐步搬迁策略
  • map是非线程安全的

猜你喜欢

转载自blog.csdn.net/xzw12138/article/details/107288181