diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..8de2fc0 --- /dev/null +++ b/.jules/bolt.md @@ -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. diff --git a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift index 13a9811..8bd694d 100644 --- a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift +++ b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift @@ -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 } @@ -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 {