mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Merge b835d3bdb4
into 677efa4678
This commit is contained in:
commit
f73f06cea6
|
@ -24,31 +24,24 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Labels is implemented by a single flat string holding name/value pairs.
|
// Labels is implemented by a single flat string holding name/value pairs.
|
||||||
// Each name and value is preceded by its length in varint encoding.
|
// Each name and value is preceded by its length, encoded as a single byte
|
||||||
|
// for size 0-254, or the following 3 bytes little-endian, if the first byte is 255.
|
||||||
|
// Maximum length allowed is 2^24 or 16MB.
|
||||||
// Names are in order.
|
// Names are in order.
|
||||||
type Labels struct {
|
type Labels struct {
|
||||||
data string
|
data string
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodeSize(data string, index int) (int, int) {
|
func decodeSize(data string, index int) (int, int) {
|
||||||
// Fast-path for common case of a single byte, value 0..127.
|
// Fast-path for common case of a single byte, value 0..254.
|
||||||
b := data[index]
|
b := data[index]
|
||||||
index++
|
if b < 255 {
|
||||||
if b < 0x80 {
|
return int(b), index + 1
|
||||||
return int(b), index
|
|
||||||
}
|
}
|
||||||
size := int(b & 0x7F)
|
// Otherwise it's encoded as 3 bytes little-endian.
|
||||||
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]
|
return int(data[index+1]) + (int(data[index+2]) << 8) + (int(data[index+3]) << 16), index + 4
|
||||||
index++
|
|
||||||
size |= int(b&0x7F) << shift
|
|
||||||
if b < 0x80 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return size, index
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodeString(data string, index int) (string, int) {
|
func decodeString(data string, index int) (string, int) {
|
||||||
|
@ -527,48 +520,25 @@ func marshalLabelToSizedBuffer(m *Label, data []byte) int {
|
||||||
return len(data) - i
|
return len(data) - i
|
||||||
}
|
}
|
||||||
|
|
||||||
func sizeVarint(x uint64) (n int) {
|
func sizeWhenEncoded(x uint64) (n int) {
|
||||||
// Most common case first
|
if x < 254 {
|
||||||
if x < 1<<7 {
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
if x >= 1<<56 {
|
return 4
|
||||||
return 9
|
|
||||||
}
|
|
||||||
if x >= 1<<28 {
|
|
||||||
x >>= 28
|
|
||||||
n = 4
|
|
||||||
}
|
|
||||||
if x >= 1<<14 {
|
|
||||||
x >>= 14
|
|
||||||
n += 2
|
|
||||||
}
|
|
||||||
if x >= 1<<7 {
|
|
||||||
n++
|
|
||||||
}
|
|
||||||
return n + 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func encodeVarint(data []byte, offset int, v uint64) int {
|
|
||||||
offset -= sizeVarint(v)
|
|
||||||
base := offset
|
|
||||||
for v >= 1<<7 {
|
|
||||||
data[offset] = uint8(v&0x7f | 0x80)
|
|
||||||
v >>= 7
|
|
||||||
offset++
|
|
||||||
}
|
|
||||||
data[offset] = uint8(v)
|
|
||||||
return base
|
|
||||||
}
|
|
||||||
|
|
||||||
// Special code for the common case that a size is less than 128
|
|
||||||
func encodeSize(data []byte, offset, v int) int {
|
func encodeSize(data []byte, offset, v int) int {
|
||||||
if v < 1<<7 {
|
if v < 255 {
|
||||||
offset--
|
offset--
|
||||||
data[offset] = uint8(v)
|
data[offset] = uint8(v)
|
||||||
return offset
|
return offset
|
||||||
}
|
}
|
||||||
return encodeVarint(data, offset, uint64(v))
|
offset -= 4
|
||||||
|
data[offset] = 255
|
||||||
|
data[offset+1] = byte(v)
|
||||||
|
data[offset+2] = byte((v >> 8))
|
||||||
|
data[offset+3] = byte((v >> 16))
|
||||||
|
return offset
|
||||||
}
|
}
|
||||||
|
|
||||||
func labelsSize(lbls []Label) (n int) {
|
func labelsSize(lbls []Label) (n int) {
|
||||||
|
@ -582,9 +552,9 @@ func labelsSize(lbls []Label) (n int) {
|
||||||
func labelSize(m *Label) (n int) {
|
func labelSize(m *Label) (n int) {
|
||||||
// strings are encoded as length followed by contents.
|
// strings are encoded as length followed by contents.
|
||||||
l := len(m.Name)
|
l := len(m.Name)
|
||||||
n += l + sizeVarint(uint64(l))
|
n += l + sizeWhenEncoded(uint64(l))
|
||||||
l = len(m.Value)
|
l = len(m.Value)
|
||||||
n += l + sizeVarint(uint64(l))
|
n += l + sizeWhenEncoded(uint64(l))
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue