From 55858923b6267cef89b12cdc18ecd548f8fbbe81 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 7 Apr 2026 04:00:41 +0000 Subject: [PATCH] Optimize @Published array mutations to prevent O(n) UI redraws Replaced `for` loop mutations in `CacheoutViewModel` with functional `.map` assignments to batch `@Published` updates. This changes the O(n) UI invalidations per operation into O(1), improving SwiftUI responsiveness when interacting with large result lists. 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..50b5505 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-04-07 - Functional Updates for Published Arrays +**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. For collections of value types (structs), utilize functional methods like `map` to batch updates into a single property assignment, significantly reducing unnecessary UI recalculations and improving responsiveness. +**Action:** Replace `for` loop mutations on `@Published` array properties with functional transformations like `map`. diff --git a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift index 13a9811..9ffa891 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 + // Optimize: Batch updates to @Published array to prevent O(n) UI redraws + scanResults = scanResults.map { result in + var updated = result + if updated.category.riskLevel == .safe && !updated.isEmpty { + updated.isSelected = true + } + return updated } } func deselectAll() { - for i in scanResults.indices { - scanResults[i].isSelected = false + // Optimize: Batch updates to @Published array to prevent O(n) UI redraws + scanResults = scanResults.map { result in + var updated = result + updated.isSelected = false + return updated } deselectAllNodeModules() } @@ -193,17 +201,32 @@ class CacheoutViewModel: ObservableObject { } func selectStaleNodeModules() { - for i in nodeModulesItems.indices where nodeModulesItems[i].isStale { - nodeModulesItems[i].isSelected = true + // Optimize: Batch updates to @Published array to prevent O(n) UI redraws + nodeModulesItems = nodeModulesItems.map { item in + var updated = item + if updated.isStale { + updated.isSelected = true + } + return updated } } func selectAllNodeModules() { - for i in nodeModulesItems.indices { nodeModulesItems[i].isSelected = true } + // Optimize: Batch updates to @Published array to prevent O(n) UI redraws + nodeModulesItems = nodeModulesItems.map { item in + var updated = item + updated.isSelected = true + return updated + } } func deselectAllNodeModules() { - for i in nodeModulesItems.indices { nodeModulesItems[i].isSelected = false } + // Optimize: Batch updates to @Published array to prevent O(n) UI redraws + nodeModulesItems = nodeModulesItems.map { item in + var updated = item + updated.isSelected = false + return updated + } } /// Menu bar label: show free GB in the tray