fix(models): Optimize allocations in VerifyPoint()

pull/17707/head
Ben Johnson 2020-04-10 13:35:45 -06:00
parent 2fe78240a0
commit 914255d374
1 changed files with 15 additions and 6 deletions

View File

@ -1374,7 +1374,13 @@ func pointKey(measurement string, tags Tags, fields Fields, t time.Time) ([]byte
}
}
key := MakeKey([]byte(measurement), tags)
estimatedSize := len(measurement) + 10 // add additional buffer for escaping & spaces
for _, t := range tags {
estimatedSize += len(t.Key) + len(t.Value) + 2
}
buf := make([]byte, 0, estimatedSize)
key := AppendMakeKey(buf, []byte(measurement), tags)
for field := range fields {
sz := seriesKeySizeV1(key, []byte(field))
if sz > MaxKeyLength {
@ -2443,23 +2449,26 @@ func (p *point) Reset() {
// again later to an int64
// NOTE2: uint is accepted, and may be 64 bits, and is for some reason accepted...
func (p Fields) MarshalBinary() []byte {
var b []byte
sz := len(p) - 1 // separators
keys := make([]string, 0, len(p))
for k := range p {
keys = append(keys, k)
sz += len(k)
}
// Not really necessary, can probably be removed.
sort.Strings(keys)
// Only sort if we have multiple fields to sort.
// This length check removes an allocation incurred by the sort.
if len(keys) > 1 {
sort.Strings(keys)
}
b := make([]byte, 0, sz)
for i, k := range keys {
if i > 0 {
b = append(b, ',')
}
b = appendField(b, k, p[k])
}
return b
}