From 0040fb93d6e6da2cd9e5c0133d09b927e68c4c53 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 15 Oct 2022 14:55:40 +0200 Subject: [PATCH] pkg/system: IsProcessZombie() skip conversion to string, use bytes instead bytes.SplitN() is more performant, and skips having to do the conversion. Signed-off-by: Sebastiaan van Stijn --- pkg/system/process_unix.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/pkg/system/process_unix.go b/pkg/system/process_unix.go index 11df98bd22..951bc179d4 100644 --- a/pkg/system/process_unix.go +++ b/pkg/system/process_unix.go @@ -4,9 +4,9 @@ package system // import "github.com/docker/docker/pkg/system" import ( + "bytes" "fmt" "os" - "strings" "golang.org/x/sys/unix" ) @@ -29,18 +29,15 @@ func KillProcess(pid int) { // IsProcessZombie return true if process has a state with "Z" // http://man7.org/linux/man-pages/man5/proc.5.html func IsProcessZombie(pid int) (bool, error) { - dataBytes, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid)) + data, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid)) if err != nil { if os.IsNotExist(err) { return false, nil } return false, err } - data := string(dataBytes) - sdata := strings.SplitN(data, " ", 4) - if len(sdata) >= 3 && sdata[2] == "Z" { + if cols := bytes.SplitN(data, []byte(" "), 4); len(cols) >= 3 && string(cols[2]) == "Z" { return true, nil } - return false, nil }