diff --git a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift index 27de41a..5fab0f4 100644 --- a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift +++ b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift @@ -106,7 +106,9 @@ class CacheoutViewModel: ObservableObject { } var selectedSize: Int64 { - selectedResults.reduce(0) { $0 + $1.sizeBytes } + // ⚡ Bolt: Optimized by using .lazy.filter before .reduce to prevent intermediate array allocation + // Expected Impact: Reduces memory churn and array allocation overhead on UI updates + scanResults.lazy.filter(\.isSelected).reduce(0) { $0 + $1.sizeBytes } } var formattedSelectedSize: String { diff --git a/Sources/Cacheout/Views/MenuBarView.swift b/Sources/Cacheout/Views/MenuBarView.swift index 9084b59..a738cd2 100644 --- a/Sources/Cacheout/Views/MenuBarView.swift +++ b/Sources/Cacheout/Views/MenuBarView.swift @@ -154,7 +154,9 @@ struct MenuBarView: View { Spacer() statPill( label: "Categories", - value: "\(viewModel.scanResults.filter { !$0.isEmpty }.count)", + // ⚡ Bolt: Optimized by using .lazy.filter.count to prevent intermediate array allocation + // Expected Impact: Eliminates eager O(N) array allocation when computing the category count + value: "\(viewModel.scanResults.lazy.filter { !$0.isEmpty }.count)", color: .blue ) } @@ -177,7 +179,10 @@ struct MenuBarView: View { // MARK: - Top Categories private var topCategories: some View { + // ⚡ Bolt: Optimized by using .lazy.filter before .sorted to prevent intermediate array allocation + // Expected Impact: Reduces memory footprint by avoiding an intermediate filtered array before sorting let top = viewModel.scanResults + .lazy .filter { !$0.isEmpty } .sorted { $0.sizeBytes > $1.sizeBytes } .prefix(5)