all: use slices.Collect with maps.Keys instead of xmaps.Keys

In Go 1.23, the standard maps.Keys helper was altered relative to xmaps.Keys
to return and iterator, which can be used with slices.Collect.

Also, Go 1.21 added the clear built-in, which replaces xmaps.Clear,
and is semantically more correct with respect to NaNs.

Updates #8632
Updates #12912
Updates #cleanup

Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
Joe Tsai
2024-10-11 11:41:09 -07:00
parent c763b7a7db
commit 8f86d4f8b9
14 changed files with 37 additions and 38 deletions

View File

@@ -5,12 +5,13 @@ package lru
import (
"bytes"
"maps"
"math/rand"
"slices"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
xmaps "golang.org/x/exp/maps"
)
func TestLRU(t *testing.T) {
@@ -75,7 +76,7 @@ func TestStressEvictions(t *testing.T) {
for len(vm) < numKeys {
vm[rand.Uint64()] = true
}
vals := xmaps.Keys(vm)
vals := slices.Collect(maps.Keys(vm))
c := Cache[uint64, bool]{
MaxEntries: cacheSize,
@@ -106,7 +107,7 @@ func TestStressBatchedEvictions(t *testing.T) {
for len(vm) < numKeys {
vm[rand.Uint64()] = true
}
vals := xmaps.Keys(vm)
vals := slices.Collect(maps.Keys(vm))
c := Cache[uint64, bool]{}

View File

@@ -8,7 +8,6 @@ import (
"testing"
qt "github.com/frankban/quicktest"
xmaps "golang.org/x/exp/maps"
)
func pair[A, B any](a A, b B) (out struct {
@@ -130,7 +129,7 @@ func Benchmark(b *testing.B) {
for range b.N {
testMap[strings.ToLower(key)] = testValue
}
xmaps.Clear(testMap)
clear(testMap)
})
b.Run("NoCase", func(b *testing.B) {
b.ReportAllocs()
@@ -138,7 +137,7 @@ func Benchmark(b *testing.B) {
for range b.N {
Set(testMap, key, testValue)
}
xmaps.Clear(testMap)
clear(testMap)
})
})
b.Run("Delete", func(b *testing.B) {

View File

@@ -5,11 +5,10 @@
package metrics
import (
"maps"
"strings"
"sync"
xmaps "golang.org/x/exp/maps"
"tailscale.com/syncs"
"tailscale.com/types/lazy"
"tailscale.com/util/clientmetric"
@@ -268,7 +267,7 @@ func SetHooksForTest(tb internal.TB, addMetric, setMetric metricFn) {
})
settingMetricsMu.Lock()
oldSettingMetricsMap := xmaps.Clone(settingMetricsMap)
oldSettingMetricsMap := maps.Clone(settingMetricsMap)
clear(settingMetricsMap)
settingMetricsMu.Unlock()
tb.Cleanup(func() {

View File

@@ -9,7 +9,6 @@ import (
"slices"
"strings"
xmaps "golang.org/x/exp/maps"
"tailscale.com/util/deephash"
)
@@ -24,7 +23,7 @@ type Snapshot struct {
// NewSnapshot returns a new [Snapshot] with the specified items and options.
func NewSnapshot(items map[Key]RawItem, opts ...SummaryOption) *Snapshot {
return &Snapshot{m: xmaps.Clone(items), sig: deephash.Hash(&items), summary: SummaryWith(opts...)}
return &Snapshot{m: maps.Clone(items), sig: deephash.Hash(&items), summary: SummaryWith(opts...)}
}
// All returns an iterator over policy settings in s. The iteration order is not
@@ -164,7 +163,7 @@ func MergeSnapshots(snapshot1, snapshot2 *Snapshot) *Snapshot {
return &Snapshot{snapshot2.m, snapshot2.sig, SummaryWith(summaryOpts...)}
}
m := make(map[Key]RawItem, snapshot1.Len()+snapshot2.Len())
xmaps.Copy(m, snapshot1.m)
xmaps.Copy(m, snapshot2.m) // snapshot2 has higher precedence
maps.Copy(m, snapshot1.m)
maps.Copy(m, snapshot2.m) // snapshot2 has higher precedence
return &Snapshot{m, deephash.Hash(&m), SummaryWith(summaryOpts...)}
}

View File

@@ -5,10 +5,11 @@ package source
import (
"fmt"
"maps"
"slices"
"sync"
"sync/atomic"
xmaps "golang.org/x/exp/maps"
"tailscale.com/util/mak"
"tailscale.com/util/set"
"tailscale.com/util/syspolicy/internal"
@@ -294,7 +295,7 @@ func (s *TestStore) Suspend() {
s.mu.Lock()
defer s.mu.Unlock()
if s.suspendCount++; s.suspendCount == 1 {
s.mw = xmaps.Clone(s.mr)
s.mw = maps.Clone(s.mr)
}
}
@@ -421,7 +422,7 @@ func (s *TestStore) notifyPolicyChanged() {
s.mu.RUnlock()
return
}
cbs := xmaps.Values(s.cbs)
cbs := slices.Collect(maps.Values(s.cbs))
s.mu.RUnlock()
var wg sync.WaitGroup