Improve DeduplicatePrograms by using a set instead of distinct (#7223)

Co-authored-by: Roy <royvou@hotmailcom>
This commit is contained in:
Roy
2020-10-13 18:31:07 +02:00
committed by GitHub
parent 954705e3a0
commit 844a68dae0

View File

@@ -890,16 +890,18 @@ namespace Microsoft.Plugin.Program.Programs
// Overriding the object.GetHashCode() function to aid in removing duplicates while adding and removing apps from the concurrent dictionary storage // Overriding the object.GetHashCode() function to aid in removing duplicates while adding and removing apps from the concurrent dictionary storage
public override int GetHashCode() public override int GetHashCode()
{ {
return new RemoveDuplicatesComparer().GetHashCode(this); return RemoveDuplicatesComparer.Default.GetHashCode(this);
} }
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
return obj is Win32Program win && new RemoveDuplicatesComparer().Equals(this, win); return obj is Win32Program win && RemoveDuplicatesComparer.Default.Equals(this, win);
} }
private class RemoveDuplicatesComparer : IEqualityComparer<Win32Program> private class RemoveDuplicatesComparer : IEqualityComparer<Win32Program>
{ {
public static readonly RemoveDuplicatesComparer Default = new RemoveDuplicatesComparer();
public bool Equals(Win32Program app1, Win32Program app2) public bool Equals(Win32Program app1, Win32Program app2)
{ {
if (!string.IsNullOrEmpty(app1.Name) && !string.IsNullOrEmpty(app2.Name) if (!string.IsNullOrEmpty(app1.Name) && !string.IsNullOrEmpty(app2.Name)
@@ -933,9 +935,8 @@ namespace Microsoft.Plugin.Program.Programs
// Deduplication code // Deduplication code
public static Win32Program[] DeduplicatePrograms(ParallelQuery<Win32Program> programs) public static Win32Program[] DeduplicatePrograms(ParallelQuery<Win32Program> programs)
{ {
var uniqueExePrograms = programs.Where(x => !(string.IsNullOrEmpty(x.LnkResolvedPath) && ExecutableApplicationExtensions.Contains(Extension(x.FullPath)) && !(x.AppType == ApplicationType.RunCommand))); var uniqueExePrograms = programs.Where(x => !(string.IsNullOrEmpty(x.LnkResolvedPath) && ExecutableApplicationExtensions.Contains(Extension(x.FullPath)) && x.AppType != ApplicationType.RunCommand));
var uniquePrograms = uniqueExePrograms.Distinct(new RemoveDuplicatesComparer()); return new HashSet<Win32Program>(uniqueExePrograms, new RemoveDuplicatesComparer()).ToArray();
return uniquePrograms.ToArray();
} }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Keeping the process alive but logging the exception")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Keeping the process alive but logging the exception")]