app/vmselect: reduce memory usage when querying big number of time series with long labels

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/646
This commit is contained in:
Aliaksandr Valialkin 2020-07-23 13:48:08 +03:00
parent 49a0011837
commit 7aa5b48508

View file

@ -889,10 +889,17 @@ func (tbfw *tmpBlocksFileWrapper) WriteBlock(mb *storage.MetricBlock) error {
if err == nil {
metricName := mb.MetricName
addrs := tbfw.m[string(metricName)]
if len(addrs) == 0 {
tbfw.orderedMetricNames = append(tbfw.orderedMetricNames, string(metricName))
addrs = append(addrs, addr)
if len(addrs) > 0 {
// An optimization: avoid memory allocation and copy for already existing metricName key in tbfw.m.
tbfw.m[string(metricName)] = addrs
} else {
// 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.
metricNameStr := string(metricName)
tbfw.orderedMetricNames = append(tbfw.orderedMetricNames, metricNameStr)
tbfw.m[metricNameStr] = addrs
}
tbfw.m[string(metricName)] = append(addrs, addr)
}
tbfw.mu.Unlock()
return err