Skip to content

Commit

Permalink
Avoid unnecessary closures/delegates in Process (#50496)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Apr 7, 2021
1 parent a7ed1fd commit bc7cf1c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Kill(bool entireProcessTree)

private bool IsSelfOrDescendantOf(Process processOfInterest)
{
if (SafePredicateTest(() => Equals(processOfInterest)))
if (Equals(processOfInterest))
return true;

Process[] allProcesses = GetProcesses();
Expand All @@ -47,7 +47,7 @@ private bool IsSelfOrDescendantOf(Process processOfInterest)
{
foreach (Process candidate in current.GetChildProcesses(allProcesses))
{
if (SafePredicateTest(() => processOfInterest.Equals(candidate)))
if (processOfInterest.Equals(candidate))
return true;

descendantProcesses.Enqueue(candidate);
Expand Down Expand Up @@ -82,7 +82,7 @@ private IReadOnlyList<Process> GetChildProcesses(Process[]? processes = null)

try
{
if (SafePredicateTest(() => IsParentOf(possibleChildProcess)))
if (IsParentOf(possibleChildProcess))
{
childProcesses.Add(possibleChildProcess);
dispose = false;
Expand All @@ -98,19 +98,10 @@ private IReadOnlyList<Process> GetChildProcesses(Process[]? processes = null)
return childProcesses;
}

private bool SafePredicateTest(Func<bool> predicate)
{
try
{
return predicate();
}
catch (Exception e) when (e is InvalidOperationException || e is Win32Exception)
{
// InvalidOperationException signifies conditions such as the process already being dead.
// Win32Exception signifies issues such as insufficient permissions to get details on the process.
// In either case, the predicate couldn't be applied so return the fallback result.
return false;
}
}
private static bool IsProcessInvalidException(Exception e) =>
// InvalidOperationException signifies conditions such as the process already being dead.
// Win32Exception signifies issues such as insufficient permissions to get details on the process.
// In either case, the predicate couldn't be applied so return the fallback result.
e is InvalidOperationException || e is Win32Exception;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,29 @@ private ProcessPriorityClass PriorityClassCore
}

/// <summary>Checks whether the argument is a direct child of this process.</summary>
private bool IsParentOf(Process possibleChildProcess) =>
Id == possibleChildProcess.ParentProcessId;
private bool IsParentOf(Process possibleChildProcess)
{
try
{
return Id == possibleChildProcess.ParentProcessId;
}
catch (Exception e) when (IsProcessInvalidException(e))
{
return false;
}
}

private bool Equals(Process process) =>
Id == process.Id;
private bool Equals(Process process)
{
try
{
return Id == process.Id;
}
catch (Exception e) when (IsProcessInvalidException(e))
{
return false;
}
}

partial void ThrowIfExited(bool refresh)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,17 @@ private bool WaitForInputIdleCore(int milliseconds)
/// <remarks>
/// A child process is a process which has this process's id as its parent process id and which started after this process did.
/// </remarks>
private bool IsParentOf(Process possibleChild) =>
StartTime < possibleChild.StartTime
&& Id == possibleChild.ParentProcessId;
private bool IsParentOf(Process possibleChild)
{
try
{
return StartTime < possibleChild.StartTime && Id == possibleChild.ParentProcessId;
}
catch (Exception e) when (IsProcessInvalidException(e))
{
return false;
}
}

/// <summary>
/// Get the process's parent process id.
Expand All @@ -353,9 +361,17 @@ private unsafe int ParentProcessId
}
}

private bool Equals(Process process) =>
Id == process.Id
&& StartTime == process.StartTime;
private bool Equals(Process process)
{
try
{
return Id == process.Id && StartTime == process.StartTime;
}
catch (Exception e) when (IsProcessInvalidException(e))
{
return false;
}
}

private List<Exception>? KillTree()
{
Expand Down Expand Up @@ -389,7 +405,7 @@ private bool Equals(Process process) =>
(exceptions ??= new List<Exception>()).Add(e);
}

List<(Process Process, SafeProcessHandle Handle)> children = GetProcessHandlePairs(p => SafePredicateTest(() => IsParentOf(p)));
List<(Process Process, SafeProcessHandle Handle)> children = GetProcessHandlePairs((thisProcess, otherProcess) => thisProcess.IsParentOf(otherProcess));
try
{
foreach ((Process Process, SafeProcessHandle Handle) child in children)
Expand All @@ -413,7 +429,7 @@ private bool Equals(Process process) =>
return exceptions;
}

private List<(Process Process, SafeProcessHandle Handle)> GetProcessHandlePairs(Func<Process, bool> predicate)
private List<(Process Process, SafeProcessHandle Handle)> GetProcessHandlePairs(Func<Process, Process, bool> predicate)
{
var results = new List<(Process Process, SafeProcessHandle Handle)>();

Expand All @@ -422,7 +438,7 @@ private bool Equals(Process process) =>
SafeProcessHandle h = SafeGetHandle(p);
if (!h.IsInvalid)
{
if (predicate(p))
if (predicate(this, p))
{
results.Add((p, h));
}
Expand Down

0 comments on commit bc7cf1c

Please sign in to comment.