diff --git a/.jules/bolt.md b/.jules/bolt.md index a7b49a6..ed2def2 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -32,3 +32,6 @@ ## 2024-05-30 - SwiftUI ForEach and Lazy Collections **Learning:** In SwiftUI, avoid using `.lazy.filter` directly inside a `ForEach` loop, as `ForEach` requires the data to conform to `RandomAccessCollection`, which `LazyFilterSequence` does not. Eagerly compute the filtered array before passing it to the `ForEach` view builder. **Action:** When optimizing SwiftUI views, only apply `.lazy.filter` to properties where you immediately consume the sequence (e.g., calling `.count`, `.reduce`, or `.sorted()`). For data bound to a `ForEach` list, continue using standard eager `.filter`. +## 2024-05-30 - Short-circuiting Computed Properties in SwiftUI +**Learning:** In SwiftUI ViewModels, calculating boolean properties (like `hasSelection`) eagerly allocated intermediate arrays and evaluated entire sums just to check for emptiness (`!array.filter(...).isEmpty` or `array.reduce(...) > 0`). This causes unnecessary memory churn and O(N) CPU operations during frequent redraws. +**Action:** Replace eager array filtering and full map reductions with `.contains(where:)` to allow short-circuit evaluation. This changes the complexity from O(N) to an O(1) best-case, skipping full traversal as soon as the first match is found. diff --git a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift index 27de41a..f0bb0af 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 to avoid allocating an intermediate array + scanResults.lazy.filter(\.isSelected).reduce(0) { $0 + $1.sizeBytes } } var formattedSelectedSize: String { @@ -118,7 +119,11 @@ class CacheoutViewModel: ObservableObject { } var hasResults: Bool { !scanResults.isEmpty || !nodeModulesItems.isEmpty } - var hasSelection: Bool { !selectedResults.isEmpty || selectedNodeModulesSize > 0 } + + // ⚡ Bolt: Use .contains(where:) to allow short-circuit evaluation and avoid full array allocations/reductions + var hasSelection: Bool { + scanResults.contains(where: \.isSelected) || nodeModulesItems.contains(where: \.isSelected) + } // MARK: - Node Modules computed properties