当前位置:网站首页>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))
}
边栏推荐
- 找到页面当前元素z-index最高的数值
- Generic classes and parameterized classes of SystemVerilog
- In depth understanding of JUC concurrency (I) what is JUC
- 利用传统方法(N-gram,HMM等)、神经网络方法(CNN,LSTM等)和预训练方法(Bert等)的中文分词任务实现
- Golang--map扩容机制(含源码)
- Eco express micro engine system has supported one click deployment to cloud hosting
- 深入了解JUC并发(二)并发理论
- Problems encountered in uni app development (continuous update)
- 借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
- 队列(线性结构)
猜你喜欢

队列(线性结构)

借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力

Linear DP (split)

Detailed notes of ES6

Classic literature reading -- deformable Detr

如何使用MITMPROXy

Monitoring uplink of VRRP

Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish

Google Play Academy 组队 PK 赛,正式开赛!

Eco express micro engine system has supported one click deployment to cloud hosting
随机推荐
Mech 3002 explanation
VLAN experiment of switching technology
It is said that Kwai will pay for the Tiktok super fast version of the video? How can you miss this opportunity to collect wool?
LeetCode 77. 组合
sudo提权
Arduino Wire 库使用
No subject alternative DNS name matching updates. jenkins. IO found, the reason for the error and how to solve it
深入学习JVM底层(四):类文件结构
TensorRT中的循环
VRRP之监视上行链路
BGP报文详细解释
500. Keyboard line
Current situation analysis of Devops and noops
Ros2 --- lifecycle node summary
Shenji Bailian 3.53-kruskal
Introduce uview into uni app
LeetCode 83. 删除排序链表中的重复元素
Step by step | help you easily submit Google play data security form
借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
Scheme and implementation of automatic renewal of token expiration