From 40ce69cc9e155e794fcaa4501e3c6704aead9401 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Tue, 22 Oct 2024 23:25:41 -0700 Subject: [PATCH] libct/cg/fs2: use strings.Cut in setUnified 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). The code is tested by testCgroupResourcesUnified. [1]: https://github.com/golang/go/issues/46336 Signed-off-by: Kir Kolyshkin --- libcontainer/cgroups/fs2/fs2.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libcontainer/cgroups/fs2/fs2.go b/libcontainer/cgroups/fs2/fs2.go index 74dfcfdaf..10e4a3ad8 100644 --- a/libcontainer/cgroups/fs2/fs2.go +++ b/libcontainer/cgroups/fs2/fs2.go @@ -237,11 +237,10 @@ func (m *Manager) setUnified(res map[string]string) error { if errors.Is(err, os.ErrPermission) || errors.Is(err, os.ErrNotExist) { // Check if a controller is available, // to give more specific error if not. - sk := strings.SplitN(k, ".", 2) - if len(sk) != 2 { + c, _, ok := strings.Cut(k, ".") + if !ok { return fmt.Errorf("unified resource %q must be in the form CONTROLLER.PARAMETER", k) } - c := sk[0] if _, ok := m.controllers[c]; !ok && c != "cgroup" { return fmt.Errorf("unified resource %q can't be set: controller %q not available", k, c) }