Merge pull request #14470 from bboreham/label-sep-byte

Labels: use single byte as separator - small speedup
This commit is contained in:
Bryan Boreham 2024-07-24 13:54:59 +01:00 committed by GitHub
commit 71c90c71d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 37 additions and 36 deletions

View file

@ -38,10 +38,10 @@ func (ls Labels) Bytes(buf []byte) []byte {
b.WriteByte(labelSep) b.WriteByte(labelSep)
for i, l := range ls { for i, l := range ls {
if i > 0 { if i > 0 {
b.WriteByte(seps[0]) b.WriteByte(sep)
} }
b.WriteString(l.Name) b.WriteString(l.Name)
b.WriteByte(seps[0]) b.WriteByte(sep)
b.WriteString(l.Value) b.WriteString(l.Value)
} }
return b.Bytes() return b.Bytes()
@ -86,9 +86,9 @@ func (ls Labels) Hash() uint64 {
} }
b = append(b, v.Name...) b = append(b, v.Name...)
b = append(b, seps[0]) b = append(b, sep)
b = append(b, v.Value...) b = append(b, v.Value...)
b = append(b, seps[0]) b = append(b, sep)
} }
return xxhash.Sum64(b) return xxhash.Sum64(b)
} }
@ -106,9 +106,9 @@ func (ls Labels) HashForLabels(b []byte, names ...string) (uint64, []byte) {
i++ i++
default: default:
b = append(b, ls[i].Name...) b = append(b, ls[i].Name...)
b = append(b, seps[0]) b = append(b, sep)
b = append(b, ls[i].Value...) b = append(b, ls[i].Value...)
b = append(b, seps[0]) b = append(b, sep)
i++ i++
j++ j++
} }
@ -130,9 +130,9 @@ func (ls Labels) HashWithoutLabels(b []byte, names ...string) (uint64, []byte) {
continue continue
} }
b = append(b, ls[i].Name...) b = append(b, ls[i].Name...)
b = append(b, seps[0]) b = append(b, sep)
b = append(b, ls[i].Value...) b = append(b, ls[i].Value...)
b = append(b, seps[0]) b = append(b, sep)
} }
return xxhash.Sum64(b), b return xxhash.Sum64(b), b
} }
@ -151,10 +151,10 @@ func (ls Labels) BytesWithLabels(buf []byte, names ...string) []byte {
i++ i++
default: default:
if b.Len() > 1 { if b.Len() > 1 {
b.WriteByte(seps[0]) b.WriteByte(sep)
} }
b.WriteString(ls[i].Name) b.WriteString(ls[i].Name)
b.WriteByte(seps[0]) b.WriteByte(sep)
b.WriteString(ls[i].Value) b.WriteString(ls[i].Value)
i++ i++
j++ j++
@ -177,10 +177,10 @@ func (ls Labels) BytesWithoutLabels(buf []byte, names ...string) []byte {
continue continue
} }
if b.Len() > 1 { if b.Len() > 1 {
b.WriteByte(seps[0]) b.WriteByte(sep)
} }
b.WriteString(ls[i].Name) b.WriteString(ls[i].Name)
b.WriteByte(seps[0]) b.WriteByte(sep)
b.WriteString(ls[i].Value) b.WriteString(ls[i].Value)
} }
return b.Bytes() return b.Bytes()

View file

