]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: use integer math for hashmap overLoadFactor
authorJosh Bleecher Snyder <josharian@gmail.com>
Mon, 17 Jul 2017 03:37:27 +0000 (17:37 -1000)
committerJosh Bleecher Snyder <josharian@gmail.com>
Mon, 14 Aug 2017 23:31:22 +0000 (23:31 +0000)
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: Keith Randall <khr@golang.org>
src/runtime/hashmap.go
src/runtime/stubs.go

index 87d0d26cfaa9d469b9188445b094f99c3437b415..64ec84474e07f26279f023970f4b813d9b799eef 100644 (file)
@@ -64,8 +64,10 @@ const (
        bucketCntBits = 3
        bucketCnt     = 1 << bucketCntBits
 
-       // Maximum average load of a bucket that triggers growth.
-       loadFactor = 6.5
+       // Maximum average load of a bucket that triggers growth is 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).
        // Must fit in a uint8.
@@ -984,8 +986,7 @@ func hashGrow(t *maptype, h *hmap) {
 
 // overLoadFactor reports whether count items placed in 1<<B buckets is over loadFactor.
 func overLoadFactor(count int64, B uint8) bool {
-       // TODO: rewrite to use integer math and comparison?
-       return count >= bucketCnt && float32(count) >= loadFactor*float32((uint64(1)<<B))
+       return count >= bucketCnt && uint64(count) >= loadFactorNum*((uint64(1)<<B)/loadFactorDen)
 }
 
 // tooManyOverflowBuckets reports whether noverflow buckets is too many for a map with 1<<B buckets.
index ce9b67a0eede188b06be967af999d12252eb1c53..7f504e684e50b31968ba6e92e7cb146cf1d70cb9 100644 (file)
@@ -91,7 +91,7 @@ func reflect_memmove(to, from unsafe.Pointer, n uintptr) {
 }
 
 // exported value for testing
-var hashLoad = loadFactor
+var hashLoad = float32(loadFactorNum) / float32(loadFactorDen)
 
 //go:nosplit
 func fastrand() uint32 {