Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
9 changes: 7 additions & 2 deletions Sources/Cacheout/ViewModels/CacheoutViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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

Expand Down
Loading