labels: small optimization to stringlabels

Add a fast path for the common case that a string is less than 127 bytes
long, to skip a shift and the loop.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2023-04-14 14:37:35 +00:00
parent 7309ac2721
commit 1801cd4196
1 changed files with 8 additions and 2 deletions

View File

@ -56,8 +56,14 @@ func (ls labelSlice) Swap(i, j int) { ls[i], ls[j] = ls[j], ls[i] }
func (ls labelSlice) Less(i, j int) bool { return ls[i].Name < ls[j].Name } func (ls labelSlice) Less(i, j int) bool { return ls[i].Name < ls[j].Name }
func decodeSize(data string, index int) (int, int) { func decodeSize(data string, index int) (int, int) {
var size int // Fast-path for common case of a single byte, value 0..127.
for shift := uint(0); ; shift += 7 { b := data[index]
index++
if b < 0x80 {
return int(b), index
}
size := int(b & 0x7F)
for shift := uint(7); ; shift += 7 {
// Just panic if we go of the end of data, since all Labels strings are constructed internally and // Just panic if we go of the end of data, since all Labels strings are constructed internally and
// malformed data indicates a bug, or memory corruption. // malformed data indicates a bug, or memory corruption.
b := data[index] b := data[index]