From 4cfdc99302958ee0babc3dd8eb02608d814db39e Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 25 May 2026 04:18:27 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Avoid=20eager=20array=20all?= =?UTF-8?q?ocation=20on=20filter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced `.filter { }.isEmpty` checks with `.contains(where:)` to use short-circuit evaluation. Replaced `.filter` + `.reduce` with `.lazy.filter` + `.reduce` to avoid creating intermediate arrays. Replaced `.filter` + `.count` with `.reduce` where `.lazy` cannot be used directly (e.g. string interpolation). This reduces memory churn and prevents O(N) array copies from executing on the main UI thread in SwiftUI view models and views. Co-authored-by: acebytes <2820910+acebytes@users.noreply.github.com> --- .jules/bolt.md | 4 ++++ Sources/Cacheout/ViewModels/CacheoutViewModel.swift | 6 ++++-- Sources/Cacheout/Views/CleanConfirmation.swift | 3 ++- Sources/Cacheout/Views/MenuBarView.swift | 6 ++++-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index b2c1128..3ae5c6a 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -28,3 +28,7 @@ ## 2025-10-24 - Bulk Disk I/O Parallelization and Thread Pool Starvation **Learning:** Both `withTaskGroup` and `Task.detached` schedule their work on Swift's cooperative thread pool, which has only as many threads as the CPU has cores. Running synchronous blocking I/O (like `FileManager.removeItem`) directly inside such tasks ties up cooperative threads — when every thread is parked in a syscall there is nothing left to advance other Swift Concurrency work, which manifests as starvation and (with self-referential `await` chains) outright deadlock. `Task.detached` does not help here: "detached" means unstructured/independent, not "off the cooperative pool." **Action:** To parallelize bulk blocking I/O, combine a sliding-window `withThrowingTaskGroup` (e.g., `maxConcurrency` of 8) with a per-item handoff to a GCD queue: wrap the blocking call in `withCheckedThrowingContinuation` and dispatch it via `DispatchQueue.global(qos: .userInitiated).async { ... continuation.resume(...) }`. The cooperative-pool task only `await`s the continuation, so it never holds a thread while the syscall runs. + +## 2024-05-25 - Avoid Eager Array Allocation on Filter +**Learning:** Using `array.filter { ... }.isEmpty` or `array.filter { ... }.count` evaluates eagerly, allocating an intermediate array and taking O(N) time and memory, which can cause significant UI thread overhead in SwiftUI. +**Action:** Use `.contains(where:)` to short circuit for existence checks. Use `.lazy.filter { ... }.count` or `.reduce(0)` to count without allocating an intermediate array. diff --git a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift index 27de41a..ad5040b 100644 --- a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift +++ b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift @@ -106,7 +106,8 @@ class CacheoutViewModel: ObservableObject { } var selectedSize: Int64 { - selectedResults.reduce(0) { $0 + $1.sizeBytes } + // ⚡ Bolt Optimization: Use .lazy.filter before .reduce to prevent intermediate array allocations + scanResults.lazy.filter(\.isSelected).reduce(0) { $0 + $1.sizeBytes } } var formattedSelectedSize: String { @@ -118,7 +119,8 @@ class CacheoutViewModel: ObservableObject { } var hasResults: Bool { !scanResults.isEmpty || !nodeModulesItems.isEmpty } - var hasSelection: Bool { !selectedResults.isEmpty || selectedNodeModulesSize > 0 } + // ⚡ Bolt Optimization: Use .contains(where:) to short-circuit evaluation instead of eagerly filtering the entire array to check for emptiness + var hasSelection: Bool { scanResults.contains(where: \.isSelected) || selectedNodeModulesSize > 0 } // MARK: - Node Modules computed properties diff --git a/Sources/Cacheout/Views/CleanConfirmation.swift b/Sources/Cacheout/Views/CleanConfirmation.swift index 58f679b..72d04cb 100644 --- a/Sources/Cacheout/Views/CleanConfirmation.swift +++ b/Sources/Cacheout/Views/CleanConfirmation.swift @@ -36,7 +36,8 @@ struct CleanConfirmationSheet: View { Text("Clean Selected Caches?") .font(.title2.bold()) - Text("This will remove \(viewModel.formattedTotalSelectedSize) from \(viewModel.selectedResults.count + viewModel.nodeModulesItems.filter(\.isSelected).count) items.") + // ⚡ Bolt Optimization: Use reduce instead of filter.count to avoid intermediate array allocation + Text("This will remove \(viewModel.formattedTotalSelectedSize) from \(viewModel.selectedResults.count + viewModel.nodeModulesItems.reduce(0) { $0 + ($1.isSelected ? 1 : 0) }) items.") .font(.body) .foregroundStyle(.secondary) .multilineTextAlignment(.center) diff --git a/Sources/Cacheout/Views/MenuBarView.swift b/Sources/Cacheout/Views/MenuBarView.swift index 9084b59..b4483d0 100644 --- a/Sources/Cacheout/Views/MenuBarView.swift +++ b/Sources/Cacheout/Views/MenuBarView.swift @@ -154,7 +154,8 @@ struct MenuBarView: View { Spacer() statPill( label: "Categories", - value: "\(viewModel.scanResults.filter { !$0.isEmpty }.count)", + // ⚡ Bolt Optimization: Use reduce instead of filter.count to avoid intermediate array allocation + value: "\(viewModel.scanResults.reduce(0) { $0 + (!$1.isEmpty ? 1 : 0) })", color: .blue ) } @@ -178,7 +179,8 @@ struct MenuBarView: View { private var topCategories: some View { let top = viewModel.scanResults - .filter { !$0.isEmpty } + // ⚡ Bolt Optimization: Use lazy.filter to avoid intermediate array allocation before sorting + .lazy.filter { !$0.isEmpty } .sorted { $0.sizeBytes > $1.sizeBytes } .prefix(5)