Skip to content

Commit

Permalink
metric/system: preallocate arrays
Browse files Browse the repository at this point in the history
Whenever possible, preallocate array to the correct size instead of letting
arrays "grow". This reduces the load on the Garbage Collector and improves
memory usage efficiency.

Signed-off-by: Florian Lehner <[email protected]>
  • Loading branch information
florianl committed Nov 20, 2023
1 parent c117bd8 commit cbbccb6
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions metric/system/diskio/diskstat_windows_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions metric/system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions metric/system/process/process_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions metric/system/process/process_linux_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions metric/system/process/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit cbbccb6

Please sign in to comment.