脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Golang - 源码剖析Golang中map扩容底层的实现

源码剖析Golang中map扩容底层的实现

2023-03-07 15:15劲仔Go Golang

之前的文章详细介绍过Go切片和map的基本使用,以及切片的扩容机制。本文针对map的扩容,会从源码的角度全面的剖析一下map扩容的底层实现,需要的可以参考一下

前言

之前的文章详细介绍过Go切片和map的基本使用,以及切片的扩容机制。本文针对map的扩容,会从源码的角度全面的剖析一下map扩容的底层实现。

map底层结构

主要包含两个核心结构体hmapbmap

数据会先存储在正常桶hmap.buckets指向的bmap数组中,一个bmap只能存储8组键值对数据,超过则会将数据存储到溢出桶hmap.extra.overflow指向的bmap数组中

那么,当溢出桶也存储不下了,会怎么办呢,数据得存储到哪去呢?答案,肯定是扩容,那么扩容怎么实现的呢?接着往下看

源码剖析Golang中map扩容底层的实现

扩容时机

在向 map 插入新 key 的时候,会进行条件检测,符合下面这 2 个条件,就会触发扩容

?
1
2
3
4
5
6
7
8
9
10
11
// If we hit the max load factor or we have too many overflow buckets,
// and we're not already in the middle of growing, start growing.
if !h.growing() && (overLoadFactor(h.count+1, h.B) || tooManyOverflowBuckets(h.noverflow, h.B)) {
  hashGrow(t, h)
  goto again // Growing the table invalidates everything, so try again
}
 
// growing reports whether h is growing. The growth may be to the same size or bigger.
func (h *hmap) growing() bool {
  return h.oldbuckets != nil
}

条件1:超过负载

map元素个数 > 6.5 * 桶个数

?
1
2
3
4
// overLoadFactor reports whether count items placed in 1<<B buckets is over loadFactor.
func overLoadFactor(count int, B uint8) bool {
  return count > bucketCnt && uintptr(count) > loadFactorNum*(bucketShift(B)/loadFactorDen)
}

其中

  • bucketCnt = 8,一个桶可以装的最大元素个数
  • loadFactor = 6.5,负载因子,平均每个桶的元素个数
  • bucketShift(B): 桶的个数

条件2:溢出桶太多

当桶总数 < 2 ^ 15 时,如果溢出桶总数 >= 桶总数,则认为溢出桶过多。

当桶总数 >= 2 ^ 15 时,直接与 2 ^ 15 比较,当溢出桶总数 >= 2 ^ 15 时,即认为溢出桶太多了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// tooManyOverflowBuckets reports whether noverflow buckets is too many for a map with 1<<B buckets.
// Note that most of these overflow buckets must be in sparse use;
// if use was dense, then we'd have already triggered regular map growth.
func tooManyOverflowBuckets(noverflow uint16, B uint8) bool {
  // If the threshold is too low, we do extraneous work.
  // If the threshold is too high, maps that grow and shrink can hold on to lots of unused memory.
  // "too many" means (approximately) as many overflow buckets as regular buckets.
  // See incrnoverflow for more details.
  if B > 15 {
    B = 15
  }
  // The compiler doesn't see here that B < 16; mask B to generate shorter shift code.
  return noverflow >= uint16(1)<<(B&15)
}

对于条件2,其实算是对条件1的补充。因为在负载因子比较小的情况下,有可能 map 的查找和插入效率也很低,而第 1 点识别不出来这种情况。

表面现象就是负载因子比较小,即 map 里元素总数少,但是桶数量多(真实分配的桶数量多,包括大量的溢出桶)。比如不断的增删,这样会造成overflow的bucket数量增多,但负载因子又不高,达不到第 1 点的临界值,就不能触发扩容来缓解这种情况。这样会造成桶的使用率不高,值存储得比较稀疏,查找插入效率会变得非常低,因此有了第 2 扩容条件。

扩容方式

双倍扩容

针对条件1,新建一个buckets数组,新的buckets大小是原来的2倍,然后旧buckets数据搬迁到新的buckets,该方法我们称之为双倍扩容

等量扩容

