Skip to content

Commit cdc71fc

Browse files
refactor: Modernize sorting (#1947)
Replaced sort.Slice and sort.SliceStable with the more modern and performant slices.Sort and slices.SortStable. Also fixed some unrelated linting errors. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent ea9bbe9 commit cdc71fc

File tree

8 files changed

+78
-27
lines changed

8 files changed

+78
-27
lines changed

go.work.sum

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ cloud.google.com/go v0.118.3/go.mod h1:Lhs3YLnBlwJ4KA6nuObNMZ/fCbOQBPuWKPoE0Wa/9
1919
cloud.google.com/go v0.120.0/go.mod h1:/beW32s8/pGRuj4IILWQNd4uuebeT4dkOhKmkfit64Q=
2020
cloud.google.com/go v0.120.1/go.mod h1:56Vs7sf/i2jYM6ZL9NYlC82r04PThNcPS5YgFmb0rp8=
2121
cloud.google.com/go v0.121.1/go.mod h1:nRFlrHq39MNVWu+zESP2PosMWA0ryJw8KUBZ2iZpxbw=
22+
cloud.google.com/go v0.121.2/go.mod h1:nRFlrHq39MNVWu+zESP2PosMWA0ryJw8KUBZ2iZpxbw=
2223
cloud.google.com/go/accessapproval v1.8.3 h1:axlU03FRiXDNupsmPG7LKzuS4Enk1gf598M62lWVB74=
2324
cloud.google.com/go/accessapproval v1.8.3/go.mod h1:3speETyAv63TDrDmo5lIkpVueFkQcQchkiw/TAMbBo4=
2425
cloud.google.com/go/accessapproval v1.8.6 h1:UkmDPCKvj24bkGVrvgJPcgSDkmIPw/bAmOiDb9avOiE=
@@ -87,6 +88,7 @@ cloud.google.com/go/auth v0.14.0/go.mod h1:CYsoRL1PdiDuqeQpZE0bP2pnPrGqFcOkI0nld
8788
cloud.google.com/go/auth v0.15.0/go.mod h1:WJDGqZ1o9E9wKIL+IwStfyn/+s59zl4Bi+1KQNVXLZ8=
8889
cloud.google.com/go/auth v0.16.0/go.mod h1:1howDHJ5IETh/LwYs3ZxvlkXF48aSqqJUM+5o02dNOI=
8990
cloud.google.com/go/auth v0.16.1/go.mod h1:1howDHJ5IETh/LwYs3ZxvlkXF48aSqqJUM+5o02dNOI=
91+
cloud.google.com/go/auth v0.16.2/go.mod h1:sRBas2Y1fB1vZTdurouM0AzuYQBMZinrUYL8EufhtEA=
9092
cloud.google.com/go/auth/oauth2adapt v0.2.2/go.mod h1:wcYjgpZI9+Yu7LyYBg4pqSiaRkfEK3GQcpb7C/uyF1Q=
9193
cloud.google.com/go/auth/oauth2adapt v0.2.6/go.mod h1:AlmsELtlEBnaNTL7jCj8VQFLy6mbZv0s4Q7NGBeQ5E8=
9294
cloud.google.com/go/auth/oauth2adapt v0.2.7/go.mod h1:NTbTTzfvPl1Y3V1nPpOgl2w6d/FjO7NNUQaWSox6ZMc=
@@ -621,6 +623,8 @@ github.com/GoogleCloudPlatform/cloudsql-proxy v0.0.0-20190129172621-c8b1d7a94ddf
621623
github.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp v1.5.0/go.mod h1:dppbR7CwXD4pgtV9t3wD1812RaLDcBjtblcDF5f1vI0=
622624
github.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp v1.5.2/go.mod h1:dppbR7CwXD4pgtV9t3wD1812RaLDcBjtblcDF5f1vI0=
623625
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.27.0/go.mod h1:yAZHSGnqScoU556rBOVkwLze6WP5N+U11RHuWaGVxwY=
626+
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.51.0/go.mod h1:BnBReJLvVYx2CS/UHOgVz2BXKXD9wsQPxZug20nZhd0=
627+
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.51.0/go.mod h1:otE2jQekW/PqXk1Awf5lmfokJx4uwuqcj1ab5SpGeW0=
624628
github.com/HdrHistogram/hdrhistogram-go v1.1.0/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo=
625629
github.com/HdrHistogram/hdrhistogram-go v1.1.2 h1:5IcZpTvzydCQeHzK4Ef/D5rrSqwxob0t8PQPMybUNFM=
626630
github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo=
@@ -1217,6 +1221,7 @@ github.com/googleapis/gax-go/v2 v2.12.0/go.mod h1:y+aIqrI5eb1YGMVJfuV3185Ts/D7qK
12171221
github.com/googleapis/gax-go/v2 v2.12.3/go.mod h1:AKloxT6GtNbaLm8QTNSidHUVsHYcBHwWRvkNFJUQcS4=
12181222
github.com/googleapis/gax-go/v2 v2.14.0/go.mod h1:lhBCnjdLrWRaPvLWhmc8IS24m9mr07qSYnHncrgo+zk=
12191223
github.com/googleapis/gax-go/v2 v2.14.1/go.mod h1:Hb/NubMaVM88SrNkvl8X/o8XWwDJEPqouaLeN2IUxoA=
1224+
github.com/googleapis/gax-go/v2 v2.14.2/go.mod h1:ON64QhlJkhVtSqp4v1uaK92VyZ2gmvDQsweuyLV+8+w=
12201225
github.com/googleapis/go-type-adapters v1.0.0 h1:9XdMn+d/G57qq1s8dNc5IesGCXHf6V2HZ2JwRxfA2tA=
12211226
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8 h1:tlyzajkF3030q6M8SvmJSemC9DTHL/xaMa18b65+JM4=
12221227
github.com/googleapis/google-cloud-go-testing v0.0.0-20210719221736-1c9a4c676720/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
@@ -1595,7 +1600,6 @@ github.com/nishanths/exhaustive v0.8.3/go.mod h1:qj+zJJUgJ76tR92+25+03oYUhzF4R7/
15951600
github.com/nishanths/exhaustive v0.9.5/go.mod h1:IbwrGdVMizvDcIxPYGVdQn5BqWJaOwpCvg4RGb8r/TA=
15961601
github.com/nishanths/predeclared v0.0.0-20190419143655-18a43bb90ffc/go.mod h1:62PewwiQTlm/7Rj+cxVYqZvDIUc+JjZq6GHAC1fsObQ=
15971602
github.com/nunnatsa/ginkgolinter v0.9.0/go.mod h1:FHaMLURXP7qImeH6bvxWJUpyH+2tuqe5j4rW1gxJRmI=
1598-
github.com/nunnatsa/ginkgolinter v0.20.0/go.mod h1:dCIuFlTPfQerXgGUju3VygfAFPdC5aE1mdacCDKDJcQ=
15991603
github.com/oasisprotocol/curve25519-voi v0.0.0-20220708102147-0a8a51822cae h1:FatpGJD2jmJfhZiFDElaC0QhZUDQnxUeAwTGkfAHN3I=
16001604
github.com/oasisprotocol/curve25519-voi v0.0.0-20220708102147-0a8a51822cae/go.mod h1:hVoHR2EVESiICEMbg137etN/Lx+lSrHPTD39Z/uE+2s=
16011605
github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a h1:dlRvE5fWabOchtH7znfiFCcOvmIYgOeAS5ifBXBlh9Q=
@@ -2040,10 +2044,12 @@ go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.4
20402044
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0/go.mod h1:Mjt1i1INqiaoZOMGR1RIUJN+i3ChKoFRqzrRQhlkbs0=
20412045
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.54.0/go.mod h1:B9yO6b04uB80CzjedvewuqDhxJxi11s7/GtiGa8bAjI=
20422046
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.60.0/go.mod h1:rg+RlpR5dKwaS95IyyZqj5Wd4E13lk/msnTS0Xl9lJM=
2047+
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.61.0/go.mod h1:snMWehoOh2wsEwnvvwtDyFCxVeDAODenXHtn5vzrKjo=
20432048
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1/go.mod h1:sEGXWArGqc3tVa+ekntsN65DmVbVeW+7lTKTjZF3/Fo=
20442049
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0/go.mod h1:p8pYQP+m5XfbZm9fxtSKAbM6oIllS7s2AfxrChvc7iw=
20452050
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0/go.mod h1:L7UH0GbB0p47T4Rri3uHjbpCFYrVrwc1I25QhNPiGK8=
20462051
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0/go.mod h1:69uWxva0WgAA/4bu2Yy70SLDBwZXuQ6PbBpbsa5iZrQ=
2052+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.61.0/go.mod h1:UHB22Z8QsdRDrnAtX4PntOl36ajSxcdUMt1sF7Y6E7Q=
20472053
go.opentelemetry.io/contrib/zpages v0.62.0/go.mod h1:C8kXoiC1Ytvereztus2R+kqdSa6W/MZ8FfS8Zwj+LiM=
20482054
go.opentelemetry.io/otel v1.14.0/go.mod h1:o4buv+dJzx8rohcUeRmWUZhqupFvzWis188WlggnNeU=
20492055
go.opentelemetry.io/otel v1.21.0/go.mod h1:QZzNPQPm1zLX4gZK4cMi+71eaorMSGT3A4znnUvNNEo=

x/interchainstaking/keeper/callbacks.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,7 @@ func AllBalancesCallback(k *Keeper, ctx sdk.Context, args []byte, query icqtypes
799799
zone.PerformanceAddress.BalanceWaitgroup = 0
800800
k.Logger(ctx).Info("zeroing performance balance waitgroup")
801801
}
802+
default:
802803
}
803804
k.SetZone(ctx, &zone)
804805

