golang的map底层数据结构

介绍

mapgolangmap
map
mapmap
map
golangmapmap

哈希冲突解决

map
map
map
map
map
map
map

注意事项

map
mapmapintstringfloat64mapmap

源码解析

runtime/map.go
mapruntime/map.gohmapmap
type hmap struct {
    count     int
    flags     uint8
    B         uint8
    noverflow uint16
    hash0     uint32

    buckets    unsafe.Pointer
    oldbuckets unsafe.Pointer
    nevacuate  uintptr
    extra      *mapextra
}

哈希表的桶结构

哈希表的桶结构定义如下:

type bmap struct {
    tophash [bucketCnt]uint8
    keys    [bucketCnt]keytype
    values  [bucketCnt]valuetype
}
tophashkeysvalues

哈希冲突解决

maptophash

数据访问和修改

map
map

扩容和缩容

mapmap

相反,当键值对的数量减少时,也可能触发缩容操作。缩容操作会将键值对重新分配到更小的哈希表中。

写在最后

感谢大家的阅读,晴天将继续努力,分享更多有趣且实用的主题,如有错误和纰漏,欢迎给予指正。 更多文章敬请关注作者个人公众号 晴天码字