From 4162c25b4dcc81e9618c231749f0f1daf75ad8f6 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 31 May 2026 05:31:57 +0000 Subject: [PATCH] Optimize array allocations and evaluation in CacheoutViewModel Replaced eager `.filter` operations with `.lazy.filter` and `.contains(where:)` in computed properties `selectedSize` and `hasSelection`. To avoid unnecessary intermediate array allocations on every UI update, minimizing memory churn and improving rendering performance. Short-circuits evaluation in `hasSelection` from O(n) filtering to O(1) in best case, and eliminates an O(n) intermediate array allocation in `selectedSize`. Co-authored-by: acebytes <2820910+acebytes@users.noreply.github.com> --- Sources/Cacheout/ViewModels/CacheoutViewModel.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift index 27de41a..b611050 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 Optimization: Using .lazy.filter avoids O(n) array allocation before .reduce + scanResults.lazy.filter { $0.isSelected }.reduce(0) { $0 + $1.sizeBytes } } var formattedSelectedSize: String { @@ -118,7 +119,10 @@ class CacheoutViewModel: ObservableObject { } var hasResults: Bool { !scanResults.isEmpty || !nodeModulesItems.isEmpty } - var hasSelection: Bool { !selectedResults.isEmpty || selectedNodeModulesSize > 0 } + var hasSelection: Bool { + // ⚡ Bolt Optimization: Using .contains(where:) instead of checking .isEmpty on a filtered array to short-circuit evaluation + scanResults.contains(where: { $0.isSelected }) || selectedNodeModulesSize > 0 + } // MARK: - Node Modules computed properties