针对条件2,并不扩大容量,buckets数量维持不变,重新做一遍类似双倍扩容的搬迁动作,把松散的键值对重新排列一次,使得同一个 bucket 中的 key 排列地更紧密,节省空间,提高 bucket 利用率,进而保证更快的存取,该方法我们称之为等量扩容

扩容函数

上面说的 hashGrow() 函数实际上并没有真正地“搬迁”,它只是分配好了新的 buckets,并将老的 buckets 挂到了 oldbuckets 字段上

真正搬迁 buckets 的动作在 growWork() 函数中,而调用 growWork() 函数的动作是在 mapassign 和 mapdelete 函数中。也就是插入或修改、删除 key 的时候,都会尝试进行搬迁 buckets 的工作。先检查 oldbuckets 是否搬迁完毕,具体来说就是检查 oldbuckets 是否为 nil

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
func hashGrow(t *maptype, h *hmap) {
  // If we've hit the load factor, get bigger.
  // Otherwise, there are too many overflow buckets,
  // so keep the same number of buckets and "grow" laterally.
  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
  }
  // commit the grow (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 {
    // Promote current overflow buckets to the old generation.
    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
  }
 
  // the actual copying of the hash table data is done incrementally
  // by growWork() and evacuate().
}

由于 map 扩容需要将原有的 key/value 重新搬迁到新的内存地址,如果map存储了数以亿计的key-value,一次性搬迁将会造成比较大的延时,因此 Go map 的扩容采取了一种称为“渐进式”的方式,原有的 key 并不会一次性搬迁完毕,每次最多只会搬迁 2 个 bucket

?
1
2
3
4
5
6
7
8
9
10
func growWork(t *maptype, h *hmap, bucket uintptr) {
  // make sure we evacuate the oldbucket corresponding
  // to the bucket we're about to use
  evacuate(t, h, bucket&h.oldbucketmask())
 
  // evacuate one more oldbucket to make progress on growing
  if h.growing() {
    evacuate(t, h, h.nevacuate)
  }
}

总结

要想掌握Go map扩容的底层实现,必须先掌握map的底层结构设计。基于底层结构,再从底层实现的源码,一步步分析。

到此这篇关于源码剖析Golang中map扩容底层的实现的文章就介绍到这了,更多相关Golang map扩容内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://juejin.cn/post/7207058392287346747

延伸 · 阅读

精彩推荐
  • Golang在go文件服务器加入http.StripPrefix的用途介绍

    在go文件服务器加入http.StripPrefix的用途介绍

    这篇文章主要介绍了在go文件服务器加入http.StripPrefix的用途介绍,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    yyyzhhhhh7112021-03-07
  • GolangGo 数据结构之堆排序示例详解

    Go 数据结构之堆排序示例详解

    这篇文章主要为大家介绍了Go 数据结构之堆排序示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    宇宙之一粟3572022-08-26
  • Golanggolang语言http协议get拼接参数操作

    golang语言http协议get拼接参数操作

    这篇文章主要介绍了golang语言http协议get拼接参数操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    wilsonyx10342021-03-14
  • GolangGolang项目搭配nginx部署反向代理负载均衡讲解

    Golang项目搭配nginx部署反向代理负载均衡讲解

    这篇文章主要为大家介绍了Golang项目搭配nginx部署正反向代理负载均衡讲解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职...

    Jeff的技术栈3642022-09-22
  • Golanggolang中struct和interface的基础使用教程

    golang中struct和interface的基础使用教程

    Go不同于一般的面向对象语言,需要我们好好的学习研究,下面这篇文章主要给大家介绍了关于golang中struct和interface的基础使用的相关资料,需要的朋友可...

    helight6682020-05-14
  • Golanggo for range遍历二维数组的示例

    go for range遍历二维数组的示例

    今天小编就为大家分享一篇关于go for range遍历二维数组的示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编...

    路人19946492020-05-25
  • Golanggolang 手写贪吃蛇示例实现

    golang 手写贪吃蛇示例实现

    这篇文章主要为大家介绍了golang 手写贪吃蛇示例实现,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    黑胡子Z5282022-07-21
  • Golang分析Go错误处理优化go recover机制缺陷

    分析Go错误处理优化go recover机制缺陷

    这篇文章主要为大家介绍了分析Go错误处理优化go recover机制缺陷示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职...

    煎鱼3812022-07-13