Skip to content

Modernize sort functions - #21

Open
JohnAkindipe wants to merge 1 commit into
BiniWorld:binibftfrom
JohnAkindipe:update-sort-functions
Open

Modernize sort functions#21
JohnAkindipe wants to merge 1 commit into
BiniWorld:binibftfrom
JohnAkindipe:update-sort-functions

Conversation

@JohnAkindipe

Copy link
Copy Markdown

Refactor: replace deprecated sort package calls with slices package equivalents

Summary

This PR modernises the sorting idioms used across the consensus library by replacing uses of the legacy sort package with the type-safe, generic slices package introduced in Go 1.21. The module's go.mod already declares go 1.21, so no minimum-version bump is required.

Code that previously called sort.Slice, sort.Strings, or sort.Ints have been replaced with the equivalent slices.Sort call. In each case the semantics are identical — ascending natural order — so there is no behavioural change.


Motivation

The sort package predates Go generics and requires either a concrete typed helper (sort.Ints, sort.Strings) or a hand-written comparator closure passed to sort.Slice. Both approaches are now considered legacy style.

The slices package (stdlib since Go 1.21) provides slices.Sort[S ~[]E, E cmp.Ordered], which is generic, avoids the closure overhead, and is more readable.


Changes

consensus/internal/bft/util.go

  • Line 188sort.Ints(followersSet)slices.Sort(followersSet)
    Sorts the integer slice of follower node IDs inside ComputeHierarchy.

consensus/pkg/wal/util.go

  • Line 81sort.Strings(walNames)slices.Sort(walNames)
    Sorts WAL file names after suffix filtering in dirReadWalNames.
  • Line 89sort.Strings(walNames)slices.Sort(walNames)
    Sorts WAL file names at the beginning of checkWalFiles to enforce ordering before iteration.
  • Lines 135–139 — multi-line sort.Slice(indexes, func(i, j int) bool { return indexes[i] < indexes[j] })slices.Sort(indexes)
    Sorts the collected uint64 WAL index values at the end of checkWalFiles. The comparator closure is eliminated entirely.

consensus/pkg/consensus/consensus.go

  • Lines 380–384 — multi-line sort.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 sortNodes helper. The comparator closure is eliminated entirely.

consensus/pkg/api/metrics.go

  • Line 54sort.Strings(labelNames)slices.Sort(labelNames)
    Sorts metric label names in makeStatsdFormat before building the statsd format string.
  • Line 65sort.Strings(labelNames)slices.Sort(labelNames)
    Sorts metric label names in makeLabelNames before 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.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant