From 9753325bc452f2ac6e2f65cccf0806178e105fc2 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 29 Apr 2026 04:17:17 +0000 Subject: [PATCH] Optimize @Published array mutations in CacheoutViewModel Replaced `for` loop mutations of `@Published` arrays (`scanResults` and `nodeModulesItems`) with `.map` to batch updates into a single assignment. Mutating elements of a `@Published` array individually inside a loop triggers a UI update for every change, causing massive recalculations (O(N)). This reduces updates to exactly 1 per batch operation (e.g., when clicking "Select All"). Co-authored-by: acebytes <2820910+acebytes@users.noreply.github.com> --- .jules/bolt.md | 3 ++ .../ViewModels/CacheoutViewModel.swift | 39 +++++++++++++++---- 2 files changed, 34 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..ca3e35f --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-20 - Batching @Published Array Mutations +**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. +**Action:** Use functional methods like `map` to batch updates into a single property assignment, significantly reducing unnecessary UI recalculations. Always add comments explaining this optimization. diff --git a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift index 13a9811..f21d25a 100644 --- a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift +++ b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift @@ -172,14 +172,22 @@ class CacheoutViewModel: ObservableObject { } func selectAllSafe() { - for i in scanResults.indices where scanResults[i].category.riskLevel == .safe && !scanResults[i].isEmpty { - scanResults[i].isSelected = true + // Optimization: Batch @Published array update to prevent O(N) UI re-renders + scanResults = scanResults.map { result in + var copy = result + if copy.category.riskLevel == .safe && !copy.isEmpty { + copy.isSelected = true + } + return copy } } func deselectAll() { - for i in scanResults.indices { - scanResults[i].isSelected = false + // Optimization: Batch @Published array update to prevent O(N) UI re-renders + scanResults = scanResults.map { result in + var copy = result + copy.isSelected = false + return copy } deselectAllNodeModules() } @@ -193,17 +201,32 @@ class CacheoutViewModel: ObservableObject { } func selectStaleNodeModules() { - for i in nodeModulesItems.indices where nodeModulesItems[i].isStale { - nodeModulesItems[i].isSelected = true + // Optimization: Batch @Published array update to prevent O(N) UI re-renders + nodeModulesItems = nodeModulesItems.map { item in + var copy = item + if copy.isStale { + copy.isSelected = true + } + return copy } } func selectAllNodeModules() { - for i in nodeModulesItems.indices { nodeModulesItems[i].isSelected = true } + // Optimization: Batch @Published array update to prevent O(N) UI re-renders + nodeModulesItems = nodeModulesItems.map { item in + var copy = item + copy.isSelected = true + return copy + } } func deselectAllNodeModules() { - for i in nodeModulesItems.indices { nodeModulesItems[i].isSelected = false } + // Optimization: Batch @Published array update to prevent O(N) UI re-renders + nodeModulesItems = nodeModulesItems.map { item in + var copy = item + copy.isSelected = false + return copy + } } /// Menu bar label: show free GB in the tray