From 844a68dae0c82e82a2cdcd9f0aff8f223a6aabab Mon Sep 17 00:00:00 2001 From: Roy Date: Tue, 13 Oct 2020 18:31:07 +0200 Subject: [PATCH] Improve DeduplicatePrograms by using a set instead of distinct (#7223) Co-authored-by: Roy --- .../Microsoft.Plugin.Program/Programs/Win32Program.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/Win32Program.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/Win32Program.cs index a75be107fe..eacf494548 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/Win32Program.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/Win32Program.cs @@ -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 public override int GetHashCode() { - return new RemoveDuplicatesComparer().GetHashCode(this); + return RemoveDuplicatesComparer.Default.GetHashCode(this); } 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 { + public static readonly RemoveDuplicatesComparer Default = new RemoveDuplicatesComparer(); + public bool Equals(Win32Program app1, Win32Program app2) { if (!string.IsNullOrEmpty(app1.Name) && !string.IsNullOrEmpty(app2.Name) @@ -933,9 +935,8 @@ namespace Microsoft.Plugin.Program.Programs // Deduplication code public static Win32Program[] DeduplicatePrograms(ParallelQuery programs) { - 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 uniquePrograms.ToArray(); + var uniqueExePrograms = programs.Where(x => !(string.IsNullOrEmpty(x.LnkResolvedPath) && ExecutableApplicationExtensions.Contains(Extension(x.FullPath)) && x.AppType != ApplicationType.RunCommand)); + return new HashSet(uniqueExePrograms, new RemoveDuplicatesComparer()).ToArray(); } [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Keeping the process alive but logging the exception")]