This is a draft for the sync.Map API proposed in golang/go#18177. It supports fast-path loads via an atomic variable, falling back to a Mutex for stores. In order to keep stores amortized to O(1), loads following a store follow the Mutex path until enough loads have occurred to offset the cost of a deep copy. For mostly-read loads, such as the maps in the reflect package in the standard library, this significantly reduces cache-line contention vs. a plain RWMutex with a map. goos: linux goarch: amd64 pkg: golang.org/x/sync/syncmap BenchmarkLoadMostlyHits/*syncmap_test.DeepCopyMap 20000000 73.1 ns/op BenchmarkLoadMostlyHits/*syncmap_test.DeepCopyMap-48 100000000 13.8 ns/op BenchmarkLoadMostlyHits/*syncmap_test.RWMutexMap 20000000 87.7 ns/op BenchmarkLoadMostlyHits/*syncmap_test.RWMutexMap-48 10000000 154 ns/op BenchmarkLoadMostlyHits/*syncmap.Map 20000000 72.1 ns/op BenchmarkLoadMostlyHits/*syncmap.Map-48 100000000 11.2 ns/op BenchmarkLoadMostlyMisses/*syncmap_test.DeepCopyMap 20000000 63.2 ns/op BenchmarkLoadMostlyMisses/*syncmap_test.DeepCopyMap-48 200000000 14.2 ns/op BenchmarkLoadMostlyMisses/*syncmap_test.RWMutexMap 20000000 72.7 ns/op BenchmarkLoadMostlyMisses/*syncmap_test.RWMutexMap-48 10000000 150 ns/op BenchmarkLoadMostlyMisses/*syncmap.Map 30000000 56.4 ns/op BenchmarkLoadMostlyMisses/*syncmap.Map-48 200000000 9.77 ns/op BenchmarkLoadOrStoreBalanced/*syncmap_test.RWMutexMap 2000000 683 ns/op BenchmarkLoadOrStoreBalanced/*syncmap_test.RWMutexMap-48 1000000 1394 ns/op BenchmarkLoadOrStoreBalanced/*syncmap.Map 2000000 645 ns/op BenchmarkLoadOrStoreBalanced/*syncmap.Map-48 1000000 1253 ns/op BenchmarkLoadOrStoreUnique/*syncmap_test.RWMutexMap 1000000 1015 ns/op BenchmarkLoadOrStoreUnique/*syncmap_test.RWMutexMap-48 1000000 1911 ns/op BenchmarkLoadOrStoreUnique/*syncmap.Map 1000000 1018 ns/op BenchmarkLoadOrStoreUnique/*syncmap.Map-48 1000000 1776 ns/op BenchmarkLoadOrStoreCollision/*syncmap_test.DeepCopyMap 50000000 30.2 ns/op BenchmarkLoadOrStoreCollision/*syncmap_test.DeepCopyMap-48 2000000000 1.24 ns/op BenchmarkLoadOrStoreCollision/*syncmap_test.RWMutexMap 30000000 50.1 ns/op BenchmarkLoadOrStoreCollision/*syncmap_test.RWMutexMap-48 5000000 451 ns/op BenchmarkLoadOrStoreCollision/*syncmap.Map 30000000 36.8 ns/op BenchmarkLoadOrStoreCollision/*syncmap.Map-48 2000000000 1.24 ns/op BenchmarkAdversarialAlloc/*syncmap_test.DeepCopyMap 10000000 213 ns/op BenchmarkAdversarialAlloc/*syncmap_test.DeepCopyMap-48 1000000 5012 ns/op BenchmarkAdversarialAlloc/*syncmap_test.RWMutexMap 20000000 68.8 ns/op BenchmarkAdversarialAlloc/*syncmap_test.RWMutexMap-48 5000000 429 ns/op BenchmarkAdversarialAlloc/*syncmap.Map 5000000 229 ns/op BenchmarkAdversarialAlloc/*syncmap.Map-48 2000000 600 ns/op BenchmarkAdversarialDelete/*syncmap_test.DeepCopyMap 5000000 314 ns/op BenchmarkAdversarialDelete/*syncmap_test.DeepCopyMap-48 2000000 726 ns/op BenchmarkAdversarialDelete/*syncmap_test.RWMutexMap 20000000 63.2 ns/op BenchmarkAdversarialDelete/*syncmap_test.RWMutexMap-48 5000000 469 ns/op BenchmarkAdversarialDelete/*syncmap.Map 10000000 203 ns/op BenchmarkAdversarialDelete/*syncmap.Map-48 10000000 253 ns/op goos: linux goarch: ppc64le pkg: golang.org/x/sync/syncmap BenchmarkLoadMostlyHits/*syncmap_test.DeepCopyMap 5000000 253 ns/op BenchmarkLoadMostlyHits/*syncmap_test.DeepCopyMap-48 50000000 26.2 ns/op BenchmarkLoadMostlyHits/*syncmap_test.RWMutexMap 5000000 505 ns/op BenchmarkLoadMostlyHits/*syncmap_test.RWMutexMap-48 3000000 443 ns/op BenchmarkLoadMostlyHits/*syncmap.Map 10000000 200 ns/op BenchmarkLoadMostlyHits/*syncmap.Map-48 100000000 18.1 ns/op BenchmarkLoadMostlyMisses/*syncmap_test.DeepCopyMap 10000000 162 ns/op BenchmarkLoadMostlyMisses/*syncmap_test.DeepCopyMap-48 100000000 23.8 ns/op BenchmarkLoadMostlyMisses/*syncmap_test.RWMutexMap 10000000 195 ns/op BenchmarkLoadMostlyMisses/*syncmap_test.RWMutexMap-48 3000000 531 ns/op BenchmarkLoadMostlyMisses/*syncmap.Map 10000000 182 ns/op BenchmarkLoadMostlyMisses/*syncmap.Map-48 100000000 15.8 ns/op BenchmarkLoadOrStoreBalanced/*syncmap_test.RWMutexMap 1000000 1664 ns/op BenchmarkLoadOrStoreBalanced/*syncmap_test.RWMutexMap-48 1000000 1768 ns/op BenchmarkLoadOrStoreBalanced/*syncmap.Map 1000000 2128 ns/op BenchmarkLoadOrStoreBalanced/*syncmap.Map-48 1000000 1903 ns/op BenchmarkLoadOrStoreUnique/*syncmap_test.RWMutexMap 1000000 2657 ns/op BenchmarkLoadOrStoreUnique/*syncmap_test.RWMutexMap-48 1000000 2577 ns/op BenchmarkLoadOrStoreUnique/*syncmap.Map 1000000 1714 ns/op BenchmarkLoadOrStoreUnique/*syncmap.Map-48 1000000 2484 ns/op BenchmarkLoadOrStoreCollision/*syncmap_test.DeepCopyMap 10000000 130 ns/op BenchmarkLoadOrStoreCollision/*syncmap_test.DeepCopyMap-48 100000000 11.3 ns/op BenchmarkLoadOrStoreCollision/*syncmap_test.RWMutexMap 3000000 426 ns/op BenchmarkLoadOrStoreCollision/*syncmap_test.RWMutexMap-48 2000000 930 ns/op BenchmarkLoadOrStoreCollision/*syncmap.Map 10000000 131 ns/op BenchmarkLoadOrStoreCollision/*syncmap.Map-48 300000000 4.07 ns/op BenchmarkAdversarialAlloc/*syncmap_test.DeepCopyMap 3000000 447 ns/op BenchmarkAdversarialAlloc/*syncmap_test.DeepCopyMap-48 300000 4159 ns/op BenchmarkAdversarialAlloc/*syncmap_test.RWMutexMap 10000000 191 ns/op BenchmarkAdversarialAlloc/*syncmap_test.RWMutexMap-48 3000000 535 ns/op BenchmarkAdversarialAlloc/*syncmap.Map 2000000 525 ns/op BenchmarkAdversarialAlloc/*syncmap.Map-48 1000000 1000 ns/op BenchmarkAdversarialDelete/*syncmap_test.DeepCopyMap 2000000 711 ns/op BenchmarkAdversarialDelete/*syncmap_test.DeepCopyMap-48 2000000 900 ns/op BenchmarkAdversarialDelete/*syncmap_test.RWMutexMap 3000000 354 ns/op BenchmarkAdversarialDelete/*syncmap_test.RWMutexMap-48 3000000 473 ns/op BenchmarkAdversarialDelete/*syncmap.Map 2000000 1357 ns/op BenchmarkAdversarialDelete/*syncmap.Map-48 5000000 334 ns/op Updates golang/go#18177 Change-Id: I8d561b617b1cd2ca03a8e68a5d5a28a519a0ce38 Reviewed-on: https://go-review.googlesource.com/33912 Reviewed-by: Russ Cox <rsc@golang.org>
199 lines
4.5 KiB
Go
199 lines
4.5 KiB
Go
// Copyright 2016 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package syncmap_test
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"golang.org/x/sync/syncmap"
|
|
)
|
|
|
|
type bench struct {
|
|
setup func(*testing.B, mapInterface)
|
|
perG func(b *testing.B, pb *testing.PB, i int, m mapInterface)
|
|
}
|
|
|
|
func benchMap(b *testing.B, bench bench) {
|
|
for _, m := range [...]mapInterface{&DeepCopyMap{}, &RWMutexMap{}, &syncmap.Map{}} {
|
|
b.Run(fmt.Sprintf("%T", m), func(b *testing.B) {
|
|
m = reflect.New(reflect.TypeOf(m).Elem()).Interface().(mapInterface)
|
|
if bench.setup != nil {
|
|
bench.setup(b, m)
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
var i int64
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
id := int(atomic.AddInt64(&i, 1) - 1)
|
|
bench.perG(b, pb, id*b.N, m)
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
func BenchmarkLoadMostlyHits(b *testing.B) {
|
|
const hits, misses = 1023, 1
|
|
|
|
benchMap(b, bench{
|
|
setup: func(_ *testing.B, m mapInterface) {
|
|
for i := 0; i < hits; i++ {
|
|
m.LoadOrStore(i, i)
|
|
}
|
|
// Prime the map to get it into a steady state.
|
|
for i := 0; i < hits*2; i++ {
|
|
m.Load(i % hits)
|
|
}
|
|
},
|
|
|
|
perG: func(b *testing.B, pb *testing.PB, i int, m mapInterface) {
|
|
for ; pb.Next(); i++ {
|
|
m.Load(i % (hits + misses))
|
|
}
|
|
},
|
|
})
|
|
}
|
|
|
|
func BenchmarkLoadMostlyMisses(b *testing.B) {
|
|
const hits, misses = 1, 1023
|
|
|
|
benchMap(b, bench{
|
|
setup: func(_ *testing.B, m mapInterface) {
|
|
for i := 0; i < hits; i++ {
|
|
m.LoadOrStore(i, i)
|
|
}
|
|
// Prime the map to get it into a steady state.
|
|
for i := 0; i < hits*2; i++ {
|
|
m.Load(i % hits)
|
|
}
|
|
},
|
|
|
|
perG: func(b *testing.B, pb *testing.PB, i int, m mapInterface) {
|
|
for ; pb.Next(); i++ {
|
|
m.Load(i % (hits + misses))
|
|
}
|
|
},
|
|
})
|
|
}
|
|
|
|
func BenchmarkLoadOrStoreBalanced(b *testing.B) {
|
|
const hits, misses = 128, 128
|
|
|
|
benchMap(b, bench{
|
|
setup: func(b *testing.B, m mapInterface) {
|
|
if _, ok := m.(*DeepCopyMap); ok {
|
|
b.Skip("DeepCopyMap has quadratic running time.")
|
|
}
|
|
for i := 0; i < hits; i++ {
|
|
m.LoadOrStore(i, i)
|
|
}
|
|
// Prime the map to get it into a steady state.
|
|
for i := 0; i < hits*2; i++ {
|
|
m.Load(i % hits)
|
|
}
|
|
},
|
|
|
|
perG: func(b *testing.B, pb *testing.PB, i int, m mapInterface) {
|
|
for ; pb.Next(); i++ {
|
|
j := i % (hits + misses)
|
|
if j < hits {
|
|
if _, ok := m.LoadOrStore(j, i); !ok {
|
|
b.Fatalf("unexpected miss for %v", j)
|
|
}
|
|
} else {
|
|
if v, loaded := m.LoadOrStore(i, i); loaded {
|
|
b.Fatalf("failed to store %v: existing value %v", i, v)
|
|
}
|
|
}
|
|
}
|
|
},
|
|
})
|
|
}
|
|
|
|
func BenchmarkLoadOrStoreUnique(b *testing.B) {
|
|
benchMap(b, bench{
|
|
setup: func(b *testing.B, m mapInterface) {
|
|
if _, ok := m.(*DeepCopyMap); ok {
|
|
b.Skip("DeepCopyMap has quadratic running time.")
|
|
}
|
|
},
|
|
|
|
perG: func(b *testing.B, pb *testing.PB, i int, m mapInterface) {
|
|
for ; pb.Next(); i++ {
|
|
m.LoadOrStore(i, i)
|
|
}
|
|
},
|
|
})
|
|
}
|
|
|
|
func BenchmarkLoadOrStoreCollision(b *testing.B) {
|
|
benchMap(b, bench{
|
|
setup: func(_ *testing.B, m mapInterface) {
|
|
m.LoadOrStore(0, 0)
|
|
},
|
|
|
|
perG: func(b *testing.B, pb *testing.PB, i int, m mapInterface) {
|
|
for ; pb.Next(); i++ {
|
|
m.LoadOrStore(0, 0)
|
|
}
|
|
},
|
|
})
|
|
}
|
|
|
|
// BenchmarkAdversarialAlloc tests performance when we store a new value
|
|
// immediately whenever the map is promoted to clean.
|
|
//
|
|
// This forces the Load calls to always acquire the map's mutex.
|
|
func BenchmarkAdversarialAlloc(b *testing.B) {
|
|
benchMap(b, bench{
|
|
perG: func(b *testing.B, pb *testing.PB, i int, m mapInterface) {
|
|
var stores, loadsSinceStore int64
|
|
for ; pb.Next(); i++ {
|
|
m.Load(i)
|
|
if loadsSinceStore++; loadsSinceStore > stores {
|
|
m.LoadOrStore(i, stores)
|
|
loadsSinceStore = 0
|
|
stores++
|
|
}
|
|
}
|
|
},
|
|
})
|
|
}
|
|
|
|
// BenchmarkAdversarialDelete tests performance when we delete and restore a
|
|
// value immediately after a large map has been promoted.
|
|
//
|
|
// This forces the Load calls to always acquire the map's mutex and periodically
|
|
// makes a full copy of the map despite changing only one entry.
|
|
func BenchmarkAdversarialDelete(b *testing.B) {
|
|
const mapSize = 1 << 10
|
|
|
|
benchMap(b, bench{
|
|
setup: func(_ *testing.B, m mapInterface) {
|
|
for i := 0; i < mapSize; i++ {
|
|
m.Store(i, i)
|
|
}
|
|
},
|
|
|
|
perG: func(b *testing.B, pb *testing.PB, i int, m mapInterface) {
|
|
for ; pb.Next(); i++ {
|
|
m.Load(i)
|
|
|
|
if i%mapSize == 0 {
|
|
var key int
|
|
m.Range(func(k, _ interface{}) bool {
|
|
key = k.(int)
|
|
return false
|
|
})
|
|
m.Delete(key)
|
|
m.Store(key, key)
|
|
}
|
|
}
|
|
},
|
|
})
|
|
}
|