x/interchainstaking/keeper/fuzz_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func FuzzZones(f *testing.F) {
6969
return
7070
case "\n\t\n\x01K\x10\x0000(0", "\n\t\n\x030D0 0(0", "\n\t\n\x0301000(0":
7171
return
72+
default:
7273
}
7374

7475
suite := new(FuzzingTestSuite)

x/interchainstaking/keeper/keeper.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ func (k *Keeper) SetValidatorsForZone(ctx sdk.Context, data []byte, icqQuery icq
301301
case val.Status != validator.Status.String():
302302
k.Logger(ctx).Debug("bond status change; fetching proof", "valoper", validator.OperatorAddress, "from", val.Status, "to", validator.Status.String())
303303
toQuery = true
304+
default:
304305
}
305306

306307
if toQuery {

x/interchainstaking/keeper/zones_test.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package keeper_test
33
import (
44
"fmt"
55
"io"
6-
"sort"
6+
"slices"
77
"testing"
88
"time"
99

@@ -128,13 +128,21 @@ func TestKeeperWithZonesRoundTrip(t *testing.T) {
128128
wantAllZones := maps.Values(gotZonesMapping)
129129
require.Equal(t, nzones, len(gotAllZones), "expecting unique nzones")
130130
// Sort them for determinism
131-
sort.Slice(gotAllZones, func(i, j int) bool {
132-
zi, zj := gotAllZones[i], gotAllZones[j]
133-
return zi.ChainId < zj.ChainId
131+
slices.SortFunc(gotAllZones, func(i, j types.Zone) int {
132+
if i.ChainId < j.ChainId {
133+
return -1
134+
} else if i.ChainId > j.ChainId {
135+
return 1
136+
}
137+
return 0
134138
})
135-
sort.Slice(wantAllZones, func(i, j int) bool {
136-
zi, zj := wantAllZones[i], wantAllZones[j]
137-
return zi.ChainId < zj.ChainId
139+
slices.SortFunc(wantAllZones, func(i, j types.Zone) int {
140+
if i.ChainId < j.ChainId {
141+
return -1
142+
} else if i.ChainId > j.ChainId {
143+
return 1
144+
}
145+
return 0
138146
})
139147
require.Equal(t, wantAllZones, gotAllZones, "expecting the zones to match")
140148

x/interchainstaking/types/delegation.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package types
22

33
import (
44
"errors"
5-
"sort"
5+
"slices"
66

77
sdkmath "cosmossdk.io/math"
88

@@ -69,8 +69,13 @@ func (d Delegation) GetValidatorAddr() sdk.ValAddress {
6969
type ValidatorIntents []*ValidatorIntent
7070

7171
func (vi ValidatorIntents) Sort() ValidatorIntents {
72-
sort.SliceStable(vi, func(i, j int) bool {
73-
return vi[i].ValoperAddress < vi[j].ValoperAddress
72+
slices.SortStableFunc(vi, func(i, j *ValidatorIntent) int {
73+
if i.ValoperAddress < j.ValoperAddress {
74+
return -1
75+
} else if i.ValoperAddress > j.ValoperAddress {
76+
return 1
77+
}
78+
return 0
7479
})
7580
return vi
7681
}

x/interchainstaking/types/rebalance.go

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package types
33
import (
44
"fmt"
55
"math"
6+
"slices"
67
"sort"
78

89
sdkmath "cosmossdk.io/math"
@@ -102,14 +103,23 @@ func (deltas AllocationDeltas) Sort() {
102103
deltas = newAllocationDeltas
103104

104105
// sort keys by relative value of delta
105-
sort.SliceStable(deltas, func(i, j int) bool {
106-
// < sorts alphabetically.
107-
return deltas[i].ValoperAddress < deltas[j].ValoperAddress
106+
slices.SortStableFunc(deltas, func(i, j *AllocationDelta) int {
107+
if i.ValoperAddress < j.ValoperAddress {
108+
return -1
109+
} else if i.ValoperAddress > j.ValoperAddress {
110+
return 1
111+
}
112+
return 0
108113
})
109114

110115
// sort keys by relative value of delta
111-
sort.SliceStable(deltas, func(i, j int) bool {
112-
return deltas[i].Amount.GT(deltas[j].Amount)
116+
slices.SortStableFunc(deltas, func(i, j *AllocationDelta) int {
117+
if i.Amount.GT(j.Amount) {
118+
return -1
119+
} else if i.Amount.LT(j.Amount) {
120+
return 1
121+
}
122+
return 0
113123
})
114124
}
115125

@@ -180,20 +190,33 @@ type RebalanceTargets []*RebalanceTarget
180190
// Sort RebalanceTargets deterministically.
181191
func (tgts RebalanceTargets) Sort() {
182192
// sort keys by relative value of delta
183-
sort.SliceStable(tgts, func(i, j int) bool {
184-
// < sorts alphabetically.
185-
return tgts[i].Source < tgts[j].Source
193+
slices.SortStableFunc(tgts, func(i, j *RebalanceTarget) int {
194+
if i.Source < j.Source {
195+
return -1
196+
} else if i.Source > j.Source {
197+
return 1
198+
}
199+
return 0
186200
})
187201

188202
// sort keys by relative value of delta
189-
sort.SliceStable(tgts, func(i, j int) bool {
190-
// < sorts alphabetically.
191-
return tgts[i].Target < tgts[j].Target
203+
slices.SortStableFunc(tgts, func(i, j *RebalanceTarget) int {
204+
if i.Target < j.Target {
205+
return -1
206+
} else if i.Target > j.Target {
207+
return 1
208+
}
209+
return 0
192210
})
193211

194212
// sort keys by relative value of delta
195-
sort.SliceStable(tgts, func(i, j int) bool {
196-
return tgts[i].Amount.LT(tgts[j].Amount)
213+
slices.SortStableFunc(tgts, func(i, j *RebalanceTarget) int {
214+
if i.Amount.LT(j.Amount) {
215+
return -1
216+
} else if i.Amount.GT(j.Amount) {
217+
return 1
218+
}
219+
return 0
197220
})
198221
}
199222

@@ -272,6 +295,7 @@ TARGET:
272295
continue TARGET
273296
}
274297
// otherwise, try next source.
298+
default:
275299
}
276300
}
277301
// we only get here if we are unable to satisfy targets due to rebalanceBudget depletion.

x/supply/keeper/keeper.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package keeper
33
import (
44
"bytes"
55
"fmt"
6-
"sort"
6+
"slices"
77

88
"cosmossdk.io/math"
99

@@ -128,8 +128,13 @@ func (k Keeper) TopN(ctx sdk.Context, baseDenom string, n uint64) []*types.Accou
128128
accountSlice = append(accountSlice, &types.Account{Address: addr, Balance: balance})
129129
}
130130

131-
sort.Slice(accountSlice, func(i, j int) bool {
132-
return accountSlice[i].Balance.GT(accountSlice[j].Balance)
131+
slices.SortFunc(accountSlice, func(i, j *types.Account) int {
132+
if i.Balance.GT(j.Balance) {
133+
return -1
134+
} else if i.Balance.LT(j.Balance) {
135+
return 1
136+
}
137+
return 0
133138
})
134139

135140
if n > uint64(len(accountSlice)) {

0 commit comments

Comments
 (0)