当前位置:网站首页>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))
}
边栏推荐
- Deep learning classification network -- vggnet
- LeetCode 77. 组合
- Eco express micro engine system has supported one click deployment to cloud hosting
- LeetCode 27. Removing Elements
- 从设计交付到开发,轻松畅快高效率!
- Don't use the new WP collection. Don't use WordPress collection without update
- Web components series (VIII) -- custom component style settings
- 网络相关知识(硬件工程师)
- 稀疏数组(非线性结构)
- 浏览器原理思维导图
猜你喜欢

Singleton mode compilation

Browser principle mind map

来自读者们的 I/O 观后感|有奖征集获奖名单

Shenji Bailian 3.53-kruskal

The difference between session and cookies

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

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

In depth understanding of JUC concurrency (II) concurrency theory

Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')

Linear DP (split)
随机推荐
Stc8h8k series assembly and C51 actual combat - serial port sending menu interface to select different functions
BGP中的状态机
CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch
Flutter hybrid development: develop a simple quick start framework | developers say · dtalk
Redis key value database [seckill]
Decryption skills of encrypted compressed files
Comment utiliser mitmproxy
51 single chip microcomputer - ADC explanation (a/d conversion, d/a conversion)
递归(迷宫问题、8皇后问题)
深入学习JVM底层(三):垃圾回收器与内存分配策略
Web page user step-by-step operation guide plug-in driver js
State machine in BGP
The official zero foundation introduction jetpack compose Chinese course is coming!
日志(常用的日志框架)
稀疏数组(非线性结构)
Verifying downloaded files using sha256 files
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?
复杂 json数据 js前台解析 详细步骤《案例:一》
队列(线性结构)
利用传统方法(N-gram,HMM等)、神经网络方法(CNN,LSTM等)和预训练方法(Bert等)的中文分词任务实现