Skip to content

Commit

Permalink
remove sync.Pool
Browse files Browse the repository at this point in the history
  • Loading branch information
VihasMakwana committed Nov 23, 2024
1 parent fd8d0df commit 70cb699
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 21 deletions.
19 changes: 1 addition & 18 deletions helpers/windows/pdh/pdh_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"sort"
"strconv"
"strings"
"sync"
"syscall"
"unicode/utf16"
"unsafe"
Expand Down Expand Up @@ -244,26 +243,13 @@ func PdhGetRawCounterValue(counter PdhCounterHandle) (PdhRawCounter, error) {
return value, nil
}

// use buffer pool to resue the underlying byte slice. This reduces memory allocations
var bufPool = sync.Pool{
New: func() interface{} {
return make([]byte, 1024)
},
}

func PdhGetRawCounterArray(counter PdhCounterHandle, filterTotal bool) (RawCouterArray, error) {
var bufferSize, itemCount uint32
if err := _PdhGetRawCounterArray(counter, &bufferSize, &itemCount, nil); err != nil {
if PdhErrno(err.(syscall.Errno)) != PDH_MORE_DATA {
return nil, PdhErrno(err.(syscall.Errno))
}
// Get the byte buffer
buf := bufPool.Get().([]byte)

// if the buffer is lower than required size, then create a new one.
if len(buf) < int(bufferSize) {
buf = make([]byte, bufferSize)
}
buf := make([]byte, bufferSize)
if err := _PdhGetRawCounterArray(counter, &bufferSize, &itemCount, &buf[0]); err != nil {
return nil, PdhErrno(err.(syscall.Errno))
}
Expand All @@ -282,9 +268,6 @@ func PdhGetRawCounterArray(counter PdhCounterHandle, filterTotal bool) (RawCoute
// we sort the array by the instance name to ensure that each index in the final array corresponds to a specific core
// This is important because we will be collecting three different types of counters, and sorting ensures that each index in each counter aligns with the correct core.
sort.Sort(RawCouterArray(ret))

// reuse the buffer
bufPool.Put(buf)
return ret, nil
}
return nil, PdhErrno(syscall.ERROR_NOT_FOUND)
Expand Down
3 changes: 0 additions & 3 deletions helpers/windows/pdh/zpdh_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 70cb699

Please sign in to comment.