From bc8381613d4caab185089394d9df4bd7deb11934 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 6 Aug 2020 19:17:51 +0300 Subject: [PATCH] app/vmselect: reduce memory allocations by pre-allocatin memory for time series map and for a list of time series names --- app/vmselect/netstorage/netstorage.go | 6 +++--- lib/storage/search.go | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/vmselect/netstorage/netstorage.go b/app/vmselect/netstorage/netstorage.go index 8491722b38..56ef994c64 100644 --- a/app/vmselect/netstorage/netstorage.go +++ b/app/vmselect/netstorage/netstorage.go @@ -594,10 +594,10 @@ func ProcessSearchQuery(sq *storage.SearchQuery, fetchData bool, deadline Deadli defer vmstorage.WG.Done() sr := getStorageSearch() - sr.Init(vmstorage.Storage, tfss, tr, *maxMetricsPerSearch, deadline.deadline) + maxSeriesCount := sr.Init(vmstorage.Storage, tfss, tr, *maxMetricsPerSearch, deadline.deadline) - m := make(map[string][]storage.BlockRef) - var orderedMetricNames []string + m := make(map[string][]storage.BlockRef, maxSeriesCount) + orderedMetricNames := make([]string, 0, maxSeriesCount) blocksRead := 0 for sr.NextMetricBlock() { blocksRead++ diff --git a/lib/storage/search.go b/lib/storage/search.go index a8cb32f9a6..ce723149d9 100644 --- a/lib/storage/search.go +++ b/lib/storage/search.go @@ -90,7 +90,9 @@ func (s *Search) reset() { // Init initializes s from the given storage, tfss and tr. // // MustClose must be called when the search is done. -func (s *Search) Init(storage *Storage, tfss []*TagFilters, tr TimeRange, maxMetrics int, deadline uint64) { +// +// Init returns the upper bound on the number of found time series. +func (s *Search) Init(storage *Storage, tfss []*TagFilters, tr TimeRange, maxMetrics int, deadline uint64) int { if s.needClosing { logger.Panicf("BUG: missing MustClose call before the next call to Init") } @@ -110,10 +112,11 @@ func (s *Search) Init(storage *Storage, tfss []*TagFilters, tr TimeRange, maxMet if err != nil { s.err = err - return + return 0 } s.storage = storage + return len(tsids) } // MustClose closes the Search.