From 065fbdea3dfd80a71b8e331bb0ed027ded68d983 Mon Sep 17 00:00:00 2001 From: Lorain Date: Wed, 2 Oct 2024 13:33:27 +0100 Subject: [PATCH] replace sort.Slice with slices.Sort, slices.SortFunc Signed-off-by: Lorain --- confchange/restore_test.go | 4 ++-- quorum/majority.go | 18 +++++++++--------- raft.go | 4 ++-- raftpb/confstate.go | 4 ++-- tracker/progress.go | 6 ++---- tracker/tracker.go | 5 ++--- 6 files changed, 19 insertions(+), 22 deletions(-) diff --git a/confchange/restore_test.go b/confchange/restore_test.go index 2dbfd2ca..d14e86d3 100644 --- a/confchange/restore_test.go +++ b/confchange/restore_test.go @@ -17,7 +17,7 @@ package confchange import ( "math/rand" "reflect" - "sort" + "slices" "testing" "testing/quick" @@ -104,7 +104,7 @@ func TestRestore(t *testing.T) { cs.VotersOutgoing, cs.LearnersNext, } { - sort.Slice(sl, func(i, j int) bool { return sl[i] < sl[j] }) + slices.Sort(sl) } cs2 := chg.Tracker.ConfState() diff --git a/quorum/majority.go b/quorum/majority.go index 0c9c24ee..85b296aa 100644 --- a/quorum/majority.go +++ b/quorum/majority.go @@ -15,10 +15,10 @@ package quorum import ( + "cmp" "fmt" "math" "slices" - "sort" "strings" ) @@ -30,7 +30,7 @@ func (c MajorityConfig) String() string { for id := range c { sl = append(sl, id) } - sort.Slice(sl, func(i, j int) bool { return sl[i] < sl[j] }) + slices.Sort(sl) var buf strings.Builder buf.WriteByte('(') for i := range sl { @@ -68,11 +68,11 @@ func (c MajorityConfig) Describe(l AckedIndexer) string { } // Sort by index - sort.Slice(info, func(i, j int) bool { - if info[i].idx == info[j].idx { - return info[i].id < info[j].id + slices.SortFunc(info, func(a, b tup) int { + if n := cmp.Compare(a.idx, b.idx); n != 0 { + return n } - return info[i].idx < info[j].idx + return cmp.Compare(a.id, b.id) }) // Populate .bar. @@ -83,8 +83,8 @@ func (c MajorityConfig) Describe(l AckedIndexer) string { } // Sort by ID. - sort.Slice(info, func(i, j int) bool { - return info[i].id < info[j].id + slices.SortFunc(info, func(a, b tup) int { + return cmp.Compare(a.id, b.id) }) var buf strings.Builder @@ -109,7 +109,7 @@ func (c MajorityConfig) Slice() []uint64 { for id := range c { sl = append(sl, id) } - sort.Slice(sl, func(i, j int) bool { return sl[i] < sl[j] }) + slices.Sort(sl) return sl } diff --git a/raft.go b/raft.go index 079b81a5..94c2363d 100644 --- a/raft.go +++ b/raft.go @@ -21,7 +21,7 @@ import ( "fmt" "math" "math/big" - "sort" + "slices" "strings" "sync" @@ -1047,7 +1047,7 @@ func (r *raft) campaign(t CampaignType) { for id := range idMap { ids = append(ids, id) } - sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] }) + slices.Sort(ids) } for _, id := range ids { if id == r.id { diff --git a/raftpb/confstate.go b/raftpb/confstate.go index 39b9dd70..3aedeb79 100644 --- a/raftpb/confstate.go +++ b/raftpb/confstate.go @@ -17,7 +17,7 @@ package raftpb import ( "fmt" "reflect" - "sort" + "slices" ) // Equivalent returns a nil error if the inputs describe the same configuration. @@ -27,7 +27,7 @@ func (cs ConfState) Equivalent(cs2 ConfState) error { orig1, orig2 := cs1, cs2 s := func(sl *[]uint64) { *sl = append([]uint64(nil), *sl...) - sort.Slice(*sl, func(i, j int) bool { return (*sl)[i] < (*sl)[j] }) + slices.Sort(*sl) } for _, cs := range []*ConfState{&cs1, &cs2} { diff --git a/tracker/progress.go b/tracker/progress.go index b475002f..5716661c 100644 --- a/tracker/progress.go +++ b/tracker/progress.go @@ -16,7 +16,7 @@ package tracker import ( "fmt" - "sort" + "slices" "strings" ) @@ -305,9 +305,7 @@ func (m ProgressMap) String() string { for k := range m { ids = append(ids, k) } - sort.Slice(ids, func(i, j int) bool { - return ids[i] < ids[j] - }) + slices.Sort(ids) var buf strings.Builder for _, id := range ids { fmt.Fprintf(&buf, "%d: %s\n", id, m[id]) diff --git a/tracker/tracker.go b/tracker/tracker.go index 5c4e14ce..17c4c93f 100644 --- a/tracker/tracker.go +++ b/tracker/tracker.go @@ -17,7 +17,6 @@ package tracker import ( "fmt" "slices" - "sort" "strings" "go.etcd.io/raft/v3/quorum" @@ -225,7 +224,7 @@ func (p *ProgressTracker) VoterNodes() []uint64 { for id := range m { nodes = append(nodes, id) } - sort.Slice(nodes, func(i, j int) bool { return nodes[i] < nodes[j] }) + slices.Sort(nodes) return nodes } @@ -238,7 +237,7 @@ func (p *ProgressTracker) LearnerNodes() []uint64 { for id := range p.Learners { nodes = append(nodes, id) } - sort.Slice(nodes, func(i, j int) bool { return nodes[i] < nodes[j] }) + slices.Sort(nodes) return nodes }