From f5fa5046202a1615e0d5b679e11dc700a38868a9 Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Sat, 2 Mar 2024 17:51:28 -0600 Subject: [PATCH] De-over-parallelize Versions tab --- Core/Extensions/EnumerableExtensions.cs | 5 +++++ Core/ModuleInstaller.cs | 2 +- Core/Relationships/RelationshipResolver.cs | 8 ++++---- Core/Types/CkanModule.cs | 14 ++++---------- GUI/Controls/ModInfoTabs/Versions.cs | 2 ++ 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Core/Extensions/EnumerableExtensions.cs b/Core/Extensions/EnumerableExtensions.cs index 8c6b30042c..00ed174070 100644 --- a/Core/Extensions/EnumerableExtensions.cs +++ b/Core/Extensions/EnumerableExtensions.cs @@ -68,6 +68,11 @@ public static Dictionary ToDictionary(this ParallelQuery ToConcurrentDictionary(this IEnumerable> pairs) => new ConcurrentDictionary(pairs); + public static IEnumerable AsParallelIf(this IEnumerable source, + bool parallel) + => parallel ? source.AsParallel() + : source; + // https://stackoverflow.com/a/55591477/2422988 public static ParallelQuery WithProgress(this ParallelQuery source, long totalCount, diff --git a/Core/ModuleInstaller.cs b/Core/ModuleInstaller.cs index b1981654b1..6a0b79865a 100644 --- a/Core/ModuleInstaller.cs +++ b/Core/ModuleInstaller.cs @@ -1399,7 +1399,7 @@ public bool CanInstall(List toInstall, .Where(m => m != null); var resolver = new RelationshipResolver(toInstall, installed, opts, registry, crit); - var resolverModList = resolver.ModList().ToList(); + var resolverModList = resolver.ModList(false).ToList(); if (resolverModList.Count >= toInstall.Count(m => !m.IsMetapackage)) { // We can install with no further dependencies diff --git a/Core/Relationships/RelationshipResolver.cs b/Core/Relationships/RelationshipResolver.cs index 250938d030..f0e189f9a1 100644 --- a/Core/Relationships/RelationshipResolver.cs +++ b/Core/Relationships/RelationshipResolver.cs @@ -519,10 +519,10 @@ private bool MightBeInstallable(CkanModule module, /// Returns a list of all modules to install to satisfy the changes required. /// Each mod is after its dependencies and before its reverse dependencies. /// - public IEnumerable ModList() + public IEnumerable ModList(bool parallel = true) => modlist.Values .Distinct() - .AsParallel() + .AsParallelIf(parallel) // Put user choices at the bottom; .OrderBy(bool) -> false first .OrderBy(m => ReasonsFor(m).Any(r => r is SelectionReason.UserRequested)) // Put dependencies before dependers @@ -539,8 +539,8 @@ private static bool AnyRelationship(SelectionReason r) || r is SelectionReason.Recommended || r is SelectionReason.Suggested; - private IEnumerable BreadthFirstSearch(IEnumerable startingGroup, - Func, IEnumerable> getNextGroup) + private static IEnumerable BreadthFirstSearch(IEnumerable startingGroup, + Func, IEnumerable> getNextGroup) { var found = startingGroup.ToHashSet(); var toSearch = new Queue(found); diff --git a/Core/Types/CkanModule.cs b/Core/Types/CkanModule.cs index 2b022d605f..6a1860b3f8 100644 --- a/Core/Types/CkanModule.cs +++ b/Core/Types/CkanModule.cs @@ -339,7 +339,7 @@ public CkanModule( /// /// Inflates a CKAN object from a JSON string. /// - public CkanModule(string json, IGameComparator comparator) + public CkanModule(string json, IGameComparator comparator = null) { try { @@ -355,7 +355,7 @@ public CkanModule(string json, IGameComparator comparator) string.Format(Properties.Resources.CkanModuleDeserialisationError, ex.Message), ex); } - _comparator = comparator; + _comparator = comparator ?? ServiceLocator.Container.Resolve(); CheckHealth(); CalculateSearchables(); } @@ -510,18 +510,12 @@ public static string ToJson(CkanModule module) } /// - /// Generates a CKAN.META object from a string. + /// Generates a CkanModule object from a string. /// Also validates that all required fields are present. /// Throws a BadMetaDataKraken if any fields are missing. /// public static CkanModule FromJson(string json) - { - log.Debug("Inflating comparator object"); - IGameComparator comparator = ServiceLocator.Container.Resolve(); - - log.Debug("Building CkanModule"); - return new CkanModule(json, comparator); - } + => new CkanModule(json); #endregion diff --git a/GUI/Controls/ModInfoTabs/Versions.cs b/GUI/Controls/ModInfoTabs/Versions.cs index a253ad67ac..5f3810e5e1 100644 --- a/GUI/Controls/ModInfoTabs/Versions.cs +++ b/GUI/Controls/ModInfoTabs/Versions.cs @@ -241,6 +241,7 @@ private void checkInstallable(ListViewItem[] items) { if (latestCompatible == null || item.Index < latestCompatible.Index) { + VersionsListView.BeginUpdate(); if (latestCompatible != null) { // Revert color of previous best guess @@ -250,6 +251,7 @@ private void checkInstallable(ListViewItem[] items) latestCompatible = item; item.BackColor = Color.Green; item.ForeColor = Color.White; + VersionsListView.EndUpdate(); } else {