Go中map的内存模型
在开发过程中,map是必不可少的数据结构,在Golang中,使用map或多或少会遇到与其他语言不一样的体验,比如访问不存在的元素会返回其类型的空值、map的大小究竟是多少,为什么会报"cannot take the address of"错误,遍历map的随机性等等。
本文希望通过研究map的底层实现,以解答这些疑惑。
hashmap的定义位于 src/runtime/hashmap.go 中,首先我们看下hashmap和bucket的定义:
type hmap struct {
    count     int    // 元素的个数
    flags     uint8  // 状态标志
    B         uint8  // 可以最多容纳 6.5 * 2 ^ B 个元素,6.5为装载因子
    noverflow uint16 // 溢出的个数
    hash0     uint32 // 哈希种子
    buckets    unsafe.Pointer // 桶的地址
    oldbuckets unsafe.Pointer // 旧桶的地址,用于扩容
    nevacuate  uintptr        // 搬迁进度,小于nevacuate的已经搬迁
    overflow *[2]*[]*bmap 
}
这篇文章写的很有深度
参考: https://www.cnblogs.com/sunsky303/p/11815172.html
    吾尝终日而思矣,不如须臾之所学也。吾尝跂而望矣,不如登高之博见也。……君子生非异也,善假于物也。
 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号