Skip to content

Commit

Permalink
Labels: use single byte as separator - small speedup
Browse files Browse the repository at this point in the history
Since `seps` is a variable, `seps[0]` has to be bounds-checked every
time. Replacing with a constant everywhere it is used skips this
overhead.

Signed-off-by: Bryan Boreham <[email protected]>
  • Loading branch information
bboreham committed Jul 15, 2024
1 parent d116bf7 commit d84282b
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 36 deletions.
24 changes: 12 additions & 12 deletions model/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
Expand All @@ -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++
}
Expand All @@ -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
}
Expand All @@ -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++
Expand All @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions model/labels/labels_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 12 additions & 12 deletions model/labels/labels_dedupelabels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}

Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions model/labels/labels_stringlabels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions model/labels/sharding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 2 additions & 2 deletions model/labels/sharding_dedupelabels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions model/labels/sharding_stringlabels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d84282b

Please sign in to comment.