From 895f89c97358ef22bd962ea8baa5d8caa102e642 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 25 Mar 2026 04:05:59 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Add=20`.lazy`=20to=20array?= =?UTF-8?q?=20chains=20in=20CacheoutViewModel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: acebytes <2820910+acebytes@users.noreply.github.com> --- .jules/bolt.md | 3 +++ Sources/Cacheout/ViewModels/CacheoutViewModel.swift | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 .jules/bolt.md 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 {