perf: pre-allocate slices

Signed-off-by: Ben Reedy <breed808@breed808.com>
This commit is contained in:
Ben Reedy 2024-05-13 08:31:11 +10:00
parent c713bed4e3
commit a49dee606b
No known key found for this signature in database
GPG Key ID: 235C15B6086C9D7E
4 changed files with 9 additions and 6 deletions

View File

@ -8,6 +8,7 @@ linters:
- loggercheck - loggercheck
- nilnil - nilnil
- nilerr - nilerr
- prealloc
- revive - revive
- unconvert - unconvert
- unparam - unparam

View File

@ -163,11 +163,12 @@ func (c *Collectors) SetPerfCounterQuery() error {
var ( var (
err error err error
perfCounterDependencies []string
perfCounterNames []string perfCounterNames []string
perfIndicies []string perfIndicies []string
) )
perfCounterDependencies := make([]string, 0, len(c.collectors))
for _, collector := range c.collectors { for _, collector := range c.collectors {
perfCounterNames, err = collector.GetPerfCounter() perfCounterNames, err = collector.GetPerfCounter()
if err != nil { if err != nil {

View File

@ -130,8 +130,9 @@ func (c *collector) SetLogger(logger log.Logger) {
func (c *collector) GetPerfCounter() ([]string, error) { func (c *collector) GetPerfCounter() ([]string, error) {
// Perflib sources are dynamic, depending on the enabled child collectors // Perflib sources are dynamic, depending on the enabled child collectors
var perflibDependencies []string expandedChildCollectors := utils.ExpandEnabledChildCollectors(*c.dfsrEnabledCollectors)
for _, source := range utils.ExpandEnabledChildCollectors(*c.dfsrEnabledCollectors) { perflibDependencies := make([]string, 0, len(expandedChildCollectors))
for _, source := range expandedChildCollectors {
perflibDependencies = append(perflibDependencies, dfsrGetPerfObjectName(source)) perflibDependencies = append(perflibDependencies, dfsrGetPerfObjectName(source))
} }

View File

@ -373,7 +373,7 @@ func scrapeFile(path string, log log.Logger) ([]*dto.MetricFamily, error) {
} }
// Use temporary array to check for duplicates // Use temporary array to check for duplicates
var families_array []*dto.MetricFamily families_array := make([]*dto.MetricFamily, 0, len(parsedFamilies))
for _, mf := range parsedFamilies { for _, mf := range parsedFamilies {
families_array = append(families_array, mf) families_array = append(families_array, mf)