Skip to content

Commit b4347b3

Browse files
⚡ Bolt: Use for-where loops to eliminate intermediate array allocations during recommendation generation (#407)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: acebytes <2820910+acebytes@users.noreply.github.com>
1 parent 65cbe31 commit b4347b3

2 files changed

Lines changed: 5 additions & 6 deletions

File tree

.jules/bolt.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@
3232
## 2024-05-30 - SwiftUI ForEach and Lazy Collections
3333
**Learning:** In SwiftUI, avoid using `.lazy.filter` directly inside a `ForEach` loop, as `ForEach` requires the data to conform to `RandomAccessCollection`, which `LazyFilterSequence` does not. Eagerly compute the filtered array before passing it to the `ForEach` view builder.
3434
**Action:** When optimizing SwiftUI views, only apply `.lazy.filter` to properties where you immediately consume the sequence (e.g., calling `.count`, `.reduce`, or `.sorted()`). For data bound to a `ForEach` list, continue using standard eager `.filter`.
35+
## 2025-04-13 - Loop Filtering and Intermediate Allocations
36+
**Learning:** In Swift, using `.filter` before a `for` loop eagerly allocates a new intermediate array, causing unnecessary memory churn and processing overhead, especially during frequent operations like system monitoring.
37+
**Action:** Use `for ... where` clauses instead of eagerly filtering the collection prior to the loop (e.g., `for item in array where condition { ... }`). This prevents the allocation of intermediate arrays and improves efficiency.

Sources/Cacheout/Memory/RecommendationEngine.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,7 @@ struct RecommendationEngine {
205205
}
206206

207207
// rosetta_detected
208-
let rosettaProcesses = scanResult.processes.filter {
209-
$0.isRosetta && $0.physFootprint >= Self.rosettaMinFootprint
210-
}
211-
for proc in rosettaProcesses {
208+
for proc in scanResult.processes where proc.isRosetta && proc.physFootprint >= Self.rosettaMinFootprint {
212209
let footprintGB = Double(proc.physFootprint) / (1024 * 1024 * 1024)
213210
recommendations.append(Recommendation(
214211
type: .rosettaDetected,
@@ -223,8 +220,7 @@ struct RecommendationEngine {
223220
}
224221

225222
// agent_memory_pressure
226-
let agents = AgentDetector.agentProcesses(from: scanResult.processes)
227-
for agent in agents where agent.physFootprint >= Self.agentMinFootprint {
223+
for agent in scanResult.processes where AgentDetector.isAgent(agent) && agent.physFootprint >= Self.agentMinFootprint {
228224
let footprintGB = Double(agent.physFootprint) / (1024 * 1024 * 1024)
229225
recommendations.append(Recommendation(
230226
type: .agentMemoryPressure,

0 commit comments

Comments
 (0)