Skip to content

Commit 06f8bc7

Browse files
committed
labels.Compare: use github.com/grailbio/base/simd package
1 parent ee4e041 commit 06f8bc7

File tree

3 files changed

+9
-17
lines changed

3 files changed

+9
-17
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ require (
2828
github.com/google/pprof v0.0.0-20230406165453-00490a63f317
2929
github.com/gophercloud/gophercloud v1.3.0
3030
github.com/grafana/regexp v0.0.0-20221122212121-6b5c0a4cb7fd
31+
github.com/grailbio/base v0.0.10
3132
github.com/grpc-ecosystem/grpc-gateway v1.16.0
3233
github.com/hashicorp/consul/api v1.20.0
3334
github.com/hashicorp/nomad/api v0.0.0-20230418003350-3067191c5197

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2z
399399
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
400400
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
401401
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
402+
github.com/grailbio/base v0.0.10 h1:FL7DEolplFFhvxNn9T6WQejBRZOcQWb92SBTHcPLX74=
403+
github.com/grailbio/base v0.0.10/go.mod h1:lzK85oI6emqHxO2Cy+xzPzEAC/GcbfH3dqGgZCD3dA4=
402404
github.com/grafana/regexp v0.0.0-20221122212121-6b5c0a4cb7fd h1:PpuIBO5P3e9hpqBD0O/HjhShYuM6XE0i/lbE6J94kww=
403405
github.com/grafana/regexp v0.0.0-20221122212121-6b5c0a4cb7fd/go.mod h1:M5qHK+eWfAv8VR/265dIuEpL3fNfeC21tXXp9itM24A=
404406
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=

model/labels/labels_stringlabels.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"unsafe"
2424

2525
"github.com/cespare/xxhash/v2"
26+
"github.com/grailbio/base/simd"
2627
"github.com/prometheus/common/model"
2728
"golang.org/x/exp/slices"
2829
)
@@ -425,24 +426,12 @@ func FromStrings(ss ...string) Labels {
425426
// TODO: replace with Less function - Compare is never needed.
426427
// TODO: just compare the underlying strings when we don't need alphanumeric sorting.
427428
func Compare(a, b Labels) int {
428-
if len(a.data) == 0 {
429-
return -len(b.data)
430-
} else if len(b.data) == 0 {
431-
return len(a.data)
432-
}
433429
// Find the first byte in the string where a and b differ.
434-
shorter, longer := a.data, b.data
435-
if len(b.data) < len(a.data) {
436-
shorter, longer = b.data, a.data
437-
}
438-
i := 0
439-
_ = longer[len(shorter)-1] // Get compiler to do bounds-check on longer just once here.
440-
for ; i < len(shorter); i++ {
441-
if shorter[i] != longer[i] {
442-
break
443-
}
430+
shorter, longer := yoloBytes(a.data), yoloBytes(b.data)
431+
if len(longer) < len(shorter) {
432+
shorter, longer = longer, shorter
444433
}
445-
firstCharDifferent := i
434+
firstCharDifferent := simd.FirstUnequal8Unsafe(shorter, longer[:len(shorter)], 0)
446435
if firstCharDifferent == len(shorter) {
447436
if len(shorter) == len(longer) {
448437
return 0
@@ -456,7 +445,7 @@ func Compare(a, b Labels) int {
456445

457446
// Now we know that there is some difference before the end of a and b.
458447
// Go back through the fields and find which field that difference is in.
459-
i = 0
448+
i := 0
460449
for {
461450
size, nextI := decodeSize(a.data, i)
462451
if nextI+size > firstCharDifferent {

0 commit comments

Comments
 (0)