Skip to content

Commit 9f42ae0

Browse files
author
Dan Laine
authored
Use the maps package when possible (ava-labs#2647)
1 parent b12227c commit 9f42ae0

File tree

6 files changed

+19
-16
lines changed

6 files changed

+19
-16
lines changed

api/health/worker.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212

1313
"github.com/prometheus/client_golang/prometheus"
1414

15+
"golang.org/x/exp/maps"
16+
1517
"github.com/ava-labs/avalanchego/utils"
1618
)
1719

@@ -120,10 +122,7 @@ func (w *worker) runChecks(ctx context.Context) {
120122
// during this iteration. If [w.checks] is modified during this iteration of
121123
// [runChecks], then the added check will not be run until the next
122124
// iteration.
123-
checks := make(map[string]Checker, len(w.checks))
124-
for name, checker := range w.checks {
125-
checks[name] = checker
126-
}
125+
checks := maps.Clone(w.checks)
127126
w.checksLock.RUnlock()
128127

129128
var wg sync.WaitGroup

api/server/router.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"sync"
1111

1212
"github.com/gorilla/mux"
13+
14+
"github.com/ava-labs/avalanchego/utils/set"
1315
)
1416

1517
var (
@@ -22,15 +24,15 @@ type router struct {
2224
router *mux.Router
2325

2426
routeLock sync.Mutex
25-
reservedRoutes map[string]bool // Reserves routes so that there can't be alias that conflict
27+
reservedRoutes set.Set[string] // Reserves routes so that there can't be alias that conflict
2628
aliases map[string][]string // Maps a route to a set of reserved routes
2729
routes map[string]map[string]http.Handler // Maps routes to a handler
2830
}
2931

3032
func newRouter() *router {
3133
return &router{
3234
router: mux.NewRouter(),
33-
reservedRoutes: make(map[string]bool),
35+
reservedRoutes: set.Set[string]{},
3436
aliases: make(map[string][]string),
3537
routes: make(map[string]map[string]http.Handler),
3638
}
@@ -68,7 +70,7 @@ func (r *router) AddRouter(base, endpoint string, handler http.Handler) error {
6870
}
6971

7072
func (r *router) addRouter(base, endpoint string, handler http.Handler) error {
71-
if r.reservedRoutes[base] {
73+
if r.reservedRoutes.Contains(base) {
7274
return fmt.Errorf("couldn't route to %s as that route is either aliased or already maps to a handler", base)
7375
}
7476

@@ -113,13 +115,13 @@ func (r *router) AddAlias(base string, aliases ...string) error {
113115
defer r.routeLock.Unlock()
114116

115117
for _, alias := range aliases {
116-
if r.reservedRoutes[alias] {
118+
if r.reservedRoutes.Contains(alias) {
117119
return fmt.Errorf("couldn't alias to %s as that route is either already aliased or already maps to a handler", alias)
118120
}
119121
}
120122

121123
for _, alias := range aliases {
122-
r.reservedRoutes[alias] = true
124+
r.reservedRoutes.Add(alias)
123125
}
124126

125127
r.aliases[base] = append(r.aliases[base], aliases...)

snow/consensus/avalanche/topological.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111

1212
"go.uber.org/zap"
1313

14+
"golang.org/x/exp/maps"
15+
1416
"github.com/ava-labs/avalanchego/ids"
1517
"github.com/ava-labs/avalanchego/snow"
1618
"github.com/ava-labs/avalanchego/snow/choices"
@@ -315,9 +317,7 @@ func (ta *Topological) HealthCheck(ctx context.Context) (interface{}, error) {
315317
// the non-transitively applied votes. Also returns the list of leaf nodes.
316318
func (ta *Topological) calculateInDegree(responses bag.UniqueBag[ids.ID]) error {
317319
// Clear the kahn node set
318-
for k := range ta.kahnNodes {
319-
delete(ta.kahnNodes, k)
320-
}
320+
maps.Clear(ta.kahnNodes)
321321
// Clear the leaf set
322322
ta.leaves.Clear()
323323

utils/set/set.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ func (s Set[T]) CappedList(size int) []T {
134134

135135
// Equals returns true if the sets contain the same elements
136136
func (s Set[T]) Equals(other Set[T]) bool {
137+
// Using maps.Equals makes the build not work for some reason so do this
138+
// manually.
137139
if s.Len() != other.Len() {
138140
return false
139141
}

utils/sorting.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func IsSortedAndUniqueByHash[T ~[]byte](s []T) bool {
8383

8484
// Returns true iff the elements in [s] are unique.
8585
func IsUnique[T comparable](elts []T) bool {
86+
// Can't use set.Set because it'd be a circular import.
8687
asMap := make(map[T]struct{}, len(elts))
8788
for _, elt := range elts {
8889
if _, ok := asMap[elt]; ok {

vms/platformvm/service.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414

1515
"go.uber.org/zap"
1616

17+
"golang.org/x/exp/maps"
18+
1719
"github.com/ava-labs/avalanchego/api"
1820
"github.com/ava-labs/avalanchego/cache"
1921
"github.com/ava-labs/avalanchego/database"
@@ -288,10 +290,7 @@ utxoFor:
288290
response.UTXOIDs = append(response.UTXOIDs, &utxo.UTXOID)
289291
}
290292

291-
balances := map[ids.ID]uint64{}
292-
for assetID, amount := range lockedStakeables {
293-
balances[assetID] = amount
294-
}
293+
balances := maps.Clone(lockedStakeables)
295294
for assetID, amount := range lockedNotStakeables {
296295
newBalance, err := math.Add64(balances[assetID], amount)
297296
if err != nil {

0 commit comments

Comments
 (0)