Go语言中的映射(maps)在内部使用哈希表(hash table)数据结构。

huangapple go评论83阅读模式
英文:

What data structure do Go maps use internally?

问题

我对以下内容感兴趣:

  • Go语言中的地图是如何在内部实现的?(哈希表、树形结构...)
  • 如果Go语言中的地图是哈希表,使用的是哪个哈希函数?
  • 如果Go语言中的地图是树形结构,是AVL树、红黑树还是其他类型?
  • 如果Go语言中的地图是基于数组的,它们是如何避免/处理冲突的?
英文:

I'm interested in the following:

  • How are Go maps implemented internally? (hash tables, trees...)
  • If Go maps are hash tables, which hash function is used?
  • If Go maps are trees, are they AVL, red-black, or some other type?
  • If Go maps are array-based, how do they avoid/handle collisions?

答案1

得分: 7

Go maps在内部是哈希表。

正如@twotwotwo在评论中澄清的那样,如果CPU支持相关指令,Go将使用基于AES的哈希函数。

否则,Go将使用FNV哈希函数(由Patrick Mylund Nielsen在Go-Nuts中提到)。

链接:

  • 官方Go博客:http://blog.golang.org/go-maps-in-action
  • Map源代码:http://golang.org/src/pkg/runtime/hashmap.c
  • Hash源代码:http://golang.org/src/pkg/runtime/alg.c
  • GoNuts讨论组:https://groups.google.com/forum/#!topic/golang-nuts/PY3CCBtbDsQ
英文:

Go maps are hash tables internally

As @twotwotwo clarified in the comment, Go will use an AES-based hash if the CPU has supporting instructions for it.

Otherwise Go will use a FNV hash function (As stated by Patrick Mylund Nielsen @ Go-Nuts)

Links:

huangapple
  • 本文由 发表于 2014年5月11日 07:39:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/23587455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定