Browse Source

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

Labels: use single byte as separator - small speedup
pull/14515/head
Bryan Boreham 4 months ago committed by GitHub
parent
commit
71c90c71d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 24
      model/labels/labels.go
  2. 5
      model/labels/labels_common.go
  3. 24
      model/labels/labels_dedupelabels.go
  4. 8
      model/labels/labels_stringlabels.go
  5. 4
      model/labels/sharding.go
  6. 4
      model/labels/sharding_dedupelabels.go
  7. 4
      model/labels/sharding_stringlabels.go

24
model/labels/labels.go

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

5
model/labels/labels_common.go

@ -29,10 +29,11 @@ const (
BucketLabel = "le"
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.
type Label struct {

24
model/labels/labels_dedupelabels.go

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

8
model/labels/labels_stringlabels.go

@ -112,9 +112,9 @@ func (ls Labels) HashForLabels(b []byte, names ...string) (uint64, []byte) {
}
if name == names[j] {
b = append(b, name...)
b = append(b, seps[0])
b = append(b, sep)
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
}
b = append(b, name...)
b = append(b, seps[0])
b = append(b, sep)
b = append(b, value...)
b = append(b, seps[0])
b = append(b, sep)
}
return xxhash.Sum64(b), b
}

4
model/labels/sharding.go

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

4
model/labels/sharding_dedupelabels.go

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

4
model/labels/sharding_stringlabels.go

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

Loading…
Cancel
Save