From 41bd1d8e3ae21d87ca65d8bf4441b08ea00fc5a5 Mon Sep 17 00:00:00 2001 From: Chris Down Date: Tue, 4 Jun 2024 14:10:54 -0700 Subject: [PATCH] below: pid_cgroup: Avoid intermediate vector alloc Summary: This is a small improvement, around 0.1% or so of CPU, but it's also just cleaner. Reviewed By: dschatzberg Differential Revision: D58084032 fbshipit-source-id: 298baf1e37402bc4ddedfb23d0384f40b0f49afc --- below/procfs/src/lib.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/below/procfs/src/lib.rs b/below/procfs/src/lib.rs index 6d75509c..db8cc0e6 100644 --- a/below/procfs/src/lib.rs +++ b/below/procfs/src/lib.rs @@ -693,14 +693,15 @@ impl ProcReader { // A line starting with "0::" would be an entry for cgroup v2. // Otherwise, the line containing "pids" controller is what we want // for cgroup v1. - let parts: Vec<_> = line.splitn(3, ':').collect(); - if parts.len() == 3 { - if parts[0] == "0" && parts[1] == "" { - cgroup_path = Some(parts[2].to_owned()); - // cgroup v2 takes precedence - break; - } else if parts[1].split(',').any(|c| c == "pids") { - cgroup_path = Some(parts[2].to_owned()); + let mut parts = line.splitn(3, ':'); + if let (Some(hierarchy_id), Some(controller_list), Some(path)) = + (parts.next(), parts.next(), parts.next()) + { + if hierarchy_id == "0" && controller_list.is_empty() { + return Ok(path.to_owned()); + } else if controller_list.split(',').any(|c| c == "pids") { + // Not return, since if cgroup v2 is found it takes precedence + cgroup_path = Some(path.to_owned()); } } }