Skip to content

Commit

Permalink
Use the maps package when possible (ava-labs#2647)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Laine authored Feb 24, 2023
1 parent b12227c commit 9f42ae0
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
7 changes: 3 additions & 4 deletions api/health/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

"github.com/prometheus/client_golang/prometheus"

"golang.org/x/exp/maps"

"github.com/ava-labs/avalanchego/utils"
)

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

var wg sync.WaitGroup
Expand Down
12 changes: 7 additions & 5 deletions api/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"sync"

"github.com/gorilla/mux"

"github.com/ava-labs/avalanchego/utils/set"
)

var (
Expand All @@ -22,15 +24,15 @@ type router struct {
router *mux.Router

routeLock sync.Mutex
reservedRoutes map[string]bool // Reserves routes so that there can't be alias that conflict
reservedRoutes set.Set[string] // Reserves routes so that there can't be alias that conflict
aliases map[string][]string // Maps a route to a set of reserved routes
routes map[string]map[string]http.Handler // Maps routes to a handler
}

func newRouter() *router {
return &router{
router: mux.NewRouter(),
reservedRoutes: make(map[string]bool),
reservedRoutes: set.Set[string]{},
aliases: make(map[string][]string),
routes: make(map[string]map[string]http.Handler),
}
Expand Down Expand Up @@ -68,7 +70,7 @@ func (r *router) AddRouter(base, endpoint string, handler http.Handler) error {
}

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

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

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

for _, alias := range aliases {
r.reservedRoutes[alias] = true
r.reservedRoutes.Add(alias)
}

r.aliases[base] = append(r.aliases[base], aliases...)
Expand Down
6 changes: 3 additions & 3 deletions snow/consensus/avalanche/topological.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

"go.uber.org/zap"

"golang.org/x/exp/maps"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/choices"
Expand Down Expand Up @@ -315,9 +317,7 @@ func (ta *Topological) HealthCheck(ctx context.Context) (interface{}, error) {
// the non-transitively applied votes. Also returns the list of leaf nodes.
func (ta *Topological) calculateInDegree(responses bag.UniqueBag[ids.ID]) error {
// Clear the kahn node set
for k := range ta.kahnNodes {
delete(ta.kahnNodes, k)
}
maps.Clear(ta.kahnNodes)
// Clear the leaf set
ta.leaves.Clear()

Expand Down
2 changes: 2 additions & 0 deletions utils/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ func (s Set[T]) CappedList(size int) []T {

// Equals returns true if the sets contain the same elements
func (s Set[T]) Equals(other Set[T]) bool {
// Using maps.Equals makes the build not work for some reason so do this
// manually.
if s.Len() != other.Len() {
return false
}
Expand Down
1 change: 1 addition & 0 deletions utils/sorting.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func IsSortedAndUniqueByHash[T ~[]byte](s []T) bool {

// Returns true iff the elements in [s] are unique.
func IsUnique[T comparable](elts []T) bool {
// Can't use set.Set because it'd be a circular import.
asMap := make(map[T]struct{}, len(elts))
for _, elt := range elts {
if _, ok := asMap[elt]; ok {
Expand Down
7 changes: 3 additions & 4 deletions vms/platformvm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

"go.uber.org/zap"

"golang.org/x/exp/maps"

"github.com/ava-labs/avalanchego/api"
"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/database"
Expand Down Expand Up @@ -288,10 +290,7 @@ utxoFor:
response.UTXOIDs = append(response.UTXOIDs, &utxo.UTXOID)
}

balances := map[ids.ID]uint64{}
for assetID, amount := range lockedStakeables {
balances[assetID] = amount
}
balances := maps.Clone(lockedStakeables)
for assetID, amount := range lockedNotStakeables {
newBalance, err := math.Add64(balances[assetID], amount)
if err != nil {
Expand Down

0 comments on commit 9f42ae0

Please sign in to comment.