From 05208d33104306c89b79f8406434d7388970872b Mon Sep 17 00:00:00 2001 From: Eric-Joker <1277243150@qq.com> Date: Thu, 8 Aug 2024 13:43:06 +0800 Subject: [PATCH] opt: reduce downloader writes --- .../Services/Download/InstallGameService.cs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/Starward/Services/Download/InstallGameService.cs b/src/Starward/Services/Download/InstallGameService.cs index d924a108e..a540c8e97 100644 --- a/src/Starward/Services/Download/InstallGameService.cs +++ b/src/Starward/Services/Download/InstallGameService.cs @@ -1211,25 +1211,18 @@ protected async Task DownloadItemAsync(InstallGameItem item, CancellationToken c { if (InstallGameManager.IsEnableSpeedLimit) { - int totalWritten = 0; - while (totalWritten < length) + int totalTokens = 0; + while (totalTokens < length) { - int remaining = length - totalWritten; + int remaining = length - totalTokens; if (TokenBucketRateLimiterExtension.TryAcquire(InstallGameManager.RateLimiter, remaining, out int tokensAcquired, out _)) await Task.Delay(1, cancellationToken).ConfigureAwait(false); else - { - await fs.WriteAsync(buffer.AsMemory(totalWritten, tokensAcquired), cancellationToken).ConfigureAwait(false); - totalWritten += tokensAcquired; - Interlocked.Add(ref _finishBytes, tokensAcquired); - } + totalTokens += tokensAcquired; } } - else - { - await fs.WriteAsync(buffer.AsMemory(0, length), cancellationToken).ConfigureAwait(false); - Interlocked.Add(ref _finishBytes, length); - } + await fs.WriteAsync(buffer.AsMemory(0, length), cancellationToken).ConfigureAwait(false); + Interlocked.Add(ref _finishBytes, length); } } }