From d188c9738ca9ebd53d978b420570c9a485822ecf Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 3 Jun 2026 03:51:22 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20[performance=20improvement]?= =?UTF-8?q?=20Short-circuit=20evaluation=20and=20lazy=20filtering?= 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 | 8 ++++++-- Sources/Cacheout/Views/MenuBarView.swift | 3 ++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index b2c1128..7950fb8 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -28,3 +28,6 @@ ## 2025-10-24 - Bulk Disk I/O Parallelization and Thread Pool Starvation **Learning:** Both `withTaskGroup` and `Task.detached` schedule their work on Swift's cooperative thread pool, which has only as many threads as the CPU has cores. Running synchronous blocking I/O (like `FileManager.removeItem`) directly inside such tasks ties up cooperative threads — when every thread is parked in a syscall there is nothing left to advance other Swift Concurrency work, which manifests as starvation and (with self-referential `await` chains) outright deadlock. `Task.detached` does not help here: "detached" means unstructured/independent, not "off the cooperative pool." **Action:** To parallelize bulk blocking I/O, combine a sliding-window `withThrowingTaskGroup` (e.g., `maxConcurrency` of 8) with a per-item handoff to a GCD queue: wrap the blocking call in `withCheckedThrowingContinuation` and dispatch it via `DispatchQueue.global(qos: .userInitiated).async { ... continuation.resume(...) }`. The cooperative-pool task only `await`s the continuation, so it never holds a thread while the syscall runs. +## 2024-05-24 - Optimize Collection Processing +**Learning:** `lazy.filter` shouldn't be chained into `count` or `isEmpty` without consideration, but instead of using `isEmpty` on `lazy.filter`, we can use `contains(where:)` to short circuit the execution to prevent evaluating all elements. Also for the `reduce` chaining after `lazy.filter`, we can chain `lazy.filter` before `.reduce` to avoid intermediate array allocations. +**Action:** Chain `.lazy.filter` before `.reduce` or use `contains(where:)` instead of `isEmpty` checks. diff --git a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift index 27de41a..c48e4c2 100644 --- a/Sources/Cacheout/ViewModels/CacheoutViewModel.swift +++ b/Sources/Cacheout/ViewModels/CacheoutViewModel.swift @@ -106,7 +106,8 @@ class CacheoutViewModel: ObservableObject { } var selectedSize: Int64 { - selectedResults.reduce(0) { $0 + $1.sizeBytes } + // ⚡ Bolt Optimization: Use lazy filtering to prevent intermediate array allocation + scanResults.lazy.filter { $0.isSelected }.reduce(0) { $0 + $1.sizeBytes } } var formattedSelectedSize: String { @@ -118,7 +119,10 @@ class CacheoutViewModel: ObservableObject { } var hasResults: Bool { !scanResults.isEmpty || !nodeModulesItems.isEmpty } - var hasSelection: Bool { !selectedResults.isEmpty || selectedNodeModulesSize > 0 } + var hasSelection: Bool { + // ⚡ Bolt Optimization: Use contains(where:) to short-circuit evaluation instead of checking .isEmpty on filtered arrays + scanResults.contains(where: \.isSelected) || selectedNodeModulesSize > 0 + } // MARK: - Node Modules computed properties diff --git a/Sources/Cacheout/Views/MenuBarView.swift b/Sources/Cacheout/Views/MenuBarView.swift index 9084b59..607b85d 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: Use .lazy.filter before .count to prevent intermediate array allocation + value: "\(viewModel.scanResults.lazy.filter { !$0.isEmpty }.count)", color: .blue ) }