Skip to content

Commit a997f26

Browse files
committed
refactor: replace sort.SliceStable with slices.SortStableFunc
This commit replaces all the instances of sort.SliceStable with slices.SortStableFunc. The godocs for sort.SliceStable recommends using slices.SortStableFunc, as it is faster and more efficient.
1 parent 40c894f commit a997f26

File tree

15 files changed

+131
-72
lines changed

15 files changed

+131
-72
lines changed

cmd/clusterctl/client/repository/components.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package repository
1818

1919
import (
2020
"fmt"
21-
"sort"
21+
"slices"
2222
"strings"
2323

2424
"github.com/pkg/errors"
@@ -262,17 +262,20 @@ func NewComponents(input ComponentsInput) (Components, error) {
262262
// Deploying cert-manager objects and especially Certificates before Mutating-
263263
// ValidatingWebhookConfigurations and CRDs ensures cert-manager's ca-injector
264264
// receives the event for the objects at the right time to inject the new CA.
265-
sort.SliceStable(objs, func(i, j int) bool {
265+
slices.SortStableFunc(objs, func(i, j unstructured.Unstructured) int {
266266
// First prioritize Namespaces over everything.
267-
if objs[i].GetKind() == "Namespace" {
268-
return true
267+
if i.GetKind() == "Namespace" {
268+
return -1
269269
}
270-
if objs[j].GetKind() == "Namespace" {
271-
return false
270+
if j.GetKind() == "Namespace" {
271+
return 1
272272
}
273273

274274
// Second prioritize cert-manager objects.
275-
return objs[i].GroupVersionKind().Group == "cert-manager.io"
275+
if i.GroupVersionKind().Group == "cert-manager.io" {
276+
return -1
277+
}
278+
return 1
276279
})
277280

278281
return &components{

cmd/clusterctl/client/repository/repository_versions.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package repository
1818

1919
import (
2020
"context"
21-
"sort"
21+
"slices"
2222

2323
"github.com/pkg/errors"
2424
"k8s.io/apimachinery/pkg/runtime"
@@ -114,17 +114,20 @@ func latestPatchRelease(ctx context.Context, repo Repository, major, minor *int3
114114
}
115115

116116
// Sort parsed versions by semantic version order.
117-
sort.SliceStable(versionCandidates, func(i, j int) bool {
117+
slices.SortStableFunc(versionCandidates, func(i, j *version.Version) int {
118118
// Prioritize release versions over pre-releases. For example v1.0.0 > v2.0.0-alpha
119119
// If both are pre-releases, sort by semantic version order as usual.
120-
if versionCandidates[j].PreRelease() == "" && versionCandidates[i].PreRelease() != "" {
121-
return false
120+
if j.PreRelease() == "" && i.PreRelease() != "" {
121+
return 1
122122
}
123-
if versionCandidates[i].PreRelease() == "" && versionCandidates[j].PreRelease() != "" {
124-
return true
123+
if i.PreRelease() == "" && j.PreRelease() != "" {
124+
return -1
125125
}
126126

127-
return versionCandidates[j].LessThan(versionCandidates[i])
127+
if j.LessThan(i) {
128+
return -1
129+
}
130+
return 1
128131
})
129132

130133
// Limit the number of searchable versions by 5.

controlplane/kubeadm/internal/workload_cluster_conditions.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,9 +1070,12 @@ func aggregateConditionsFromMachinesToKCP(input aggregateConditionsFromMachinesT
10701070
messageIndex = append(messageIndex, m)
10711071
}
10721072

1073-
sort.SliceStable(messageIndex, func(i, j int) bool {
1074-
return len(messageMap[messageIndex[i]]) > len(messageMap[messageIndex[j]]) ||
1075-
(len(messageMap[messageIndex[i]]) == len(messageMap[messageIndex[j]]) && strings.Join(messageMap[messageIndex[i]], ",") < strings.Join(messageMap[messageIndex[j]], ","))
1073+
slices.SortStableFunc(messageIndex, func(i, j string) int {
1074+
if len(messageMap[i]) > len(messageMap[j]) ||
1075+
(len(messageMap[i]) == len(messageMap[j]) && strings.Join(messageMap[i], ",") < strings.Join(messageMap[j], ",")) {
1076+
return -1
1077+
}
1078+
return 1
10761079
})
10771080

10781081
// Build the message

internal/controllers/clusterclass/clusterclass_controller.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"reflect"
23-
"sort"
23+
"slices"
2424
"strings"
2525

2626
"github.com/pkg/errors"
@@ -364,8 +364,11 @@ func (r *Reconciler) reconcileVariables(ctx context.Context, s *scope) (ctrl.Res
364364
// Alphabetically sort the variables by name. This ensures no unnecessary updates to the ClusterClass status.
365365
// Note: Definitions in `statusVarList[i].Definitions` have a stable order as they are added in a deterministic order
366366
// and are always held in an array.
367-
sort.SliceStable(statusVarList, func(i, j int) bool {
368-
return statusVarList[i].Name < statusVarList[j].Name
367+
slices.SortStableFunc(statusVarList, func(i, j clusterv1.ClusterClassStatusVariable) int {
368+
if i.Name < j.Name {
369+
return -1
370+
}
371+
return 1
369372
})
370373
clusterClass.Status.Variables = statusVarList
371374

internal/controllers/machineset/machineset_controller.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"context"
2222
"fmt"
2323
"math"
24-
"sort"
24+
"slices"
2525
"strings"
2626
"time"
2727

@@ -1602,17 +1602,23 @@ func (r *Reconciler) reconcileExternalTemplateReference(ctx context.Context, clu
16021602
// - Machines failing to come up first because
16031603
// there is a chance that they are not hosting any workloads (minimize disruption).
16041604
func sortMachinesToRemediate(machines []*clusterv1.Machine) {
1605-
sort.SliceStable(machines, func(i, j int) bool {
1606-
if annotations.HasRemediateMachine(machines[i]) && !annotations.HasRemediateMachine(machines[j]) {
1607-
return true
1605+
slices.SortStableFunc(machines, func(i, j *clusterv1.Machine) int {
1606+
if annotations.HasRemediateMachine(i) && !annotations.HasRemediateMachine(j) {
1607+
return -1
16081608
}
1609-
if !annotations.HasRemediateMachine(machines[i]) && annotations.HasRemediateMachine(machines[j]) {
1610-
return false
1609+
if !annotations.HasRemediateMachine(i) && annotations.HasRemediateMachine(j) {
1610+
return 1
16111611
}
16121612
// Use newest (and Name) as a tie-breaker criteria.
1613-
if machines[i].CreationTimestamp.Equal(&machines[j].CreationTimestamp) {
1614-
return machines[i].Name < machines[j].Name
1613+
if i.CreationTimestamp.Equal(&j.CreationTimestamp) {
1614+
if i.Name < j.Name {
1615+
return -1
1616+
}
1617+
return 1
1618+
}
1619+
if i.CreationTimestamp.After(j.CreationTimestamp.Time) {
1620+
return -1
16151621
}
1616-
return machines[i].CreationTimestamp.After(machines[j].CreationTimestamp.Time)
1622+
return 1
16171623
})
16181624
}

internal/controllers/machineset/machineset_controller_test.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package machineset
1919
import (
2020
"context"
2121
"fmt"
22-
"sort"
22+
"slices"
2323
"strings"
2424
"testing"
2525
"time"
@@ -3140,8 +3140,11 @@ func TestSortMachinesToRemediate(t *testing.T) {
31403140
machines := make([]*clusterv1.Machine, len(unhealthyMachines))
31413141
copy(machines, unhealthyMachines)
31423142
sortMachinesToRemediate(machines)
3143-
sort.SliceStable(unhealthyMachines, func(i, j int) bool {
3144-
return unhealthyMachines[i].CreationTimestamp.After(unhealthyMachines[j].CreationTimestamp.Time)
3143+
slices.SortStableFunc(unhealthyMachines, func(i, j *clusterv1.Machine) int {
3144+
if i.CreationTimestamp.After(j.CreationTimestamp.Time) {
3145+
return -1
3146+
}
3147+
return 1
31453148
})
31463149
g.Expect(unhealthyMachines).To(Equal(machines))
31473150
})
@@ -3154,11 +3157,17 @@ func TestSortMachinesToRemediate(t *testing.T) {
31543157
machines = append(machines, unhealthyMachinesWithAnnotations...)
31553158
sortMachinesToRemediate(machines)
31563159

3157-
sort.SliceStable(unhealthyMachines, func(i, j int) bool {
3158-
return unhealthyMachines[i].CreationTimestamp.After(unhealthyMachines[j].CreationTimestamp.Time)
3160+
slices.SortStableFunc(unhealthyMachines, func(i, j *clusterv1.Machine) int {
3161+
if i.CreationTimestamp.After(j.CreationTimestamp.Time) {
3162+
return -1
3163+
}
3164+
return 1
31593165
})
3160-
sort.SliceStable(unhealthyMachinesWithAnnotations, func(i, j int) bool {
3161-
return unhealthyMachinesWithAnnotations[i].CreationTimestamp.After(unhealthyMachinesWithAnnotations[j].CreationTimestamp.Time)
3166+
slices.SortStableFunc(unhealthyMachinesWithAnnotations, func(i, j *clusterv1.Machine) int {
3167+
if i.CreationTimestamp.After(j.CreationTimestamp.Time) {
3168+
return -1
3169+
}
3170+
return 1
31623171
})
31633172
g.Expect(machines).To(Equal(append(unhealthyMachinesWithAnnotations, unhealthyMachines...)))
31643173
})

test/framework/clusterctl/e2e_config.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"regexp"
2828
"runtime"
2929
"slices"
30-
"sort"
3130
"strconv"
3231
"strings"
3332
"time"
@@ -367,17 +366,20 @@ func resolveReleaseMarker(ctx context.Context, releaseMarker string, goproxyClie
367366
}
368367

369368
// Sort parsed versions by semantic version order.
370-
sort.SliceStable(versionCandidates, func(i, j int) bool {
369+
slices.SortStableFunc(versionCandidates, func(i, j semver.Version) int {
371370
// Prioritize pre-release versions over releases. For example v2.0.0-alpha > v1.0.0
372371
// If both are pre-releases, sort by semantic version order as usual.
373-
if len(versionCandidates[i].Pre) == 0 && len(versionCandidates[j].Pre) > 0 {
374-
return false
372+
if len(i.Pre) == 0 && len(j.Pre) > 0 {
373+
return 1
375374
}
376-
if len(versionCandidates[j].Pre) == 0 && len(versionCandidates[i].Pre) > 0 {
377-
return true
375+
if len(j.Pre) == 0 && len(i.Pre) > 0 {
376+
return -1
378377
}
379378

380-
return versionCandidates[j].LT(versionCandidates[i])
379+
if j.LT(i) {
380+
return -1
381+
}
382+
return 1
381383
})
382384

383385
// Limit the number of searchable versions by 5.

test/infrastructure/docker/exp/internal/controllers/dockermachinepool_controller_phases.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"context"
2222
"fmt"
2323
"math/rand"
24-
"sort"
24+
"slices"
2525

2626
"github.com/blang/semver/v4"
2727
"github.com/pkg/errors"
@@ -339,10 +339,13 @@ func (r *DockerMachinePoolReconciler) propagateMachineDeleteAnnotation(ctx conte
339339
// orderByDeleteMachineAnnotation will sort DockerMachines with the clusterv1.DeleteMachineAnnotation to the front of the list.
340340
// It will preserve the existing order of the list otherwise so that it respects the existing delete priority otherwise.
341341
func orderByDeleteMachineAnnotation(machines []infrav1.DockerMachine) []infrav1.DockerMachine {
342-
sort.SliceStable(machines, func(i, _ int) bool {
343-
_, iHasAnnotation := machines[i].Annotations[clusterv1.DeleteMachineAnnotation]
342+
slices.SortStableFunc(machines, func(i, _ infrav1.DockerMachine) int {
343+
_, iHasAnnotation := i.Annotations[clusterv1.DeleteMachineAnnotation]
344344

345-
return iHasAnnotation
345+
if iHasAnnotation {
346+
return -1
347+
}
348+
return 1
346349
})
347350

348351
return machines

util/conditions/merge_strategies.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,14 @@ func sortConditions(conditions []ConditionWithOwnerInfo, orderedConditionTypes [
327327
conditionOrder[conditionType] = i
328328
}
329329

330-
sort.SliceStable(conditions, func(i, j int) bool {
330+
slices.SortStableFunc(conditions, func(i, j ConditionWithOwnerInfo) int {
331331
// Sort by condition order (user defined, useful when computing summary of different conditions from the same object)
332-
return conditionOrder[conditions[i].Type] < conditionOrder[conditions[j].Type] ||
332+
if conditionOrder[i.Type] < conditionOrder[j.Type] ||
333333
// If same condition order, sort by last transition time (useful when computing aggregation of the same conditions from different objects)
334-
(conditionOrder[conditions[i].Type] == conditionOrder[conditions[j].Type] && conditions[i].LastTransitionTime.Before(&conditions[j].LastTransitionTime))
334+
(conditionOrder[i.Type] == conditionOrder[j.Type] && i.LastTransitionTime.Before(&j.LastTransitionTime)) {
335+
return -1
336+
}
337+
return 1
335338
})
336339
}
337340

@@ -463,8 +466,11 @@ func aggregateMessages(conditions []ConditionWithOwnerInfo, n *int, dropEmpty bo
463466
messageIndex = append(messageIndex, m)
464467
}
465468

466-
sort.SliceStable(messageIndex, func(i, j int) bool {
467-
return sortMessage(messageIndex[i], messageIndex[j], messageMustGoFirst, messagePriorityMap, messageObjMapForKind)
469+
slices.SortStableFunc(messageIndex, func(i, j string) int {
470+
if sortMessage(i, j, messageMustGoFirst, messagePriorityMap, messageObjMapForKind) {
471+
return -1
472+
}
473+
return 1
468474
})
469475

470476
// Pick the first n messages, decrement n.

util/conditions/patch.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package conditions
1818

1919
import (
2020
"reflect"
21-
"sort"
21+
"slices"
2222

2323
"github.com/google/go-cmp/cmp"
2424
"github.com/pkg/errors"
@@ -217,8 +217,11 @@ func (p Patch) Apply(latest Setter, opts ...PatchApplyOption) error {
217217
}
218218

219219
if applyOpt.conditionSortFunc != nil {
220-
sort.SliceStable(latestConditions, func(i, j int) bool {
221-
return applyOpt.conditionSortFunc(latestConditions[i], latestConditions[j])
220+
slices.SortStableFunc(latestConditions, func(i, j metav1.Condition) int {
221+
if applyOpt.conditionSortFunc(i, j) {
222+
return -1
223+
}
224+
return 1
222225
})
223226
}
224227

0 commit comments

Comments
 (0)