当前位置:网站首页>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))
}
边栏推荐
- JWT tool class
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ F: Polyhedral dice
- Monitoring uplink of VRRP
- 递归(迷宫问题、8皇后问题)
- Go learning notes integration
- Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!
- External interrupts cannot be accessed. Just delete the code and restore it Record this unexpected bug
- Web components series (VIII) -- custom component style settings
- Lucene Basics
- 神机百炼3.52-Prim
猜你喜欢

From design delivery to development, easy and efficient!

AttributeError: ‘str‘ object has no attribute ‘decode‘

神机百炼3.52-Prim

Shenji Bailian 3.54-dichotomy of dyeing judgment

Invalid operation: Load into table ‘sources_ orderdata‘ failed. Check ‘stl_ load_ errors‘ system table

Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4

Decryption skills of encrypted compressed files

深入了解JUC并发(二)并发理论

Little bear sect manual query and ADC in-depth study

In depth understanding of JUC concurrency (I) what is JUC
随机推荐
步骤详解 | 助您轻松提交 Google Play 数据安全表单
ROS create workspace
Data playback partner rviz+plotjuggler
Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!
WLAN相关知识点总结
Detailed explanation of BGP message
神机百炼3.54-染色法判定二分图
锐捷EBGP 配置案例
CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch
Stc8h8k series assembly and C51 actual combat - serial port sending menu interface to select different functions
经典文献阅读之--Deformable DETR
Web page user step-by-step operation guide plug-in driver js
Decryption skills of encrypted compressed files
BGP中的状态机
Introduce uview into uni app
借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
社区说|Kotlin Flow 的原理与设计哲学
Support new and old imperial CMS collection and warehousing tutorials
加密压缩文件解密技巧
The official zero foundation introduction jetpack compose Chinese course is coming!