]> Cypherpunks repositories - gostls13.git/commitdiff
map: use correct load factor for deciding when to grow
authorKeith Randall <khr@golang.org>
Sat, 7 Oct 2023 16:19:16 +0000 (09:19 -0700)
committerKeith Randall <khr@golang.org>
Mon, 9 Oct 2023 15:38:54 +0000 (15:38 +0000)
The correct load factor is 6.5, not 6.
This got broken by accident in CL 462115.

Fixes #63438

Change-Id: Ib07bb6ab6103aec87cb775bc06bd04362a64e489
Reviewed-on: https://go-review.googlesource.com/c/go/+/533279
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/runtime/export_test.go
src/runtime/map.go
src/runtime/map_test.go

index f81e8a9ea1ce0135701feecc136ef07e237d1312..6335dab41bff9f07f03de21dd91b0ce7e8b8f359 100644 (file)
@@ -583,6 +583,10 @@ func MapBucketsPointerIsNil(m map[int]int) bool {
        return h.buckets == nil
 }
 
+func OverLoadFactor(count int, B uint8) bool {
+       return overLoadFactor(count, B)
+}
+
 func LockOSCounts() (external, internal uint32) {
        gp := getg()
        if gp.m.lockedExt+gp.m.lockedInt == 0 {
index e6d651f688380ea2647a3fca38730174f387f96d..5b264b07136040e6fe5083da44157325bf07529f 100644 (file)
@@ -70,7 +70,7 @@ const (
        // Because of minimum alignment rules, bucketCnt is known to be at least 8.
        // Represent as loadFactorNum/loadFactorDen, to allow integer math.
        loadFactorDen = 2
-       loadFactorNum = (bucketCnt * 13 / 16) * loadFactorDen
+       loadFactorNum = loadFactorDen * bucketCnt * 13 / 16
 
        // Maximum key or elem size to keep inline (instead of mallocing per element).
        // Must fit in a uint8.
index 300e996de3e4fdceddcbbe2ee7add9fcd6c72074..7e911b9fc915b05d3427b300983dc0ecc83aa3eb 100644 (file)
@@ -1419,3 +1419,18 @@ func TestEmptyMapWithInterfaceKey(t *testing.T) {
                _ = mi[panicStructKey]
        })
 }
+
+func TestLoadFactor(t *testing.T) {
+       for b := uint8(0); b < 20; b++ {
+               count := 13 * (1 << b) / 2 // 6.5
+               if b == 0 {
+                       count = 8
+               }
+               if runtime.OverLoadFactor(count, b) {
+                       t.Errorf("OverLoadFactor(%d,%d)=true, want false", count, b)
+               }
+               if !runtime.OverLoadFactor(count+1, b) {
+                       t.Errorf("OverLoadFactor(%d,%d)=false, want true", count+1, b)
+               }
+       }
+}