@ -29,10 +29,11 @@ const (
BucketLabel = "le" BucketLabel = "le"
InstanceName = "instance" InstanceName = "instance"
labelSep = '\xfe' labelSep = '\xfe' // Used at beginning of `Bytes` return.
sep = '\xff' // Used between labels in `Bytes` and `Hash`.
) )
var seps = []byte{'\xff'} var seps = []byte{sep} // Used with Hash, which has no WriteByte method.
// Label is a key/value pair of strings. // Label is a key/value pair of strings.
type Label struct { type Label struct {

View file

@ -146,13 +146,13 @@ func (ls Labels) Bytes(buf []byte) []byte {
b := bytes.NewBuffer(buf[:0]) b := bytes.NewBuffer(buf[:0])
for i := 0; i < len(ls.data); { for i := 0; i < len(ls.data); {
if i > 0 { if i > 0 {
b.WriteByte(seps[0]) b.WriteByte(sep)
} }
var name, value string var name, value string
name, i = decodeString(ls.syms, ls.data, i) name, i = decodeString(ls.syms, ls.data, i)
value, i = decodeString(ls.syms, ls.data, i) value, i = decodeString(ls.syms, ls.data, i)
b.WriteString(name) b.WriteString(name)
b.WriteByte(seps[0]) b.WriteByte(sep)
b.WriteString(value) b.WriteString(value)
} }
return b.Bytes() return b.Bytes()
@ -201,9 +201,9 @@ func (ls Labels) Hash() uint64 {
} }
b = append(b, name...) b = append(b, name...)
b = append(b, seps[0]) b = append(b, sep)
b = append(b, value...) b = append(b, value...)
b = append(b, seps[0]) b = append(b, sep)
pos = newPos pos = newPos
} }
return xxhash.Sum64(b) return xxhash.Sum64(b)
@ -226,9 +226,9 @@ func (ls Labels) HashForLabels(b []byte, names ...string) (uint64, []byte) {
} }
if name == names[j] { if name == names[j] {
b = append(b, name...) b = append(b, name...)
b = append(b, seps[0]) b = append(b, sep)
b = append(b, value...) b = append(b, value...)
b = append(b, seps[0]) b = append(b, sep)
} }
} }
@ -252,9 +252,9 @@ func (ls Labels) HashWithoutLabels(b []byte, names ...string) (uint64, []byte) {
continue continue
} }
b = append(b, name...) b = append(b, name...)
b = append(b, seps[0]) b = append(b, sep)
b = append(b, value...) b = append(b, value...)
b = append(b, seps[0]) b = append(b, sep)
} }
return xxhash.Sum64(b), b return xxhash.Sum64(b), b
} }
@ -275,10 +275,10 @@ func (ls Labels) BytesWithLabels(buf []byte, names ...string) []byte {
} }
if lName == names[j] { if lName == names[j] {
if b.Len() > 1 { if b.Len() > 1 {
b.WriteByte(seps[0]) b.WriteByte(sep)
} }
b.WriteString(lName) b.WriteString(lName)
b.WriteByte(seps[0]) b.WriteByte(sep)
b.WriteString(lValue) b.WriteString(lValue)
} }
pos = newPos pos = newPos
@ -299,10 +299,10 @@ func (ls Labels) BytesWithoutLabels(buf []byte, names ...string) []byte {
} }
if j == len(names) || lName != names[j] { if j == len(names) || lName != names[j] {
if b.Len() > 1 { if b.Len() > 1 {
b.WriteByte(seps[0]) b.WriteByte(sep)
} }
b.WriteString(lName) b.WriteString(lName)
b.WriteByte(seps[0]) b.WriteByte(sep)
b.WriteString(lValue) b.WriteString(lValue)
} }
pos = newPos pos = newPos

View file

@ -112,9 +112,9 @@ func (ls Labels) HashForLabels(b []byte, names ...string) (uint64, []byte) {
} }
if name == names[j] { if name == names[j] {
b = append(b, name...) b = append(b, name...)
b = append(b, seps[0]) b = append(b, sep)
b = append(b, value...) b = append(b, value...)
b = append(b, seps[0]) b = append(b, sep)
} }
} }
@ -138,9 +138,9 @@ func (ls Labels) HashWithoutLabels(b []byte, names ...string) (uint64, []byte) {
continue continue
} }
b = append(b, name...) b = append(b, name...)
b = append(b, seps[0]) b = append(b, sep)
b = append(b, value...) b = append(b, value...)
b = append(b, seps[0]) b = append(b, sep)
} }
return xxhash.Sum64(b), b return xxhash.Sum64(b), b
} }

View file

@ -39,9 +39,9 @@ func StableHash(ls Labels) uint64 {
} }
b = append(b, v.Name...) b = append(b, v.Name...)
b = append(b, seps[0]) b = append(b, sep)
b = append(b, v.Value...) b = append(b, v.Value...)
b = append(b, seps[0]) b = append(b, sep)
} }
return xxhash.Sum64(b) return xxhash.Sum64(b)
} }

View file

@ -43,9 +43,9 @@ func StableHash(ls Labels) uint64 {
} }
b = append(b, name...) b = append(b, name...)
b = append(b, seps[0]) b = append(b, sep)
b = append(b, value...) b = append(b, value...)
b = append(b, seps[0]) b = append(b, sep)
pos = newPos pos = newPos
} }
return xxhash.Sum64(b) return xxhash.Sum64(b)

View file

@ -43,9 +43,9 @@ func StableHash(ls Labels) uint64 {
} }
b = append(b, v.Name...) b = append(b, v.Name...)
b = append(b, seps[0]) b = append(b, sep)
b = append(b, v.Value...) b = append(b, v.Value...)
b = append(b, seps[0]) b = append(b, sep)
} }
if h != nil { if h != nil {
return h.Sum64() return h.Sum64()