diff options
author | Thomas Otto <th1000s@posteo.net> | 2021-12-09 00:10:37 +0100 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2021-12-11 15:14:41 -0500 |
commit | 5c1612ec0ad328249a611933cf7d13b05a99680a (patch) | |
tree | bac98995a7f8a0bdce39ca80c83abb9fe240eab8 /src | |
parent | 87156fb8710335ba8f933f6372fc4b24e07a2e07 (diff) |
Query fewer processes when searching for the parent
This query now happens for more invocation types, so speed it up.
Call `refresh_process()` only on pids numerically close to the one
of delta itself.
Diffstat (limited to 'src')
-rw-r--r-- | src/utils/process.rs | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/src/utils/process.rs b/src/utils/process.rs index 827e44de..5602dd3f 100644 --- a/src/utils/process.rs +++ b/src/utils/process.rs @@ -275,16 +275,11 @@ trait ProcessInterface { self.refresh_process(sibling_pid).then(|| ())?; self.process(sibling_pid) } - #[cfg(test)] - fn find_sibling_process<F, T>(&mut self, pid: Pid, extract_args: F) -> Option<T> + fn find_sibling_in_refreshed_processes<F, T>(&mut self, pid: Pid, extract_args: &F) -> Option<T> where F: Fn(&[String]) -> ProcessArgs<T>, Self: Sized, { - self.refresh_processes(); - - let this_start_time = self.process(pid)?.start_time(); - /* $ start_blame_of.sh src/main.rs | delta @@ -303,6 +298,8 @@ trait ProcessInterface { */ + let this_start_time = self.process(pid)?.start_time(); + let mut pid_distances = HashMap::<Pid, usize>::new(); let mut collect_parent_pids = |pid, distance| { pid_distances.insert(pid, distance); @@ -407,7 +404,8 @@ where started, so no command line can be parsed. Same if the data was piped from an input file. There might also be intermediary scripts in between or piped input with a gap in pids or (rarely) - randomized pids, so check all processes for the closest match in the process tree. + randomized pids, so check processes for the closest match in the process tree. + The size of this process tree can be reduced by only refreshing selected processes. 100 /usr/bin/some-terminal-emulator 124 \_ -shell @@ -426,19 +424,31 @@ where 567 | \_ less --RAW-CONTROL-CHARS --quit-if-one-screen */ - #[cfg(test)] - { - info.find_sibling_process(my_pid, extract_args) + + let pid_range = my_pid.saturating_sub(10)..my_pid.saturating_add(20); + for p in pid_range.clone() { + // Processes which were not refreshed do not exist for sysinfo, so by selectively + // letting it know about processes the `find_sibling..` function will only + // consider these. + info.refresh_process(p); } - #[cfg(not(test))] - { - None + + match info.find_sibling_in_refreshed_processes(my_pid, &extract_args) { + None => { + #[cfg(test)] + { + info.refresh_processes(); + info.find_sibling_in_refreshed_processes(my_pid, &extract_args) + } + #[cfg(not(test))] + None + } + some => some, } } // Walk up the process tree, calling `f` with the pid and the distance to `starting_pid`. // Prerequisite: `info.refresh_processes()` has been called. -#[cfg(test)] fn iter_parents<P, F>(info: &P, starting_pid: Pid, f: F) where P: ProcessInterface, |