diff --git a/Sources/Cacheout/Memory/RecommendationEngine.swift b/Sources/Cacheout/Memory/RecommendationEngine.swift index d4e93d3..59b52c4 100644 --- a/Sources/Cacheout/Memory/RecommendationEngine.swift +++ b/Sources/Cacheout/Memory/RecommendationEngine.swift @@ -205,10 +205,9 @@ struct RecommendationEngine { } // rosetta_detected - let rosettaProcesses = scanResult.processes.filter { - $0.isRosetta && $0.physFootprint >= Self.rosettaMinFootprint - } - for proc in rosettaProcesses { + // ⚡ Bolt Optimization: Using a `where` clause on the `for` loop instead of an eager `.filter` + // prevents the allocation of an intermediate array, saving memory churn when scanning hundreds of processes. + for proc in scanResult.processes where proc.isRosetta && proc.physFootprint >= Self.rosettaMinFootprint { let footprintGB = Double(proc.physFootprint) / (1024 * 1024 * 1024) recommendations.append(Recommendation( type: .rosettaDetected, @@ -223,8 +222,9 @@ struct RecommendationEngine { } // agent_memory_pressure - let agents = AgentDetector.agentProcesses(from: scanResult.processes) - for agent in agents where agent.physFootprint >= Self.agentMinFootprint { + // ⚡ Bolt Optimization: Checking `isAgent` in the `where` clause rather than running an eager `.filter` + // prevents an intermediate array allocation. + for agent in scanResult.processes where AgentDetector.isAgent(agent) && agent.physFootprint >= Self.agentMinFootprint { let footprintGB = Double(agent.physFootprint) / (1024 * 1024 * 1024) recommendations.append(Recommendation( type: .agentMemoryPressure, diff --git a/Sources/Cacheout/Views/CleanConfirmation.swift b/Sources/Cacheout/Views/CleanConfirmation.swift index 58f679b..4f7dd7c 100644 --- a/Sources/Cacheout/Views/CleanConfirmation.swift +++ b/Sources/Cacheout/Views/CleanConfirmation.swift @@ -36,7 +36,8 @@ struct CleanConfirmationSheet: View { Text("Clean Selected Caches?") .font(.title2.bold()) - Text("This will remove \(viewModel.formattedTotalSelectedSize) from \(viewModel.selectedResults.count + viewModel.nodeModulesItems.filter(\.isSelected).count) items.") + // ⚡ Bolt Optimization: Using `.lazy.filter` prevents an intermediate array allocation here. + Text("This will remove \(viewModel.formattedTotalSelectedSize) from \(viewModel.selectedResults.count + viewModel.nodeModulesItems.lazy.filter(\.isSelected).count) items.") .font(.body) .foregroundStyle(.secondary) .multilineTextAlignment(.center) diff --git a/Sources/Cacheout/Views/MenuBarView.swift b/Sources/Cacheout/Views/MenuBarView.swift index 9084b59..57332b5 100644 --- a/Sources/Cacheout/Views/MenuBarView.swift +++ b/Sources/Cacheout/Views/MenuBarView.swift @@ -154,7 +154,8 @@ struct MenuBarView: View { Spacer() statPill( label: "Categories", - value: "\(viewModel.scanResults.filter { !$0.isEmpty }.count)", + // ⚡ Bolt Optimization: Using `.lazy.filter` before `.count` prevents Swift from allocating an intermediate array. + value: "\(viewModel.scanResults.lazy.filter { !$0.isEmpty }.count)", color: .blue ) } @@ -177,7 +178,10 @@ struct MenuBarView: View { // MARK: - Top Categories private var topCategories: some View { + // ⚡ Bolt Optimization: Chaining `.lazy.filter` before `.sorted` avoids allocating an intermediate array + // during SwiftUI render passes, reducing unnecessary heap allocations. let top = viewModel.scanResults + .lazy .filter { !$0.isEmpty } .sorted { $0.sizeBytes > $1.sizeBytes } .prefix(5)