From 3a3837f422e2b37c730793bdbeadcc0530e11368 Mon Sep 17 00:00:00 2001 From: Michael Anthony Knyszek Date: Sat, 18 May 2024 17:02:54 +0000 Subject: [PATCH] internal/concurrent: make HashTrieMap iteration more idiomatic Currently a HashTrieMap has a method called Enumerate whose method closure is an iter.Seq2, but the current convention is to name the method All and return an iter.Seq2. This is an easy transformation, so do it now. Change-Id: I323e505008b7df3a9e20fe8c223b281a8c290006 Reviewed-on: https://go-review.googlesource.com/c/go/+/586995 Reviewed-by: Michael Pratt LUCI-TryBot-Result: Go LUCI --- src/internal/concurrent/hashtriemap.go | 16 +++++++++------- src/internal/concurrent/hashtriemap_test.go | 12 ++++++------ src/unique/handle.go | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/internal/concurrent/hashtriemap.go b/src/internal/concurrent/hashtriemap.go index 5e31023494..4f7e730d4f 100644 --- a/src/internal/concurrent/hashtriemap.go +++ b/src/internal/concurrent/hashtriemap.go @@ -270,13 +270,15 @@ func (ht *HashTrieMap[K, V]) CompareAndDelete(key K, old V) (deleted bool) { return true } -// Enumerate produces all key-value pairs in the map. The enumeration does -// not represent any consistent snapshot of the map, but is guaranteed -// to visit each unique key-value pair only once. It is safe to operate -// on the tree during iteration. No particular enumeration order is -// guaranteed. -func (ht *HashTrieMap[K, V]) Enumerate(yield func(key K, value V) bool) { - ht.iter(ht.root, yield) +// All returns an iter.Seq2 that produces all key-value pairs in the map. +// The enumeration does not represent any consistent snapshot of the map, +// but is guaranteed to visit each unique key-value pair only once. It is +// safe to operate on the tree during iteration. No particular enumeration +// order is guaranteed. +func (ht *HashTrieMap[K, V]) All() func(yield func(K, V) bool) { + return func(yield func(key K, value V) bool) { + ht.iter(ht.root, yield) + } } func (ht *HashTrieMap[K, V]) iter(i *indirect[K, V], yield func(key K, value V) bool) bool { diff --git a/src/internal/concurrent/hashtriemap_test.go b/src/internal/concurrent/hashtriemap_test.go index 96f134c293..e233824c0f 100644 --- a/src/internal/concurrent/hashtriemap_test.go +++ b/src/internal/concurrent/hashtriemap_test.go @@ -119,17 +119,17 @@ func testHashTrieMap(t *testing.T, newMap func() *HashTrieMap[string, int]) { } } }) - t.Run("Enumerate", func(t *testing.T) { + t.Run("All", func(t *testing.T) { m := newMap() - testEnumerate(t, m, testDataMap(testData[:]), func(_ string, _ int) bool { + testAll(t, m, testDataMap(testData[:]), func(_ string, _ int) bool { return true }) }) - t.Run("EnumerateDelete", func(t *testing.T) { + t.Run("AllDelete", func(t *testing.T) { m := newMap() - testEnumerate(t, m, testDataMap(testData[:]), func(s string, i int) bool { + testAll(t, m, testDataMap(testData[:]), func(s string, i int) bool { expectDeleted(t, s, i)(m.CompareAndDelete(s, i)) return true }) @@ -200,12 +200,12 @@ func testHashTrieMap(t *testing.T, newMap func() *HashTrieMap[string, int]) { }) } -func testEnumerate[K, V comparable](t *testing.T, m *HashTrieMap[K, V], testData map[K]V, yield func(K, V) bool) { +func testAll[K, V comparable](t *testing.T, m *HashTrieMap[K, V], testData map[K]V, yield func(K, V) bool) { for k, v := range testData { expectStored(t, k, v)(m.LoadOrStore(k, v)) } visited := make(map[K]int) - m.Enumerate(func(key K, got V) bool { + m.All()(func(key K, got V) bool { want, ok := testData[key] if !ok { t.Errorf("unexpected key %v in map", key) diff --git a/src/unique/handle.go b/src/unique/handle.go index 4d9669162f..0842ae3185 100644 --- a/src/unique/handle.go +++ b/src/unique/handle.go @@ -126,7 +126,7 @@ func addUniqueMap[T comparable](typ *abi.Type) *uniqueMap[T] { cleanupFuncs = append(cleanupFuncs, func() { // Delete all the entries whose weak references are nil and clean up // deleted entries. - m.Enumerate(func(key T, wp weak.Pointer[T]) bool { + m.All()(func(key T, wp weak.Pointer[T]) bool { if wp.Strong() == nil { m.CompareAndDelete(key, wp) }