From cb4511c09d9a4896ebf385528979266498ece8fc Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Tue, 29 Jul 2025 21:36:36 +0100 Subject: [PATCH] Instruct parallel foreach to not go over the number of processor threads, it's counter productive. And it should not do it by default but apparently, it does. --- .../Components/Dialogs/ImportRoms.razor.cs | 8 ++++++++ RomRepoMgr.Core/Workers/FileImporter.cs | 8 ++++++++ RomRepoMgr/ViewModels/ImportRomFolderViewModel.cs | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/RomRepoMgr.Blazor/Components/Dialogs/ImportRoms.razor.cs b/RomRepoMgr.Blazor/Components/Dialogs/ImportRoms.razor.cs index 563949d..9e8c760 100644 --- a/RomRepoMgr.Blazor/Components/Dialogs/ImportRoms.razor.cs +++ b/RomRepoMgr.Blazor/Components/Dialogs/ImportRoms.razor.cs @@ -202,6 +202,10 @@ public partial class ImportRoms : ComponentBase _stopwatch.Restart(); Parallel.ForEach(_rootImporter.Files, + new ParallelOptions + { + MaxDegreeOfParallelism = Environment.ProcessorCount + }, file => { _ = InvokeAsync(() => @@ -284,6 +288,10 @@ public partial class ImportRoms : ComponentBase // For each archive Parallel.ForEach(_rootImporter.Archives, + new ParallelOptions + { + MaxDegreeOfParallelism = Environment.ProcessorCount + }, archive => { _ = InvokeAsync(() => diff --git a/RomRepoMgr.Core/Workers/FileImporter.cs b/RomRepoMgr.Core/Workers/FileImporter.cs index 10101b4..e9ab92f 100644 --- a/RomRepoMgr.Core/Workers/FileImporter.cs +++ b/RomRepoMgr.Core/Workers/FileImporter.cs @@ -99,6 +99,10 @@ public sealed class FileImporter ConcurrentBag archives = []; Parallel.ForEach(Files, + new ParallelOptions + { + MaxDegreeOfParallelism = Environment.ProcessorCount + }, file => { SetProgress?.Invoke(this, @@ -170,6 +174,10 @@ public sealed class FileImporter ConcurrentBag archives = []; Parallel.ForEach(Files, + new ParallelOptions + { + MaxDegreeOfParallelism = Environment.ProcessorCount + }, file => { try diff --git a/RomRepoMgr/ViewModels/ImportRomFolderViewModel.cs b/RomRepoMgr/ViewModels/ImportRomFolderViewModel.cs index b9ad202..dec0463 100644 --- a/RomRepoMgr/ViewModels/ImportRomFolderViewModel.cs +++ b/RomRepoMgr/ViewModels/ImportRomFolderViewModel.cs @@ -213,6 +213,10 @@ public sealed partial class ImportRomFolderViewModel : ViewModelBase _stopwatch.Restart(); Parallel.ForEach(_rootImporter.Files, + new ParallelOptions + { + MaxDegreeOfParallelism = Environment.ProcessorCount + }, file => { Dispatcher.UIThread.Post(() => @@ -284,6 +288,10 @@ public sealed partial class ImportRomFolderViewModel : ViewModelBase _stopwatch.Restart(); Parallel.ForEach(_rootImporter.Archives, + new ParallelOptions + { + MaxDegreeOfParallelism = Environment.ProcessorCount + }, archive => { Dispatcher.UIThread.Post(() => @@ -365,6 +373,10 @@ public sealed partial class ImportRomFolderViewModel : ViewModelBase _stopwatch.Restart(); Parallel.ForEach(_rootImporter.Archives, + new ParallelOptions + { + MaxDegreeOfParallelism = Environment.ProcessorCount + }, archive => { Dispatcher.UIThread.Post(() =>