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
Expand Up @@ -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.
## 2026-03-20 - Prevent intermediate array allocations in computed properties
**Learning:** Eager `.filter` operations in SwiftUI computed properties or view bodies create temporary arrays, causing memory churn and frequent garbage collection. Additionally, checking `.isEmpty` on a filtered array forces full evaluation of the collection.
**Action:** Chain `.lazy.filter` before terminal operations like `.reduce`, `.count`, or `.sorted()`, and use `.contains(where:)` to short-circuit existence checks.
9 changes: 7 additions & 2 deletions Sources/Cacheout/ViewModels/CacheoutViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ class CacheoutViewModel: ObservableObject {
}

var selectedSize: Int64 {
selectedResults.reduce(0) { $0 + $1.sizeBytes }
// ⚑ Bolt Optimization: Use .lazy.filter to avoid allocating an intermediate array.
// Expected impact: Reduces memory churn during SwiftUI view updates when selections change.
scanResults.lazy.filter(\.isSelected).reduce(0) { $0 + $1.sizeBytes }
}

var formattedSelectedSize: String {
Expand All @@ -118,7 +120,10 @@ class CacheoutViewModel: ObservableObject {
}

var hasResults: Bool { !scanResults.isEmpty || !nodeModulesItems.isEmpty }
var hasSelection: Bool { !selectedResults.isEmpty || selectedNodeModulesSize > 0 }
// ⚑ Bolt Optimization: Replace !selectedResults.isEmpty with scanResults.contains(where: \.isSelected)
// to avoid eager array allocation and short-circuit evaluation.
// Expected impact: Reduces O(N) allocation to O(1) early exit when checking selection state.
var hasSelection: Bool { scanResults.contains(where: \.isSelected) || selectedNodeModulesSize > 0 }

// MARK: - Node Modules computed properties

Expand Down
7 changes: 6 additions & 1 deletion Sources/Cacheout/Views/MenuBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ struct MenuBarView: View {
Spacer()
statPill(
label: "Categories",
value: "\(viewModel.scanResults.filter { !$0.isEmpty }.count)",
// ⚑ Bolt Optimization: Add .lazy to avoid allocating an intermediate array before counting.
// Expected impact: Prevents a temporary array allocation during MenuBarView render.
value: "\(viewModel.scanResults.lazy.filter { !$0.isEmpty }.count)",
color: .blue
)
}
Expand All @@ -177,7 +179,10 @@ struct MenuBarView: View {
// MARK: - Top Categories

private var topCategories: some View {
// ⚑ Bolt Optimization: Add .lazy to prevent an intermediate array allocation before sorting.
// Expected impact: Reduces memory overhead during top category calculation.
let top = viewModel.scanResults
.lazy
.filter { !$0.isEmpty }
.sorted { $0.sizeBytes > $1.sizeBytes }
.prefix(5)
Expand Down
Loading