go语言中,map的桶的数量是根据元素数量经过计算得出的,计算方法如下:
package main
import (
"fmt"
)
func main() {
res := calcB(1000)
fmt.Println("元素数量:1000,B:", res, "桶数量", calcBucket(res))
}
func calcBucket(B uint8) int {
res := 1 << B
return res
}
func calcB(count int) uint8 {
var B uint8 = 0
for overFactor(count, B) {
B++
}
return B
}
func overFactor(count int, B uint8) bool {
const loadFactorNum = 13
const loadFactorDen = 2
const bucketCnt = 8
return count > bucketCnt && uintptr(count) > loadFactorNum*(bucketShift(B)/loadFactorDen)
}
func bucketShift(b uint8) uintptr {
return uintptr(1) << (b & ((4<<(^uintptr(0)>>63))*8 - 1))
}