Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-05-24 - `Array.lazy` Collection Chain Optimization
**Learning:** Chaining array operations like `.filter {}.reduce()` allocates intermediate arrays that can negatively impact performance. The memory and computation cost is high, particularly in views or view models where properties might be accessed repeatedly.
**Action:** Append `.lazy` before chained collection functions like `.filter` and `.map` when reducing an array to a single value to avoid memory allocations and unnecessary array traversals.
4 changes: 2 additions & 2 deletions Sources/Cacheout/ViewModels/CacheoutViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class CacheoutViewModel: ObservableObject {
}

var totalRecoverable: Int64 {
scanResults.filter { !$0.isEmpty }.reduce(0) { $0 + $1.sizeBytes }
scanResults.lazy.filter { !$0.isEmpty }.reduce(0) { $0 + $1.sizeBytes }
}

var hasResults: Bool { !scanResults.isEmpty || !nodeModulesItems.isEmpty }
Expand All @@ -131,7 +131,7 @@ class CacheoutViewModel: ObservableObject {
}

var selectedNodeModulesSize: Int64 {
nodeModulesItems.filter(\.isSelected).reduce(0) { $0 + $1.sizeBytes }
nodeModulesItems.lazy.filter(\.isSelected).reduce(0) { $0 + $1.sizeBytes }
}

var formattedSelectedNodeModulesSize: String {
Expand Down