当前位置:网站首页>Golang -- map capacity expansion mechanism (including source code)
Golang -- map capacity expansion mechanism (including source code)
2022-07-02 06:20:00 【Srwici】
Catalog
1 Expand capacity equally
1.1 The trigger condition
There are too many overflowing barrels
- B Greater than 15 when , With 15 Calculation , If the bucket overflows >= 2^15 Power , Trigger equal expansion
- When B Less than 15 when , With B Calculation , If the bucket overflows >= Greater than 2^B Power , Trigger equal expansion
1.2 Trigger result
Create a new one of the same size map, Move the old data
1.3 Source code
route : go1.18/src/runtime/map.go
// overflow buckets Too much
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 Incremental capacity expansion
2.1 The trigger condition
Load factor >6.5
- Load factor :map Total elements /bucket Number .
- map The total amount of elements is :hmap Medium count.
- bucket Number :2^B
2.2 Trigger result
Capacity to double , Move data .
2.3 Source code
route : go1.18/src/runtime/map.go
func overLoadFactor(count int, B uint8) bool {
return count > bucketCnt && uintptr(count) > loadFactorNum*(bucketShift(B)/loadFactorDen)
// People words : When count Greater than 8 And the bearing factor is greater than 6.5 It can meet the requirements
}
count (
bucketCntBits = 3
bucketCnt = 1 << bucketCntBits
loadFactorNum = 13
loadFactorDen = 2
)
// bucketShift returns 1<<b, optimized for code generation.
// translate , return 2 Of B Power
func bucketShift(b uint8) uintptr {
// Masking the shift amount allows overflow checks to be elided.
return uintptr(1) << (b & (goarch.PtrSize*8 - 1))
}
边栏推荐
- Support new and old imperial CMS collection and warehousing tutorials
- Shenji Bailian 3.54-dichotomy of dyeing judgment
- 找到页面当前元素z-index最高的数值
- Community theory | kotlin flow's principle and design philosophy
- Talking about MySQL database
- 谷歌出海创业加速器报名倒计时 3 天,创业人闯关指南提前收藏!
- Linear DP (split)
- 深入学习JVM底层(四):类文件结构
- Eco express micro engine system has supported one click deployment to cloud hosting
- 让每一位开发者皆可使用机器学习技术
猜你喜欢

Shenji Bailian 3.54-dichotomy of dyeing judgment

Spark overview

Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!

Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes

最新CUDA环境配置(Win10 + CUDA 11.6 + VS2019)

Monitoring uplink of VRRP

网络相关知识(硬件工程师)

Frequently asked questions about jetpack compose and material you

Deep learning classification network -- Network in network

Detailed explanation of BGP message
随机推荐
最新CUDA环境配置(Win10 + CUDA 11.6 + VS2019)
网络相关知识(硬件工程师)
Detailed steps of JS foreground parsing of complex JSON data "case: I"
No subject alternative DNS name matching updates. jenkins. IO found, the reason for the error and how to solve it
Common means of modeling: combination
亚马逊aws数据湖工作之坑1
Little bear sect manual query and ADC in-depth study
深入学习JVM底层(二):HotSpot虚拟机对象
Classic literature reading -- deformable Detr
LeetCode 47. 全排列 II
Golang--map扩容机制(含源码)
Zabbix Server trapper 命令注入漏洞 (CVE-2017-2824)
Problems encountered in uni app development (continuous update)
Top 10 classic MySQL errors
利用NVIDIA GPU将Minecraft场景渲染成真实场景
Web components series (VIII) -- custom component style settings
加密压缩文件解密技巧
New version of dedecms collection and release plug-in tutorial tool
Jetpack Compose 与 Material You 常见问题解答
Monitoring uplink of VRRP