From a1c8541d8b105cafb1230117c76e9c7a1f15a14e Mon Sep 17 00:00:00 2001 From: Michael Jolley Date: Fri, 26 Sep 2025 09:59:23 -0500 Subject: [PATCH] Materializing lists to prevent rescoring (#42017) Read title. Making MainListPage materialize search results so enumerating doesn't rescore the item. --- .../Commands/MainListPage.cs | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/MainListPage.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/MainListPage.cs index b619cebc28..42969f11b8 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/MainListPage.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/MainListPage.cs @@ -32,9 +32,9 @@ public partial class MainListPage : DynamicListPage, private readonly IServiceProvider _serviceProvider; private readonly TopLevelCommandManager _tlcManager; - private IEnumerable>? _filteredItems; - private IEnumerable>? _filteredApps; - private IEnumerable>? _fallbackItems; + private List>? _filteredItems; + private List>? _filteredApps; + private List>? _fallbackItems; private bool _includeApps; private bool _filteredItemsIncludesApps; private int _appResultLimit = 10; @@ -158,13 +158,13 @@ public partial class MainListPage : DynamicListPage, { lock (_tlcManager.TopLevelCommands) { - IEnumerable> limitedApps = Enumerable.Empty>(); + List> limitedApps = new List>(); // Fuzzy matching can produce a lot of results, so we want to limit the // number of apps we show at once if it's a large set. - if (_filteredApps?.Any() == true) + if (_filteredApps?.Count > 0) { - limitedApps = _filteredApps.OrderByDescending(s => s.Score).Take(_appResultLimit); + limitedApps = _filteredApps.OrderByDescending(s => s.Score).Take(_appResultLimit).ToList(); } var items = Enumerable.Empty>() @@ -330,23 +330,23 @@ public partial class MainListPage : DynamicListPage, { newApps = AllAppsCommandProvider.Page.GetItems().ToList(); } - } - if (token.IsCancellationRequested) - { - return; - } - - if (token.IsCancellationRequested) - { - return; + if (token.IsCancellationRequested) + { + return; + } } // Produce a list of everything that matches the current filter. - _filteredItems = ListHelpers.FilterListWithScores(newFilteredItems ?? [], SearchText, ScoreTopLevelItem); + _filteredItems = [.. ListHelpers.FilterListWithScores(newFilteredItems ?? [], SearchText, ScoreTopLevelItem)]; + + if (token.IsCancellationRequested) + { + return; + } // Defaulting scored to 1 but we'll eventually use user rankings - _fallbackItems = newFallbacks.Select(f => new Scored { Item = f, Score = 1 }); + _fallbackItems = [.. newFallbacks.Select(f => new Scored { Item = f, Score = 1 })]; if (token.IsCancellationRequested) { @@ -367,7 +367,12 @@ public partial class MainListPage : DynamicListPage, // but we need to know the limit now to avoid re-scoring apps var appLimit = AllAppsCommandProvider.TopLevelResultLimit; - _filteredApps = scoredApps; + _filteredApps = [.. scoredApps]; + + if (token.IsCancellationRequested) + { + return; + } } RaiseItemsChanged();