From: Michael Pratt Date: Mon, 11 Nov 2024 21:07:58 +0000 (-0500) Subject: runtime: add benchmark of iteration over map with low load X-Git-Tag: go1.24rc1~415 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=6e9c56e26b1ed26cb0dc81a6aeb974e675a9ce9e;p=gostls13.git runtime: add benchmark of iteration over map with low load Change-Id: I3a3b7da6245a18bf1db0c595008f0eea853ce544 Reviewed-on: https://go-review.googlesource.com/c/go/+/627155 Reviewed-by: Keith Randall LUCI-TryBot-Result: Go LUCI Reviewed-by: Keith Randall Auto-Submit: Michael Pratt --- diff --git a/src/runtime/map_benchmark_test.go b/src/runtime/map_benchmark_test.go index 5f03042649..205647113f 100644 --- a/src/runtime/map_benchmark_test.go +++ b/src/runtime/map_benchmark_test.go @@ -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")