From 51226e671b21be588b1c0ce270537a0a3936cc97 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 15 Apr 2026 05:38:25 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20@Published=20arr?= =?UTF-8?q?ay=20mutations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit replaces loop-based mutations of `@Published` arrays in `CacheoutViewModel` with functional `.map` methods to batch property updates and prevent excessive UI recalculations. Co-authored-by: acebytes <2820910+acebytes@users.noreply.github.com> --- .jules/bolt.md | 3 ++ .../ViewModels/CacheoutViewModel.swift | 43 +++++++++++++++---- 2 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..b5b4908 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-04-15 - Optimizing Published Arrays in ViewModels +**Learning:** In SwiftUI `ObservableObject` view models, mutating individual elements of a `@Published` array property inside a loop triggers a UI update notification for every change. This can be especially inefficient if the array has many elements, as each update forces SwiftUI to recalculate the view hierarchy repeatedly. +**Action:** For collections of value types (structs), utilize functional methods like `.map` to batch updates into a single property assignment, significantly reducing unnecessary UI recalculations. Always add comments explaining this optimization to prevent future developers from 'optimizing' it back to a standard `for` loop, as `.map` can appear slower in standard Swift contexts. diff --git a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift index 13a9811..27dcf51 100644 --- a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift +++ b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift @@ -172,14 +172,26 @@ class CacheoutViewModel: ObservableObject { } func selectAllSafe() { - for i in scanResults.indices where scanResults[i].category.riskLevel == .safe && !scanResults[i].isEmpty { - scanResults[i].isSelected = true + // Optimization: Use .map to batch array updates instead of a loop. + // Mutating individual elements of a @Published array inside a loop + // triggers a UI update for every single change. This reduces it to one. + scanResults = scanResults.map { result in + var r = result + if r.category.riskLevel == .safe && !r.isEmpty { + r.isSelected = true + } + return r } } func deselectAll() { - for i in scanResults.indices { - scanResults[i].isSelected = false + // Optimization: Use .map to batch array updates instead of a loop. + // Mutating individual elements of a @Published array inside a loop + // triggers a UI update for every single change. This reduces it to one. + scanResults = scanResults.map { result in + var r = result + r.isSelected = false + return r } deselectAllNodeModules() } @@ -193,17 +205,32 @@ class CacheoutViewModel: ObservableObject { } func selectStaleNodeModules() { - for i in nodeModulesItems.indices where nodeModulesItems[i].isStale { - nodeModulesItems[i].isSelected = true + // Optimization: Use .map to batch array updates instead of a loop. + nodeModulesItems = nodeModulesItems.map { item in + var m = item + if m.isStale { + m.isSelected = true + } + return m } } func selectAllNodeModules() { - for i in nodeModulesItems.indices { nodeModulesItems[i].isSelected = true } + // Optimization: Use .map to batch array updates instead of a loop. + nodeModulesItems = nodeModulesItems.map { item in + var m = item + m.isSelected = true + return m + } } func deselectAllNodeModules() { - for i in nodeModulesItems.indices { nodeModulesItems[i].isSelected = false } + // Optimization: Use .map to batch array updates instead of a loop. + nodeModulesItems = nodeModulesItems.map { item in + var m = item + m.isSelected = false + return m + } } /// Menu bar label: show free GB in the tray