Skip to content

Commit ecdcec1

Browse files
committed
runtime: additional map benchmarks for repeated lookups
For the future. Update #5147 R=khr, r CC=golang-dev https://golang.org/cl/8165044
1 parent 43e38d5 commit ecdcec1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/pkg/runtime/mapspeed_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ func BenchmarkSmallStrMap(b *testing.B) {
138138
_, _ = m[key]
139139
}
140140
}
141+
141142
func BenchmarkIntMap(b *testing.B) {
142143
m := make(map[int]bool)
143144
for i := 0; i < 8; i++ {
@@ -148,3 +149,25 @@ func BenchmarkIntMap(b *testing.B) {
148149
_, _ = m[7]
149150
}
150151
}
152+
153+
// Accessing the same keys in a row.
154+
func benchmarkRepeatedLookup(b *testing.B, lookupKeySize int) {
155+
m := make(map[string]bool)
156+
// At least bigger than a single bucket:
157+
for i := 0; i < 64; i++ {
158+
m[fmt.Sprintf("some key %d", i)] = true
159+
}
160+
base := strings.Repeat("x", lookupKeySize-1)
161+
key1 := base + "1"
162+
key2 := base + "2"
163+
b.ResetTimer()
164+
for i := 0; i < b.N/4; i++ {
165+
_ = m[key1]
166+
_ = m[key1]
167+
_ = m[key2]
168+
_ = m[key2]
169+
}
170+
}
171+
172+
func BenchmarkRepeatedLookupStrMapKey32(b *testing.B) { benchmarkRepeatedLookup(b, 32) }
173+
func BenchmarkRepeatedLookupStrMapKey1M(b *testing.B) { benchmarkRepeatedLookup(b, 1<<20) }

0 commit comments

Comments
 (0)