Modernize sort functions - #21
Open
JohnAkindipe wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refactor: replace deprecated
sortpackage calls withslicespackage equivalentsSummary
This PR modernises the sorting idioms used across the consensus library by replacing uses of the legacy
sortpackage with the type-safe, genericslicespackage introduced in Go 1.21. The module'sgo.modalready declaresgo 1.21, so no minimum-version bump is required.Code that previously called
sort.Slice,sort.Strings, orsort.Intshave been replaced with the equivalentslices.Sortcall. In each case the semantics are identical — ascending natural order — so there is no behavioural change.Motivation
The
sortpackage predates Go generics and requires either a concrete typed helper (sort.Ints,sort.Strings) or a hand-written comparator closure passed tosort.Slice. Both approaches are now considered legacy style.The
slicespackage (stdlib since Go 1.21) providesslices.Sort[S ~[]E, E cmp.Ordered], which is generic, avoids the closure overhead, and is more readable.Changes
consensus/internal/bft/util.gosort.Ints(followersSet)→slices.Sort(followersSet)Sorts the integer slice of follower node IDs inside
ComputeHierarchy.consensus/pkg/wal/util.gosort.Strings(walNames)→slices.Sort(walNames)Sorts WAL file names after suffix filtering in
dirReadWalNames.sort.Strings(walNames)→slices.Sort(walNames)Sorts WAL file names at the beginning of
checkWalFilesto enforce ordering before iteration.sort.Slice(indexes, func(i, j int) bool { return indexes[i] < indexes[j] })→slices.Sort(indexes)Sorts the collected
uint64WAL index values at the end ofcheckWalFiles. The comparator closure is eliminated entirely.consensus/pkg/consensus/consensus.gosort.Slice(sorted, func(i, j int) bool { return sorted[i] < sorted[j] })→slices.Sort(sorted)Sorts a copy of the node ID slice in the
sortNodeshelper. The comparator closure is eliminated entirely.consensus/pkg/api/metrics.gosort.Strings(labelNames)→slices.Sort(labelNames)Sorts metric label names in
makeStatsdFormatbefore building the statsd format string.sort.Strings(labelNames)→slices.Sort(labelNames)Sorts metric label names in
makeLabelNamesbefore appending them to the result slice.Import cleanup
In each of the four affected files the
"sort"import was replaced with"slices".Testing
No test changes are required. The replacement is a mechanical substitution with identical runtime semantics. Existing unit and integration tests continue to exercise the same code paths and provide the same coverage.