@@ -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.
427428func 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