From 2663d2be9931ad352f4f225074e5187646985b5c Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 16 Jun 2026 03:49:16 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Prevent=20intermediate=20ar?= =?UTF-8?q?ray=20allocations=20in=20CacheoutViewModel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: acebytes <2820910+acebytes@users.noreply.github.com> --- .jules/bolt.md | 3 +++ Sources/Cacheout/ViewModels/CacheoutViewModel.swift | 9 +++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) 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