From 001f5303a745dd73b3ed80974f0b720cd248910c Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Tue, 11 Mar 2025 23:14:58 +0200 Subject: [PATCH] Replace InvokeAsync with Parallel.ForEachAsync --- DepotDownloader/ContentDownloader.cs | 29 +++++++++++++++---------- DepotDownloader/Util.cs | 32 ---------------------------- 2 files changed, 18 insertions(+), 43 deletions(-) diff --git a/DepotDownloader/ContentDownloader.cs b/DepotDownloader/ContentDownloader.cs index 8ab56d49..1925703f 100644 --- a/DepotDownloader/ContentDownloader.cs +++ b/DepotDownloader/ContentDownloader.cs @@ -915,18 +915,25 @@ namespace DepotDownloader var files = depotFilesData.filteredFiles.Where(f => !f.Flags.HasFlag(EDepotFileFlag.Directory)).ToArray(); var networkChunkQueue = new ConcurrentQueue<(FileStreamData fileStreamData, DepotManifest.FileData fileData, DepotManifest.ChunkData chunk)>(); - await Util.InvokeAsync( - files.Select(file => new Func(async () => - await Task.Run(() => DownloadSteam3AsyncDepotFile(cts, downloadCounter, depotFilesData, file, networkChunkQueue)))), - maxDegreeOfParallelism: Config.MaxDownloads - ); + var parallelOptions = new ParallelOptions + { + MaxDegreeOfParallelism = Config.MaxDownloads, + CancellationToken = cts.Token + }; - await Util.InvokeAsync( - networkChunkQueue.Select(q => new Func(async () => - await Task.Run(() => DownloadSteam3AsyncDepotFileChunk(cts, downloadCounter, depotFilesData, - q.fileData, q.fileStreamData, q.chunk)))), - maxDegreeOfParallelism: Config.MaxDownloads - ); + await Parallel.ForEachAsync(files, parallelOptions, async (file, cancellationToken) => + { + await Task.Yield(); + DownloadSteam3AsyncDepotFile(cts, downloadCounter, depotFilesData, file, networkChunkQueue); + }); + + await Parallel.ForEachAsync(networkChunkQueue, parallelOptions, async (q, cancellationToken) => + { + await DownloadSteam3AsyncDepotFileChunk( + cts, downloadCounter, depotFilesData, + q.fileData, q.fileStreamData, q.chunk + ); + }); // Check for deleted files if updating the depot. if (depotFilesData.previousManifest != null) diff --git a/DepotDownloader/Util.cs b/DepotDownloader/Util.cs index b23eb856..2481b3d5 100644 --- a/DepotDownloader/Util.cs +++ b/DepotDownloader/Util.cs @@ -235,37 +235,5 @@ namespace DepotDownloader return output; } - - public static async Task InvokeAsync(IEnumerable> taskFactories, int maxDegreeOfParallelism) - { - ArgumentNullException.ThrowIfNull(taskFactories); - ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(maxDegreeOfParallelism, 0); - - var queue = taskFactories.ToArray(); - - if (queue.Length == 0) - { - return; - } - - var tasksInFlight = new List(maxDegreeOfParallelism); - var index = 0; - - do - { - while (tasksInFlight.Count < maxDegreeOfParallelism && index < queue.Length) - { - var taskFactory = queue[index++]; - - tasksInFlight.Add(taskFactory()); - } - - var completedTask = await Task.WhenAny(tasksInFlight).ConfigureAwait(false); - - await completedTask.ConfigureAwait(false); - - tasksInFlight.Remove(completedTask); - } while (index < queue.Length || tasksInFlight.Count != 0); - } } }