From 6a7d922e1eb312f216e9405d971e60965168ab15 Mon Sep 17 00:00:00 2001 From: OsakaRuma Date: Thu, 5 Sep 2024 22:18:34 +0800 Subject: [PATCH] optimized the number of threads that need to be started when there are few files in the compressed package during streaming download Signed-off-by: OsakaRuma --- .../FastZipStreamDownloadTask.partial.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Starward.Core.ZipStreamDownload/FastZipStreamDownloadTask.partial.cs b/src/Starward.Core.ZipStreamDownload/FastZipStreamDownloadTask.partial.cs index 1ed96120f..d75f04e50 100644 --- a/src/Starward.Core.ZipStreamDownload/FastZipStreamDownloadTask.partial.cs +++ b/src/Starward.Core.ZipStreamDownload/FastZipStreamDownloadTask.partial.cs @@ -91,18 +91,21 @@ private void AddEntryTask(ZipEntry entry) private async Task WaitExecuteEntryTasksAsync(IZipFileDownloadFactory zipFileDownloadFactory, bool extractFiles, CancellationToken cancellationToken = default) { + cancellationToken.ThrowIfCancellationRequested(); + var existingFileVerifyThreadCount = Math.Min(_entryTaskCount, _existingFileVerifyThreadCount); + var downloadThreadCount = Math.Min(_entryTaskCount, _downloadThreadCount); + var extractAndCrcVerifyThreadCount = Math.Min(_entryTaskCount, _extractAndCrcVerifyThreadCount); try { - cancellationToken.ThrowIfCancellationRequested(); using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); var taskList = new List( - _existingFileVerifyThreadCount + _downloadThreadCount + _extractAndCrcVerifyThreadCount); - for (var i = 0; i < _existingFileVerifyThreadCount; i++) + existingFileVerifyThreadCount + downloadThreadCount + extractAndCrcVerifyThreadCount); + for (var i = 0; i < existingFileVerifyThreadCount; i++) taskList.Add(FileVerifyTaskMethod(cancellationTokenSource.Token)); - for (var i = 0; i < _downloadThreadCount; i++) + for (var i = 0; i < downloadThreadCount; i++) taskList.Add(EntryDownloadTaskMethod(zipFileDownloadFactory, EnableFullStreamDownload, cancellationTokenSource.Token)); - for (var i = 0; i < _extractAndCrcVerifyThreadCount; i++) + for (var i = 0; i < extractAndCrcVerifyThreadCount; i++) taskList.Add(EntryExtractAndFileCrcVerifyTaskMethod(EnableFullStreamDownload, extractFiles, cancellationTokenSource.Token)); taskList.ForEach(t => t.ConfigureAwait(false).GetAwaiter().OnCompleted(() =>