Skip to content

Commit 6e9c56e

Browse files
prattmicgopherbot
authored andcommitted
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 <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> Auto-Submit: Michael Pratt <[email protected]>
1 parent 5e91059 commit 6e9c56e

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/runtime/map_benchmark_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,42 @@ func BenchmarkMapIter(b *testing.B) {
714714
b.Run("Key=int32/Elem=*int32", benchSizes(benchmarkMapIter[int32, *int32]))
715715
}
716716

717+
func benchmarkMapIterLowLoad[K mapBenchmarkKeyType, E mapBenchmarkElemType](b *testing.B, n int) {
718+
// Only insert one entry regardless of map size.
719+
k := genValues[K](0, 1)
720+
e := genValues[E](0, 1)
721+
722+
m := make(map[K]E, n)
723+
for i := range k {
724+
m[k[i]] = e[i]
725+
}
726+
727+
iterations := iterCount(b, n)
728+
sinkK := newSink[K]()
729+
sinkE := newSink[E]()
730+
b.ResetTimer()
731+
732+
for i := 0; i < iterations; i++ {
733+
for k, e := range m {
734+
*sinkK = k
735+
*sinkE = e
736+
}
737+
}
738+
}
739+
740+
func BenchmarkMapIterLowLoad(b *testing.B) {
741+
b.Run("Key=int32/Elem=int32", benchSizes(benchmarkMapIterLowLoad[int32, int32]))
742+
b.Run("Key=int64/Elem=int64", benchSizes(benchmarkMapIterLowLoad[int64, int64]))
743+
b.Run("Key=string/Elem=string", benchSizes(benchmarkMapIterLowLoad[string, string]))
744+
b.Run("Key=smallType/Elem=int32", benchSizes(benchmarkMapIterLowLoad[smallType, int32]))
745+
b.Run("Key=mediumType/Elem=int32", benchSizes(benchmarkMapIterLowLoad[mediumType, int32]))
746+
b.Run("Key=bigType/Elem=int32", benchSizes(benchmarkMapIterLowLoad[bigType, int32]))
747+
b.Run("Key=bigType/Elem=bigType", benchSizes(benchmarkMapIterLowLoad[bigType, bigType]))
748+
b.Run("Key=int32/Elem=bigType", benchSizes(benchmarkMapIterLowLoad[int32, bigType]))
749+
b.Run("Key=*int32/Elem=int32", benchSizes(benchmarkMapIterLowLoad[*int32, int32]))
750+
b.Run("Key=int32/Elem=*int32", benchSizes(benchmarkMapIterLowLoad[int32, *int32]))
751+
}
752+
717753
func benchmarkMapAccessHit[K mapBenchmarkKeyType, E mapBenchmarkElemType](b *testing.B, n int) {
718754
if n == 0 {
719755
b.Skip("can't access empty map")

0 commit comments

Comments
 (0)