app/vmselect/netstorage: use unsafe string as a key for a map when the map already contains the given key

This should prevent from a memory allocation and a string copy.
This commit is contained in:
Aliaksandr Valialkin 2021-02-16 15:43:07 +02:00
parent 1bf6cd814d
commit 5a401225c7

View file

@ -875,14 +875,15 @@ func ProcessSearchQuery(sq *storage.SearchQuery, fetchData bool, deadline search
return nil, fmt.Errorf("cannot write %d bytes to temporary file: %w", len(buf), err)
}
metricName := sr.MetricBlockRef.MetricName
brs := m[string(metricName)]
metricNameStrUnsafe := bytesutil.ToUnsafeString(metricName)
brs := m[metricNameStrUnsafe]
brs = append(brs, blockRef{
partRef: sr.MetricBlockRef.BlockRef.PartRef(),
addr: addr,
})
if len(brs) > 1 {
// An optimization: do not allocate a string for already existing metricName key in m
m[string(metricName)] = brs
m[metricNameStrUnsafe] = brs
} else {
// An optimization for big number of time series with long metricName values:
// use only a single copy of metricName for both orderedMetricNames and m.