diff --git a/.jules/bolt.md b/.jules/bolt.md index b2c1128..6afe130 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-30 - Eager Arrays from isEmpty and count checks +**Learning:** In Swift, calling `.filter(condition).count` or `!array.filter(condition).isEmpty` creates intermediate arrays in memory simply to perform an aggregation or boolean check. This contributes to memory churn and GC overhead in SwiftUI rendering loops. +**Action:** Chain `.lazy.filter(condition).count` to prevent the intermediate array allocation, and replace `!array.filter(condition).isEmpty` checks with the O(1) short-circuit evaluation `array.contains(where: condition)`. diff --git a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift index 27de41a..7869601 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: Use .lazy.filter directly to avoid allocating the selectedResults array again + 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: Use .contains(where:) to allow O(1) short-circuit evaluation instead of full O(N) array allocation via selectedResults.isEmpty + 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..700ddd2 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: Avoid intermediate array allocation by using .lazy before .filter and removing call to selectedResults which allocates + Text("This will remove \(viewModel.formattedTotalSelectedSize) from \(viewModel.scanResults.lazy.filter(\.isSelected).count + viewModel.nodeModulesItems.lazy.filter(\.isSelected).count) items.") .font(.body) .foregroundStyle(.secondary) .multilineTextAlignment(.center) diff --git a/Sources/Cacheout/Views/MenuBarView.swift b/Sources/Cacheout/Views/MenuBarView.swift index 9084b59..42e3ca2 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: Avoid intermediate array allocation by using .lazy before .filter + value: "\(viewModel.scanResults.lazy.filter { !$0.isEmpty }.count)", color: .blue ) }