From b3a8b0742c87cc8d90d29f0bf2db30037c21b28f Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 29 Sep 2020 12:12:10 +0200 Subject: [PATCH] libcontainer: prefer bytes.TrimSpace() over strings.TrimSpace() Perform trimming before converting to a string, which should be somewhat more performant. Signed-off-by: Sebastiaan van Stijn --- libcontainer/cgroups/fs2/create.go | 4 ++-- libcontainer/cgroups/fscommon/utils.go | 2 +- libcontainer/init_linux.go | 3 ++- libcontainer/integration/exec_test.go | 6 +++--- libcontainer/network_linux.go | 4 ++-- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/libcontainer/cgroups/fs2/create.go b/libcontainer/cgroups/fs2/create.go index 7be9ece0b..568cee6c1 100644 --- a/libcontainer/cgroups/fs2/create.go +++ b/libcontainer/cgroups/fs2/create.go @@ -105,7 +105,7 @@ func CreateCgroupPath(path string, c *configs.Cgroup) (Err error) { } cgTypeFile := filepath.Join(current, "cgroup.type") cgType, _ := ioutil.ReadFile(cgTypeFile) - switch strings.TrimSpace(string(cgType)) { + switch string(bytes.TrimSpace(cgType)) { // If the cgroup is in an invalid mode (usually this means there's an internal // process in the cgroup tree, because we created a cgroup under an // already-populated-by-other-processes cgroup), then we have to error out if @@ -128,7 +128,7 @@ func CreateCgroupPath(path string, c *configs.Cgroup) (Err error) { fallthrough case "threaded": if containsDomainController(c) { - return fmt.Errorf("cannot enter cgroupv2 %q with domain controllers -- it is in %s mode", current, strings.TrimSpace(string(cgType))) + return fmt.Errorf("cannot enter cgroupv2 %q with domain controllers -- it is in %s mode", current, string(bytes.TrimSpace(cgType))) } } } diff --git a/libcontainer/cgroups/fscommon/utils.go b/libcontainer/cgroups/fscommon/utils.go index 614891a7c..7c387a8b5 100644 --- a/libcontainer/cgroups/fscommon/utils.go +++ b/libcontainer/cgroups/fscommon/utils.go @@ -79,5 +79,5 @@ func GetCgroupParamString(path, file string) (string, error) { return "", err } - return strings.TrimSpace(string(contents)), nil + return strings.TrimSpace(contents), nil } diff --git a/libcontainer/init_linux.go b/libcontainer/init_linux.go index f2dc17e00..11dfe17e1 100644 --- a/libcontainer/init_linux.go +++ b/libcontainer/init_linux.go @@ -3,6 +3,7 @@ package libcontainer import ( + "bytes" "encoding/json" "fmt" "io" @@ -304,7 +305,7 @@ func setupUser(config *initConfig) error { // There's nothing we can do about /etc/group entries, so we silently // ignore setting groups here (since the user didn't explicitly ask us to // set the group). - allowSupGroups := !config.RootlessEUID && strings.TrimSpace(string(setgroups)) != "deny" + allowSupGroups := !config.RootlessEUID && string(bytes.TrimSpace(setgroups)) != "deny" if allowSupGroups { suppGroups := append(execUser.Sgids, addGroups...) diff --git a/libcontainer/integration/exec_test.go b/libcontainer/integration/exec_test.go index 10ce99391..475672aef 100644 --- a/libcontainer/integration/exec_test.go +++ b/libcontainer/integration/exec_test.go @@ -1087,7 +1087,7 @@ func TestSysctl(t *testing.T) { // Wait for process waitProcess(&pconfig, t) - shmmniOutput := strings.TrimSpace(string(stdout.Bytes())) + shmmniOutput := string(bytes.TrimSpace(stdout.Bytes())) if shmmniOutput != "8192" { t.Fatalf("kernel.shmmni property expected to be 8192, but is %s", shmmniOutput) } @@ -1221,7 +1221,7 @@ func TestOomScoreAdj(t *testing.T) { // Wait for process waitProcess(&pconfig, t) - outputOomScoreAdj := strings.TrimSpace(string(stdout.Bytes())) + outputOomScoreAdj := string(bytes.TrimSpace(stdout.Bytes())) // Check that the oom_score_adj matches the value that was set as part of config. if outputOomScoreAdj != strconv.Itoa(*config.OomScoreAdj) { @@ -1624,7 +1624,7 @@ func TestRootfsPropagationSharedMount(t *testing.T) { // Check if mount is visible on host or not. out, err := exec.Command("findmnt", "-n", "-f", "-oTARGET", dir2host).CombinedOutput() - outtrim := strings.TrimSpace(string(out)) + outtrim := string(bytes.TrimSpace(out)) if err != nil { t.Logf("findmnt error %q: %q", err, outtrim) } diff --git a/libcontainer/network_linux.go b/libcontainer/network_linux.go index 938d8ce06..a0a87b984 100644 --- a/libcontainer/network_linux.go +++ b/libcontainer/network_linux.go @@ -3,11 +3,11 @@ package libcontainer import ( + "bytes" "fmt" "io/ioutil" "path/filepath" "strconv" - "strings" "github.com/opencontainers/runc/libcontainer/configs" "github.com/opencontainers/runc/types" @@ -79,7 +79,7 @@ func readSysfsNetworkStats(ethInterface, statsFile string) (uint64, error) { if err != nil { return 0, err } - return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64) + return strconv.ParseUint(string(bytes.TrimSpace(data)), 10, 64) } // loopback is a network strategy that provides a basic loopback device