Commit 733567a1 authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder

runtime: use integer math for hashmap overLoadFactor

Change-Id: I92cf39a05e738a03d956779d7a1ab1ef8074b2ab
Reviewed-on: https://go-review.googlesource.com/54655
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarKeith Randall <khr@golang.org>
parent 694875cb
...@@ -64,8 +64,10 @@ const ( ...@@ -64,8 +64,10 @@ const (
bucketCntBits = 3 bucketCntBits = 3
bucketCnt = 1 << bucketCntBits bucketCnt = 1 << bucketCntBits
// Maximum average load of a bucket that triggers growth. // Maximum average load of a bucket that triggers growth is 6.5.
loadFactor = 6.5 // Represent as loadFactorNum/loadFactDen, to allow integer math.
loadFactorNum = 13
loadFactorDen = 2
// Maximum key or value size to keep inline (instead of mallocing per element). // Maximum key or value size to keep inline (instead of mallocing per element).
// Must fit in a uint8. // Must fit in a uint8.
...@@ -984,8 +986,7 @@ func hashGrow(t *maptype, h *hmap) { ...@@ -984,8 +986,7 @@ func hashGrow(t *maptype, h *hmap) {
// overLoadFactor reports whether count items placed in 1<<B buckets is over loadFactor. // overLoadFactor reports whether count items placed in 1<<B buckets is over loadFactor.
func overLoadFactor(count int64, B uint8) bool { func overLoadFactor(count int64, B uint8) bool {
// TODO: rewrite to use integer math and comparison? return count >= bucketCnt && uint64(count) >= loadFactorNum*((uint64(1)<<B)/loadFactorDen)
return count >= bucketCnt && float32(count) >= loadFactor*float32((uint64(1)<<B))
} }
// tooManyOverflowBuckets reports whether noverflow buckets is too many for a map with 1<<B buckets. // tooManyOverflowBuckets reports whether noverflow buckets is too many for a map with 1<<B buckets.
......
...@@ -91,7 +91,7 @@ func reflect_memmove(to, from unsafe.Pointer, n uintptr) { ...@@ -91,7 +91,7 @@ func reflect_memmove(to, from unsafe.Pointer, n uintptr) {
} }
// exported value for testing // exported value for testing
var hashLoad = loadFactor var hashLoad = float32(loadFactorNum) / float32(loadFactorDen)
//go:nosplit //go:nosplit
func fastrand() uint32 { func fastrand() uint32 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment