From 82458ee29569e71db94d84a283fe7272fbedf75f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 18 May 2026 03:56:55 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Parallelize=20bulk=20file?= =?UTF-8?q?=20deletion=20to=20reduce=20UI=20blocking?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactored `removeContents(of:)` in `CacheCleaner` to perform parallel file deletion using `withThrowingTaskGroup` with a sliding window iterator and `Task.detached`. This significantly speeds up permanent deletion of large cache directories and prevents the actor's thread pool from exhaustion, which could otherwise lead to application blocking. Co-authored-by: acebytes <2820910+acebytes@users.noreply.github.com> --- Sources/Cacheout/Cleaner/CacheCleaner.swift | 32 ++++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/Sources/Cacheout/Cleaner/CacheCleaner.swift b/Sources/Cacheout/Cleaner/CacheCleaner.swift index 5e1c18f..4d09a77 100644 --- a/Sources/Cacheout/Cleaner/CacheCleaner.swift +++ b/Sources/Cacheout/Cleaner/CacheCleaner.swift @@ -64,7 +64,7 @@ actor CacheCleaner { if moveToTrash { try await trashDirectory(url) } else { - try removeContents(of: url) + try await removeContents(of: url, fileManager: fileManager) } categoryFreed += result.sizeBytes } catch { @@ -133,12 +133,36 @@ actor CacheCleaner { } } - private func removeContents(of url: URL) throws { + // ⚡ Bolt Optimization: Parallelize bulk I/O operations using a sliding window TaskGroup + // and Task.detached to prevent cooperative thread pool exhaustion. + nonisolated private func removeContents(of url: URL, fileManager: FileManager) async throws { let contents = try fileManager.contentsOfDirectory( at: url, includingPropertiesForKeys: nil ) - for item in contents { - try fileManager.removeItem(at: item) + + try await withThrowingTaskGroup(of: Void.self) { group in + let maxConcurrency = 8 + var iterator = contents.makeIterator() + + for _ in 0..