当前位置:网站首页>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))
}
边栏推荐
- 【每日一题】写一个函数,判断一个字符串是否为另外一个字符串旋转之后的字符串。
- WLAN相关知识点总结
- 递归(迷宫问题、8皇后问题)
- 介绍两款代码自动生成器,帮助提升工作效率
- The Chinese word segmentation task is realized by using traditional methods (n-gram, HMM, etc.), neural network methods (CNN, LSTM, etc.) and pre training methods (Bert, etc.)
- 经典文献阅读之--Deformable DETR
- IPv6 experiment and summary
- 【程序员的自我修养]—找工作反思篇二
- VRRP之监视上行链路
- How to use mitmproxy
猜你喜欢
随机推荐
Zhuanzhuanben - LAN construction - Notes
Web page user step-by-step operation guide plug-in driver js
LeetCode 39. 组合总和
步骤详解 | 助您轻松提交 Google Play 数据安全表单
加密压缩文件解密技巧
Arduino Wire 库使用
BGP中的状态机
How to use mitmproxy
栈(线性结构)
Scheme and implementation of automatic renewal of token expiration
LeetCode 90. Subset II
浏览器原理思维导图
Common means of modeling: combination
Cglib代理-代码增强测试
Google Go to sea entrepreneurship accelerator registration countdown 3 days, entrepreneurs pass through the guide in advance collection!
In depth understanding of JUC concurrency (II) concurrency theory
浅谈三点建议为所有已经毕业和终将毕业的同学
日期时间API详解
Data playback partner rviz+plotjuggler
网络相关知识(硬件工程师)