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
- nilnil
- nilerr
- prealloc
- revive
- unconvert
- unparam

View File

@ -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 {

View File

@ -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))
}

View File

@ -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)