From 12dc475dd68bc7e79159bf11231282ea7ad23017 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Fri, 20 Mar 2020 11:51:55 -0700 Subject: [PATCH] libcontainer: simplify createCgroupsv2Path fmt.Sprintf is slow and is not needed here, string concatenation would be sufficient. It is also redundant to convert []byte from string and back, since `bytes` package now provides the same functions as `strings`. Use Fields() instead of TrimSpace() and Split(), mainly for readability (note Fields() is somewhat slower than Split() but here it doesn't matter much). Use Join() to prepend the plus signs. Signed-off-by: Kir Kolyshkin --- libcontainer/cgroups/systemd/unified_hierarchy.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/libcontainer/cgroups/systemd/unified_hierarchy.go b/libcontainer/cgroups/systemd/unified_hierarchy.go index 91157b417..6f5a7b5c5 100644 --- a/libcontainer/cgroups/systemd/unified_hierarchy.go +++ b/libcontainer/cgroups/systemd/unified_hierarchy.go @@ -3,6 +3,7 @@ package systemd import ( + "bytes" "fmt" "io/ioutil" "math" @@ -211,15 +212,8 @@ func createCgroupsv2Path(path string) (Err error) { return fmt.Errorf("invalid cgroup path %s", path) } - res := "" - for i, c := range strings.Split(strings.TrimSpace(string(content)), " ") { - if i == 0 { - res = fmt.Sprintf("+%s", c) - } else { - res = res + fmt.Sprintf(" +%s", c) - } - } - resByte := []byte(res) + ctrs := bytes.Fields(content) + res := append([]byte("+"), bytes.Join(ctrs, []byte(" +"))...) current := "/sys/fs" elements := strings.Split(path, "/") @@ -240,7 +234,7 @@ func createCgroupsv2Path(path string) (Err error) { } } if i < len(elements[3:])-1 { - if err := ioutil.WriteFile(filepath.Join(current, "cgroup.subtree_control"), resByte, 0755); err != nil { + if err := ioutil.WriteFile(filepath.Join(current, "cgroup.subtree_control"), res, 0755); err != nil { return err } }