Skip to content

Commit 930cd49

Browse files
committed
libct/cg/fs2: use strings.Cut in parsePSIData
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 code is tested by TestStatCPUPSI. [1]: golang/go#46336 Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 40ce69c commit 930cd49

File tree

1 file changed

+7
-7
lines changed
  • libcontainer/cgroups/fs2

1 file changed

+7
-7
lines changed

libcontainer/cgroups/fs2/psi.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,29 +58,29 @@ func statPSI(dirPath string, file string) (*cgroups.PSIStats, error) {
5858
func parsePSIData(psi []string) (cgroups.PSIData, error) {
5959
data := cgroups.PSIData{}
6060
for _, f := range psi {
61-
kv := strings.SplitN(f, "=", 2)
62-
if len(kv) != 2 {
61+
key, val, ok := strings.Cut(f, "=")
62+
if !ok {
6363
return data, fmt.Errorf("invalid psi data: %q", f)
6464
}
6565
var pv *float64
66-
switch kv[0] {
66+
switch key {
6767
case "avg10":
6868
pv = &data.Avg10
6969
case "avg60":
7070
pv = &data.Avg60
7171
case "avg300":
7272
pv = &data.Avg300
7373
case "total":
74-
v, err := strconv.ParseUint(kv[1], 10, 64)
74+
v, err := strconv.ParseUint(val, 10, 64)
7575
if err != nil {
76-
return data, fmt.Errorf("invalid %s PSI value: %w", kv[0], err)
76+
return data, fmt.Errorf("invalid %s PSI value: %w", key, err)
7777
}
7878
data.Total = v
7979
}
8080
if pv != nil {
81-
v, err := strconv.ParseFloat(kv[1], 64)
81+
v, err := strconv.ParseFloat(val, 64)
8282
if err != nil {
83-
return data, fmt.Errorf("invalid %s PSI value: %w", kv[0], err)
83+
return data, fmt.Errorf("invalid %s PSI value: %w", key, err)
8484
}
8585
*pv = v
8686
}

0 commit comments

Comments
 (0)