]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: add benchmark of iteration over map with low load
authorMichael Pratt <mpratt@google.com>
Mon, 11 Nov 2024 21:07:58 +0000 (16:07 -0500)
committerGopher Robot <gobot@golang.org>
Wed, 13 Nov 2024 18:14:14 +0000 (18:14 +0000)
Change-Id: I3a3b7da6245a18bf1db0c595008f0eea853ce544
Reviewed-on: https://go-review.googlesource.com/c/go/+/627155
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Michael Pratt <mpratt@google.com>

src/runtime/map_benchmark_test.go

index 5f0304264907f9fced9802d58fae1cdd41899c1d..205647113fe85b9fa03824e50ced5f0cc8dfbc41 100644 (file)
@@ -714,6 +714,42 @@ func BenchmarkMapIter(b *testing.B) {
        b.Run("Key=int32/Elem=*int32", benchSizes(benchmarkMapIter[int32, *int32]))
 }
 
+func benchmarkMapIterLowLoad[K mapBenchmarkKeyType, E mapBenchmarkElemType](b *testing.B, n int) {
+       // Only insert one entry regardless of map size.
+       k := genValues[K](0, 1)
+       e := genValues[E](0, 1)
+
+       m := make(map[K]E, n)
+       for i := range k {
+               m[k[i]] = e[i]
+       }
+
+       iterations := iterCount(b, n)
+       sinkK := newSink[K]()
+       sinkE := newSink[E]()
+       b.ResetTimer()
+
+       for i := 0; i < iterations; i++ {
+               for k, e := range m {
+                       *sinkK = k
+                       *sinkE = e
+               }
+       }
+}
+
+func BenchmarkMapIterLowLoad(b *testing.B) {
+       b.Run("Key=int32/Elem=int32", benchSizes(benchmarkMapIterLowLoad[int32, int32]))
+       b.Run("Key=int64/Elem=int64", benchSizes(benchmarkMapIterLowLoad[int64, int64]))
+       b.Run("Key=string/Elem=string", benchSizes(benchmarkMapIterLowLoad[string, string]))
+       b.Run("Key=smallType/Elem=int32", benchSizes(benchmarkMapIterLowLoad[smallType, int32]))
+       b.Run("Key=mediumType/Elem=int32", benchSizes(benchmarkMapIterLowLoad[mediumType, int32]))
+       b.Run("Key=bigType/Elem=int32", benchSizes(benchmarkMapIterLowLoad[bigType, int32]))
+       b.Run("Key=bigType/Elem=bigType", benchSizes(benchmarkMapIterLowLoad[bigType, bigType]))
+       b.Run("Key=int32/Elem=bigType", benchSizes(benchmarkMapIterLowLoad[int32, bigType]))
+       b.Run("Key=*int32/Elem=int32", benchSizes(benchmarkMapIterLowLoad[*int32, int32]))
+       b.Run("Key=int32/Elem=*int32", benchSizes(benchmarkMapIterLowLoad[int32, *int32]))
+}
+
 func benchmarkMapAccessHit[K mapBenchmarkKeyType, E mapBenchmarkElemType](b *testing.B, n int) {
        if n == 0 {
                b.Skip("can't access empty map")