Browse Source

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>
pull/9400/head
Bryan Boreham 3 years ago committed by GitHub
parent
commit
1fb3c1b598
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      storage/merge.go
  2. 5
      tsdb/index/postings.go

9
storage/merge.go

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

5
tsdb/index/postings.go

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

Loading…
Cancel
Save