当前位置:网站首页>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))
}
边栏推荐
- 浏览器原理思维导图
- Shenji Bailian 3.52-prim
- Scheme and implementation of automatic renewal of token expiration
- The real definition of open source software
- Zhuanzhuanben - LAN construction - Notes
- Use some common functions of hbuilderx
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
- 10 erreurs classiques de MySQL
- TensorRT中的循环
- Ros2 --- lifecycle node summary
猜你喜欢
LeetCode 78. subset
日志(常用的日志框架)
500. Keyboard line
深入学习JVM底层(四):类文件结构
Pbootcms collection and warehousing tutorial quick collection release
How to use mitmproxy
Detailed explanation of BGP message
Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
Eco express micro engine system has supported one click deployment to cloud hosting
VLAN experiment of switching technology
随机推荐
栈(线性结构)
Deep learning classification network -- Network in network
LeetCode 27. Removing Elements
Detailed steps of JS foreground parsing of complex JSON data "case: I"
深入学习JVM底层(二):HotSpot虚拟机对象
Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
500. Keyboard line
Browser principle mind map
Is there a really free applet?
I/o multiplexing & event driven yyds dry inventory
Classic literature reading -- deformable Detr
【程序员的自我修养]—找工作反思篇二
In depth understanding of JUC concurrency (II) concurrency theory
Scheme and implementation of automatic renewal of token expiration
RestTemplate请求时设置请求头,请求参数,请求体。
BGP中的状态机
LeetCode 39. 组合总和
Deep learning classification network -- vggnet
Decryption skills of encrypted compressed files
来自读者们的 I/O 观后感|有奖征集获奖名单