Repository: opencontainers/runc
File: libcontainer/cgroups/fscommon/utils.go (Go)

Code before:
	key, val, ok := strings.Cut(t, " ")
	if !ok {
		return "", 0, fmt.Errorf("line %q is not in key value format", t)
	}

	value, err := ParseUint(val, 10, 64)
	if err != nil {
		return "", 0, err
	}

	return key, value, nil
}

// GetValueByKey reads space-separated "key value" pairs from the specified
// cgroup file, looking for a specified key, and returns its value as uint64,
// using [ParseUint] for conversion. If the value is not found, 0 is returned.
func GetValueByKey(path, file, key string) (uint64, error) {
	content, err := cgroups.ReadFile(path, file)
	if err != nil {
		return 0, err
	}

	key += " "
	lines := strings.Split(content, "\n")
	for _, line := range lines {
		v, ok := strings.CutPrefix(line, key)
		if ok {
			val, err := ParseUint(v, 10, 64)
			if err != nil {
				err = &ParseError{Path: path, File: file, Err: err}
			}
			return val, err
		}
	}

	return 0, nil
}

// GetCgroupParamUint reads a single uint64 value from the specified cgroup file.
// If the value read is "max", the math.MaxUint64 is returned.
func GetCgroupParamUint(path, file string) (uint64, error) {
	contents, err := GetCgroupParamString(path, file)
	if err != nil {
		return 0, err
	}
	contents = strings.TrimSpace(contents)
	if contents == "max" {
		return math.MaxUint64, nil
	}

	res, err := ParseUint(contents, 10, 64)

Review comment (performance):
I was about to suggest  "perhaps inline the `key + " "` here, because it makes it easier to read  (I  had to look for a second "why are we adding the space here?")

```go
v, ok := strings.CutPrefix(line, key + " ")
```

But with a very  naive benchmark taking some random file as input, looks like it makes quite a difference  in performance;

```
BenchmarkWithVar-10   13308     89859 ns/op	   65552 B/op    2 allocs/op
BenchmarkInline-10     8439    141521 ns/op	   65540 B/op    1 allocs/op
```


Code after:
	key, val, ok := strings.Cut(t, " ")
	if !ok || key == "" || val == "" {
		return "", 0, fmt.Errorf(`line %q is not in "key value" format`, t)
	}

	value, err := ParseUint(val, 10, 64)
	if err != nil {
		return "", 0, err
	}

	return key, value, nil
}

// GetValueByKey reads space-separated "key value" pairs from the specified
// cgroup file, looking for a specified key, and returns its value as uint64,
// using [ParseUint] for conversion. If the value is not found, 0 is returned.
func GetValueByKey(path, file, key string) (uint64, error) {
	content, err := cgroups.ReadFile(path, file)
	if err != nil {
		return 0, err
	}

	key += " "
	lines := strings.Split(content, "\n")
	for _, line := range lines {
		v, ok := strings.CutPrefix(line, key)
		if ok {
			val, err := ParseUint(v, 10, 64)
			if err != nil {
				err = &ParseError{Path: path, File: file, Err: err}
			}
			return val, err
		}
	}

	return 0, nil
}

// GetCgroupParamUint reads a single uint64 value from the specified cgroup file.
// If the value read is "max", the math.MaxUint64 is returned.
func GetCgroupParamUint(path, file string) (uint64, error) {
	contents, err := GetCgroupParamString(path, file)
	if err != nil {
		return 0, err
	}
	if contents == "max" {
		return math.MaxUint64, nil
	}

	res, err := ParseUint(contents, 10, 64)
	if err != nil {