From 7149781fd091e76e1d45043088f1295148a6e884 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Tue, 22 Oct 2024 22:36:26 -0700 Subject: [PATCH] exec: use strings.Cut to parse --cgroup Using strings.Cut (added in Go 1.18, see [1]) results in faster and cleaner code with less allocations (as we're not using a slice). This part of code is covered by tests in tests/integration/exec.bats. [1]: https://github.com/golang/go/issues/46336 Signed-off-by: Kir Kolyshkin --- exec.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/exec.go b/exec.go index 16bbeebfb..b896b4b56 100644 --- a/exec.go +++ b/exec.go @@ -124,20 +124,17 @@ func getSubCgroupPaths(args []string) (map[string]string, error) { paths := make(map[string]string, len(args)) for _, c := range args { // Split into controller:path. - cs := strings.SplitN(c, ":", 3) - if len(cs) > 2 { - return nil, fmt.Errorf("invalid --cgroup argument: %s", c) - } - if len(cs) == 1 { // no controller: prefix + if ctr, path, ok := strings.Cut(c, ":"); ok { + // There may be a few comma-separated controllers. + for _, ctrl := range strings.Split(ctr, ",") { + paths[ctrl] = path + } + } else { + // No controller: prefix. if len(args) != 1 { return nil, fmt.Errorf("invalid --cgroup argument: %s (missing : prefix)", c) } paths[""] = c - } else { - // There may be a few comma-separated controllers. - for _, ctrl := range strings.Split(cs[0], ",") { - paths[ctrl] = cs[1] - } } } return paths, nil