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 da05c10544
commit a3a09a3c6e

View File

@ -1085,11 +1085,12 @@ func (tbfw *tmpBlocksFileWrapper) RegisterAndWriteBlock(mb *storage.MetricBlock)
tmpBufPool.Put(bb) tmpBufPool.Put(bb)
if err == nil { if err == nil {
metricName := mb.MetricName metricName := mb.MetricName
addrs := tbfw.m[string(metricName)] metricNameStrUnsafe := bytesutil.ToUnsafeString(metricName)
addrs := tbfw.m[metricNameStrUnsafe]
addrs = append(addrs, addr) addrs = append(addrs, addr)
if len(addrs) > 1 { if len(addrs) > 1 {
// An optimization: avoid memory allocation and copy for already existing metricName key in tbfw.m. // An optimization: avoid memory allocation and copy for already existing metricName key in tbfw.m.
tbfw.m[string(metricName)] = addrs tbfw.m[metricNameStrUnsafe] = addrs
} else { } else {
// An optimization for big number of time series with long names: store only a single copy of metricNameStr // An optimization for big number of time series with long names: store only a single copy of metricNameStr
// in both tbfw.orderedMetricNames and tbfw.m. // in both tbfw.orderedMetricNames and tbfw.m.