syncmap: rename DeepCopyMap to CopyMap Also, remove unnecessary variable Change-Id: Ic2a8966b14cd4dab4e7e8fcd3496f10940e9b1f7 Reviewed-on: https://go-review.googlesource.com/c/sync/+/848685 Reviewed-by: Carlos Amedee <carlos@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com> Auto-Submit: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/singleflight/singleflight_test.go b/singleflight/singleflight_test.go index 9d8edc8..921adac 100644 --- a/singleflight/singleflight_test.go +++ b/singleflight/singleflight_test.go
@@ -46,8 +46,6 @@ } for _, tc := range testCases { - tc := tc - t.Run(tc.name, func(t *testing.T) { t.Parallel()
diff --git a/syncmap/map_bench_test.go b/syncmap/map_bench_test.go index 9fb7181..dc5ea88 100644 --- a/syncmap/map_bench_test.go +++ b/syncmap/map_bench_test.go
@@ -19,7 +19,7 @@ } func benchMap(b *testing.B, bench bench) { - for _, m := range [...]mapInterface{&DeepCopyMap{}, &RWMutexMap{}, &syncmap.Map{}} { + for _, m := range [...]mapInterface{&CopyMap{}, &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 { @@ -86,7 +86,7 @@ benchMap(b, bench{ setup: func(b *testing.B, m mapInterface) { - if _, ok := m.(*DeepCopyMap); ok { + if _, ok := m.(*CopyMap); ok { b.Skip("DeepCopyMap has quadratic running time.") } for i := 0; i < hits; i++ { @@ -118,7 +118,7 @@ func BenchmarkLoadOrStoreUnique(b *testing.B) { benchMap(b, bench{ setup: func(b *testing.B, m mapInterface) { - if _, ok := m.(*DeepCopyMap); ok { + if _, ok := m.(*CopyMap); ok { b.Skip("DeepCopyMap has quadratic running time.") } },
diff --git a/syncmap/map_reference_test.go b/syncmap/map_reference_test.go index f9b4dcf..59d4d9b 100644 --- a/syncmap/map_reference_test.go +++ b/syncmap/map_reference_test.go
@@ -81,21 +81,21 @@ } } -// DeepCopyMap is an implementation of mapInterface using a Mutex and -// atomic.Value. It makes deep copies of the map on every write to avoid -// acquiring the Mutex in Load. -type DeepCopyMap struct { +// CopyMap is an implementation of mapInterface using a Mutex and atomic.Value. +// Each update operation makes a copy of the map (a functional update) so that +// no published map is mutated. This avoids acquiring the Mutex in Load and Range. +type CopyMap struct { mu sync.Mutex clean atomic.Value } -func (m *DeepCopyMap) Load(key any) (value any, ok bool) { +func (m *CopyMap) Load(key any) (value any, ok bool) { clean, _ := m.clean.Load().(map[any]any) value, ok = clean[key] return value, ok } -func (m *DeepCopyMap) Store(key, value any) { +func (m *CopyMap) Store(key, value any) { m.mu.Lock() dirty := m.dirty() dirty[key] = value @@ -103,7 +103,7 @@ m.mu.Unlock() } -func (m *DeepCopyMap) LoadOrStore(key, value any) (actual any, loaded bool) { +func (m *CopyMap) LoadOrStore(key, value any) (actual any, loaded bool) { clean, _ := m.clean.Load().(map[any]any) actual, loaded = clean[key] if loaded { @@ -124,7 +124,7 @@ return actual, loaded } -func (m *DeepCopyMap) Delete(key any) { +func (m *CopyMap) Delete(key any) { m.mu.Lock() dirty := m.dirty() delete(dirty, key) @@ -132,7 +132,7 @@ m.mu.Unlock() } -func (m *DeepCopyMap) Range(f func(key, value any) (shouldContinue bool)) { +func (m *CopyMap) Range(f func(key, value any) (shouldContinue bool)) { clean, _ := m.clean.Load().(map[any]any) for k, v := range clean { if !f(k, v) { @@ -141,7 +141,7 @@ } } -func (m *DeepCopyMap) dirty() map[any]any { +func (m *CopyMap) dirty() map[any]any { clean, _ := m.clean.Load().(map[any]any) dirty := make(map[any]any, len(clean)+1) for k, v := range clean {
diff --git a/syncmap/map_test.go b/syncmap/map_test.go index 912d09c..1f2c337 100644 --- a/syncmap/map_test.go +++ b/syncmap/map_test.go
@@ -95,7 +95,7 @@ } func applyDeepCopyMap(calls []mapCall) ([]mapResult, map[any]any) { - return applyCalls(new(DeepCopyMap), calls) + return applyCalls(new(CopyMap), calls) } func TestMapMatchesRWMutex(t *testing.T) {