Skip to content

Commit f134871

Browse files
committed
libct/cg/fscommon: ParseKeyValue: use strings.Cut
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). [1]: golang/go#46336 Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent e9855bd commit f134871

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

libcontainer/cgroups/fscommon/utils.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,22 @@ func ParseUint(s string, base, bitSize int) (uint64, error) {
5454
return value, nil
5555
}
5656

57-
// ParseKeyValue parses a space-separated "name value" kind of cgroup
57+
// ParseKeyValue parses a space-separated "key value" kind of cgroup
5858
// parameter and returns its key as a string, and its value as uint64
59-
// (ParseUint is used to convert the value). For example,
59+
// (using [ParseUint] to convert the value). For example,
6060
// "io_service_bytes 1234" will be returned as "io_service_bytes", 1234.
6161
func ParseKeyValue(t string) (string, uint64, error) {
62-
parts := strings.SplitN(t, " ", 3)
63-
if len(parts) != 2 {
62+
key, val, ok := strings.Cut(t, " ")
63+
if !ok {
6464
return "", 0, fmt.Errorf("line %q is not in key value format", t)
6565
}
6666

67-
value, err := ParseUint(parts[1], 10, 64)
67+
value, err := ParseUint(val, 10, 64)
6868
if err != nil {
6969
return "", 0, err
7070
}
7171

72-
return parts[0], value, nil
72+
return key, value, nil
7373
}
7474

7575
// GetValueByKey reads a key-value pairs from the specified cgroup file,

0 commit comments

Comments
 (0)