[PTRun]Optimize loop gathering results and memory consumption in WindowWalker (#31188)

This commit is contained in:
gokcekantarci
2024-02-26 19:30:33 +03:00
committed by GitHub
parent fef50971af
commit 81a6cde7a5
2 changed files with 47 additions and 37 deletions

View File

@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Microsoft.Plugin.WindowWalker.Properties; using Microsoft.Plugin.WindowWalker.Properties;
using Wox.Infrastructure; using Wox.Infrastructure;
using Wox.Plugin; using Wox.Plugin;
@@ -22,32 +23,22 @@ namespace Microsoft.Plugin.WindowWalker.Components
/// <returns>List of results</returns> /// <returns>List of results</returns>
internal static List<Result> GetResultList(List<SearchResult> searchControllerResults, bool isKeywordSearch, string icon, string infoIcon) internal static List<Result> GetResultList(List<SearchResult> searchControllerResults, bool isKeywordSearch, string icon, string infoIcon)
{ {
bool addExplorerInfo = false; if (searchControllerResults == null || searchControllerResults.Count == 0)
List<Result> resultsList = new List<Result>();
foreach (SearchResult x in searchControllerResults)
{ {
if (string.Equals(x.Result.Process.Name, "explorer.exe", StringComparison.OrdinalIgnoreCase) && x.Result.Process.IsShellProcess) return new List<Result>();
{
addExplorerInfo = true;
} }
resultsList.Add(new Result() List<Result> resultsList = new List<Result>(searchControllerResults.Count);
{ bool addExplorerInfo = searchControllerResults.Any(x =>
Title = x.Result.Title, string.Equals(x.Result.Process.Name, "explorer.exe", StringComparison.OrdinalIgnoreCase) &&
IcoPath = icon, x.Result.Process.IsShellProcess);
SubTitle = GetSubtitle(x.Result),
ContextData = x.Result,
Action = c =>
{
x.Result.SwitchToWindow();
return true;
},
// For debugging you can set the second parameter to true to see more information. // Process each SearchResult to convert it into a Result.
ToolTipData = GetToolTip(x.Result, false), // Using parallel processing if the operation is CPU-bound and the list is large.
}); resultsList = searchControllerResults
} .AsParallel()
.Select(x => CreateResultFromSearchResult(x, icon))
.ToList();
if (addExplorerInfo && isKeywordSearch && !WindowWalkerSettings.Instance.HideExplorerSettingInfo) if (addExplorerInfo && isKeywordSearch && !WindowWalkerSettings.Instance.HideExplorerSettingInfo)
{ {
@@ -57,6 +48,31 @@ namespace Microsoft.Plugin.WindowWalker.Components
return resultsList; return resultsList;
} }
/// <summary>
/// Creates a Result object from a given SearchResult.
/// </summary>
/// <param name="searchResult">The SearchResult object to convert.</param>
/// <param name="icon">The path to the icon that should be used for the Result.</param>
/// <returns>A Result object populated with data from the SearchResult.</returns>
private static Result CreateResultFromSearchResult(SearchResult searchResult, string icon)
{
return new Result
{
Title = searchResult.Result.Title,
IcoPath = icon,
SubTitle = GetSubtitle(searchResult.Result),
ContextData = searchResult.Result,
Action = c =>
{
searchResult.Result.SwitchToWindow();
return true;
},
// For debugging you can set the second parameter to true to see more information.
ToolTipData = GetToolTip(searchResult.Result, false),
};
}
/// <summary> /// <summary>
/// Returns the subtitle for a result /// Returns the subtitle for a result
/// </summary> /// </summary>

View File

@@ -220,8 +220,7 @@ namespace PowerLauncher.Plugin
if (results != null) if (results != null)
{ {
UpdatePluginMetadata(results, metadata, query); UpdateResults(results, metadata, query);
UpdateResultWithActionKeyword(results, query);
} }
}); });
@@ -233,14 +232,6 @@ namespace PowerLauncher.Plugin
metadata.QueryCount += 1; metadata.QueryCount += 1;
metadata.AvgQueryTime = metadata.QueryCount == 1 ? milliseconds : (metadata.AvgQueryTime + milliseconds) / 2; metadata.AvgQueryTime = metadata.QueryCount == 1 ? milliseconds : (metadata.AvgQueryTime + milliseconds) / 2;
if (results != null)
{
foreach (var result in results)
{
result.Metadata = pair.Metadata;
}
}
return results; return results;
} }
catch (Exception e) catch (Exception e)
@@ -251,10 +242,15 @@ namespace PowerLauncher.Plugin
} }
} }
private static List<Result> UpdateResultWithActionKeyword(List<Result> results, Query query) private static void UpdateResults(List<Result> results, PluginMetadata metadata, Query query)
{ {
foreach (Result result in results) foreach (Result result in results)
{ {
result.PluginDirectory = metadata.PluginDirectory;
result.PluginID = metadata.ID;
result.OriginQuery = query;
result.Metadata = metadata;
if (string.IsNullOrEmpty(result.QueryTextDisplay)) if (string.IsNullOrEmpty(result.QueryTextDisplay))
{ {
result.QueryTextDisplay = result.Title; result.QueryTextDisplay = result.Title;
@@ -266,8 +262,6 @@ namespace PowerLauncher.Plugin
result.QueryTextDisplay = string.Format(CultureInfo.CurrentCulture, "{0} {1}", query.ActionKeyword, result.QueryTextDisplay); result.QueryTextDisplay = string.Format(CultureInfo.CurrentCulture, "{0} {1}", query.ActionKeyword, result.QueryTextDisplay);
} }
} }
return results;
} }
public static void UpdatePluginMetadata(List<Result> results, PluginMetadata metadata, Query query) public static void UpdatePluginMetadata(List<Result> results, PluginMetadata metadata, Query query)