From a49dee606b874ea7ec69ee6a523239640c5fea8c Mon Sep 17 00:00:00 2001 From: Ben Reedy Date: Mon, 13 May 2024 08:31:11 +1000 Subject: [PATCH] perf: pre-allocate slices Signed-off-by: Ben Reedy --- .golangci.yaml | 1 + pkg/collector/collector.go | 7 ++++--- pkg/collector/dfsr/dfsr.go | 5 +++-- pkg/collector/textfile/textfile.go | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 2b568be2..25f72094 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -8,6 +8,7 @@ linters: - loggercheck - nilnil - nilerr + - prealloc - revive - unconvert - unparam diff --git a/pkg/collector/collector.go b/pkg/collector/collector.go index d53daa69..1a7d355b 100644 --- a/pkg/collector/collector.go +++ b/pkg/collector/collector.go @@ -163,11 +163,12 @@ func (c *Collectors) SetPerfCounterQuery() error { var ( err error - perfCounterDependencies []string - perfCounterNames []string - perfIndicies []string + perfCounterNames []string + perfIndicies []string ) + perfCounterDependencies := make([]string, 0, len(c.collectors)) + for _, collector := range c.collectors { perfCounterNames, err = collector.GetPerfCounter() if err != nil { diff --git a/pkg/collector/dfsr/dfsr.go b/pkg/collector/dfsr/dfsr.go index c68a5346..002d5772 100644 --- a/pkg/collector/dfsr/dfsr.go +++ b/pkg/collector/dfsr/dfsr.go @@ -130,8 +130,9 @@ func (c *collector) SetLogger(logger log.Logger) { func (c *collector) GetPerfCounter() ([]string, error) { // Perflib sources are dynamic, depending on the enabled child collectors - var perflibDependencies []string - for _, source := range utils.ExpandEnabledChildCollectors(*c.dfsrEnabledCollectors) { + expandedChildCollectors := utils.ExpandEnabledChildCollectors(*c.dfsrEnabledCollectors) + perflibDependencies := make([]string, 0, len(expandedChildCollectors)) + for _, source := range expandedChildCollectors { perflibDependencies = append(perflibDependencies, dfsrGetPerfObjectName(source)) } diff --git a/pkg/collector/textfile/textfile.go b/pkg/collector/textfile/textfile.go index 6d2b238a..1ce2933f 100644 --- a/pkg/collector/textfile/textfile.go +++ b/pkg/collector/textfile/textfile.go @@ -373,7 +373,7 @@ func scrapeFile(path string, log log.Logger) ([]*dto.MetricFamily, error) { } // Use temporary array to check for duplicates - var families_array []*dto.MetricFamily + families_array := make([]*dto.MetricFamily, 0, len(parsedFamilies)) for _, mf := range parsedFamilies { families_array = append(families_array, mf)