diff --git a/metric/system/diskio/diskstat_windows_helper.go b/metric/system/diskio/diskstat_windows_helper.go index 370b1fd7ef..9baf94376e 100644 --- a/metric/system/diskio/diskstat_windows_helper.go +++ b/metric/system/diskio/diskstat_windows_helper.go @@ -59,7 +59,7 @@ func ioCounters(names ...string) (map[string]disk.IOCountersStat, error) { if err != nil || len(logicalDisks) == 0 { return nil, err } - ret := make(map[string]disk.IOCountersStat) + ret := make(map[string]disk.IOCountersStat, len(logicalDisks)) for _, drive := range logicalDisks { // not get _Total or Harddrive if len(drive.Name) > 3 { @@ -158,7 +158,7 @@ func getLogicalDriveStrings() ([]logicalDrive, error) { } return nil, err } - var logicalDrives []logicalDrive + logicalDrives := make([]logicalDrive, 0, len(lpBuffer)) for _, v := range lpBuffer { if v >= 65 && v <= 90 { s := string(v) diff --git a/metric/system/process/process.go b/metric/system/process/process.go index 0b1b80d75d..0bdc05b061 100644 --- a/metric/system/process/process.go +++ b/metric/system/process/process.go @@ -115,8 +115,8 @@ func (procStats *Stats) Get() ([]mapstr.M, []mapstr.M, error) { } // Format the list to the MapStr type used by the outputs - var procs []mapstr.M - var rootEvents []mapstr.M + procs := make([]mapstr.M, 0, len(plist)) + rootEvents := make([]mapstr.M, 0, len(plist)) for _, process := range plist { process := process diff --git a/metric/system/process/process_darwin.go b/metric/system/process/process_darwin.go index 950d9734b6..5c17cc2275 100644 --- a/metric/system/process/process_darwin.go +++ b/metric/system/process/process_darwin.go @@ -64,8 +64,8 @@ func (procStats *Stats) FetchPids() (ProcsMap, []ProcState, error) { bbuf := bytes.NewBuffer(buf) - procMap := make(ProcsMap, 0) - var plist []ProcState + procMap := make(ProcsMap, num) + plist := make([]ProcState, 0, num) for i := 0; i < num; i++ { if err := binary.Read(bbuf, binary.LittleEndian, &pid); err != nil { @@ -193,7 +193,7 @@ func getProcArgs(pid int, filter func(string) bool) ([]string, string, mapstr.M, } // read CLI args - var argv []string + argv := make([]string, 0, argc) for i := 0; i < int(argc); i++ { arg, err := bbuf.ReadBytes(0) if err == io.EOF { diff --git a/metric/system/process/process_linux_common.go b/metric/system/process/process_linux_common.go index 38427a85c8..026cf45699 100644 --- a/metric/system/process/process_linux_common.go +++ b/metric/system/process/process_linux_common.go @@ -58,8 +58,8 @@ func (procStats *Stats) FetchPids() (ProcsMap, []ProcState, error) { return nil, nil, fmt.Errorf("error reading directory names: %w", err) } - procMap := make(ProcsMap) - var plist []ProcState + procMap := make(ProcsMap, len(names)) + plist := make([]ProcState, 0, len(names)) // Iterate over the directory, fetch just enough info so we can filter based on user input. logger := logp.L() diff --git a/metric/system/process/process_windows.go b/metric/system/process/process_windows.go index 61882d6ece..dcc345d69a 100644 --- a/metric/system/process/process_windows.go +++ b/metric/system/process/process_windows.go @@ -38,8 +38,8 @@ func (procStats *Stats) FetchPids() (ProcsMap, []ProcState, error) { return nil, nil, fmt.Errorf("EnumProcesses failed: %w", err) } - procMap := make(ProcsMap, 0) - var plist []ProcState + procMap := make(ProcsMap, len(pids)) + plist := make([]ProcState, 0, len(pids)) // This is probably the only implementation that doesn't benefit from our // little fillPid callback system. We'll need to iterate over everything // manually.