当前位置:网站首页>Golang--map扩容机制(含源码)
Golang--map扩容机制(含源码)
2022-07-02 06:11:00 【Srwici】
1 等量扩容
1.1 触发条件
溢出桶数量过多
- B大于15时,以15计算,如果溢出桶 >= 2^15次方,触发等量扩容
- 当B小于15时,以B计算,如果溢出桶 >= 大于2^B次方,触发等量扩容
1.2 触发结果
新建等量大小的map,将旧数据挪过去
1.3 源码
路径: go1.18/src/runtime/map.go
// overflow buckets 太多
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 增量扩容
2.1 触发条件
承载因子>6.5
- 承载因子:map元素总量/bucket数量。
- map元素总量为:hmap中的count。
- bucket数量:2^B
2.2 触发结果
容量翻倍,挪移数据。
2.3 源码
路径: go1.18/src/runtime/map.go
func overLoadFactor(count int, B uint8) bool {
return count > bucketCnt && uintptr(count) > loadFactorNum*(bucketShift(B)/loadFactorDen)
//人话:当count大于8且承载因子大于6.5时可满足要求
}
count (
bucketCntBits = 3
bucketCnt = 1 << bucketCntBits
loadFactorNum = 13
loadFactorDen = 2
)
// bucketShift returns 1<<b, optimized for code generation.
// 翻译,返回2的B次方
func bucketShift(b uint8) uintptr {
// Masking the shift amount allows overflow checks to be elided.
return uintptr(1) << (b & (goarch.PtrSize*8 - 1))
}
边栏推荐
- Go learning notes integration
- 深入学习JVM底层(二):HotSpot虚拟机对象
- Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4
- Current situation analysis of Devops and noops
- CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch
- 深入学习JVM底层(三):垃圾回收器与内存分配策略
- Problems encountered in uni app development (continuous update)
- Style modification of Mui bottom navigation
- 浅谈三点建议为所有已经毕业和终将毕业的同学
- LeetCode 90. 子集 II
猜你喜欢

加密压缩文件解密技巧

Google Go to sea entrepreneurship accelerator registration countdown 3 days, entrepreneurs pass through the guide in advance collection!

Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed

500. Keyboard line

Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability

深入了解JUC并发(一)什么是JUC

Little bear sect manual query and ADC in-depth study

LeetCode 78. 子集

VRRP之监视上行链路

Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4
随机推荐
Monitoring uplink of VRRP
注解和反射详解以及运用
JWT tool class
Use some common functions of hbuilderx
LeetCode 283. 移动零
Mock simulate the background return data with mockjs
Detailed notes of ES6
Deep learning classification network -- alexnet
LeetCode 283. Move zero
Introduce uview into uni app
Jetpack Compose 与 Material You 常见问题解答
Support new and old imperial CMS collection and warehousing tutorials
LeetCode 77. combination
复杂 json数据 js前台解析 详细步骤《案例:一》
Network related knowledge (Hardware Engineer)
神机百炼3.54-染色法判定二分图
深入学习JVM底层(五):类加载机制
ZABBIX server trap command injection vulnerability (cve-2017-2824)
Generic classes and parameterized classes of SystemVerilog
Classic literature reading -- deformable Detr