Replace calls to strings.Compare (#9397)

< is clearer and faster. As the documentation says,
"Basically no one should use strings.Compare."

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2021-09-27 13:03:53 +01:00 committed by GitHub
parent dbbfd1ccf6
commit 1fb3c1b598
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 9 deletions

View File

@ -18,7 +18,6 @@ import (
"container/heap" "container/heap"
"math" "math"
"sort" "sort"
"strings"
"sync" "sync"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -197,15 +196,13 @@ func mergeStrings(a, b []string) []string {
res := make([]string, 0, maxl*10/9) res := make([]string, 0, maxl*10/9)
for len(a) > 0 && len(b) > 0 { for len(a) > 0 && len(b) > 0 {
d := strings.Compare(a[0], b[0]) if a[0] == b[0] {
if d == 0 {
res = append(res, a[0]) res = append(res, a[0])
a, b = a[1:], b[1:] a, b = a[1:], b[1:]
} else if d < 0 { } else if a[0] < b[0] {
res = append(res, a[0]) res = append(res, a[0])
a = a[1:] a = a[1:]
} else if d > 0 { } else {
res = append(res, b[0]) res = append(res, b[0])
b = b[1:] b = b[1:]
} }

View File

@ -18,7 +18,6 @@ import (
"encoding/binary" "encoding/binary"
"runtime" "runtime"
"sort" "sort"
"strings"
"sync" "sync"
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
@ -94,8 +93,8 @@ func (p *MemPostings) SortedKeys() []labels.Label {
p.mtx.RUnlock() p.mtx.RUnlock()
sort.Slice(keys, func(i, j int) bool { sort.Slice(keys, func(i, j int) bool {
if d := strings.Compare(keys[i].Name, keys[j].Name); d != 0 { if keys[i].Name != keys[j].Name {
return d < 0 return keys[i].Name < keys[j].Name
} }
return keys[i].Value < keys[j].Value return keys[i].Value < keys[j].Value
}) })