diff --git a/.jules/bolt.md b/.jules/bolt.md index b2c1128..2d751bb 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -28,3 +28,6 @@ ## 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. +## 2025-10-25 - Short-circuit Evaluation in SwiftUI Computed Properties +**Learning:** Using `.isEmpty` on an eagerly filtered array (e.g., `array.filter { condition }.isEmpty`) or reducing an array to check if a value is greater than zero causes unnecessary O(N) evaluation and temporary array allocations. In SwiftUI, these computed properties are accessed frequently during render cycles, causing memory churn. +**Action:** Use `.contains(where: condition)` to allow O(1) early-exit short-circuiting, and `.lazy.filter` before `.reduce` to avoid intermediate array allocations. diff --git a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift index 27de41a..3317a9e 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 filtering to prevent intermediate array allocation + 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 instead of eagerly filtering + var hasSelection: Bool { scanResults.contains(where: \.isSelected) || nodeModulesItems.contains(where: \.isSelected) } // MARK: - Node Modules computed properties