Skip to content

Commit

Permalink
labels: Use slices.Sort instead of sort.Strings
Browse files Browse the repository at this point in the history
As suggested by package "sort" documentation
(https://pkg.go.dev/sort#Strings), slices.Sort should be preferred to
sort.Strings since it runs faster.

Comparison between sort.Strings ("old") and slices.Sort ("new"):

name                        old time/op    new time/op    delta
pkg:github.com/cilium/cilium/pkg/labels goos:linux goarch:amd64
Labels_SortedList-8            787ns ± 4%     736ns ±12%   -6.42%  (p=0.014 n=7+8)
pkg:github.com/cilium/cilium/pkg/labels/cidr goos:linux goarch:amd64
Labels_SortedListCIDRIDs-8    5.22µs ± 8%    4.67µs ± 4%  -10.63%  (p=0.000 n=10+10)

name                        old alloc/op   new alloc/op   delta
pkg:github.com/cilium/cilium/pkg/labels goos:linux goarch:amd64
Labels_SortedList-8             360B ± 0%      336B ± 0%   -6.67%  (p=0.000 n=10+10)
pkg:github.com/cilium/cilium/pkg/labels/cidr goos:linux goarch:amd64
Labels_SortedListCIDRIDs-8    1.62kB ± 0%    1.60kB ± 0%   -1.48%  (p=0.000 n=10+10)

name                        old allocs/op  new allocs/op  delta
pkg:github.com/cilium/cilium/pkg/labels goos:linux goarch:amd64
Labels_SortedList-8             3.00 ± 0%      2.00 ± 0%  -33.33%  (p=0.000 n=10+10)
pkg:github.com/cilium/cilium/pkg/labels/cidr goos:linux goarch:amd64
Labels_SortedListCIDRIDs-8      3.00 ± 0%      2.00 ± 0%  -33.33%  (p=0.000 n=10+10)

Signed-off-by: Fabio Falzoi <fabio.falzoi@isovalent.com>
  • Loading branch information
pippolo84 authored and joestringer committed Oct 17, 2023
1 parent facf3b4 commit d6e3eda
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
"net"
"slices"
"sort"
"strings"
)
Expand Down Expand Up @@ -512,7 +513,7 @@ func (l Labels) SortedList() []byte {
for k := range l {
keys = append(keys, k)
}
sort.Strings(keys)
slices.Sort(keys)

// Labels can have arbitrary size. However, when many CIDR identities are in
// the system, for example due to a FQDN policy matching S3, CIDR labels
Expand Down

0 comments on commit d6e3eda

Please sign in to comment.