diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..2f39305 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-03-30 - Replace actors with structs for parallel TaskGroups +**Learning:** In Swift structured concurrency, using an `actor` to manage a `withTaskGroup` where tasks invoke synchronous, blocking I/O (like `FileManager` operations) directly on the actor inadvertently serializes the tasks, preventing parallelism. +**Action:** For stateless components interacting with thread-safe dependencies (like `FileManager`), use `struct`s or `nonisolated` methods to allow tasks in a `TaskGroup` to execute concurrently across threads. diff --git a/Sources/Cacheout/Scanner/CacheScanner.swift b/Sources/Cacheout/Scanner/CacheScanner.swift index 3ce3e9c..07ec84b 100644 --- a/Sources/Cacheout/Scanner/CacheScanner.swift +++ b/Sources/Cacheout/Scanner/CacheScanner.swift @@ -1,13 +1,13 @@ /// # CacheScanner — Parallel Cache Category Scanner /// -/// An `actor` that scans all registered cache categories concurrently using +/// A `struct` that scans all registered cache categories concurrently using /// Swift's structured concurrency (`TaskGroup`). Each category is scanned in /// its own child task for maximum parallelism. /// /// ## Thread Safety /// -/// Uses the `actor` isolation model to ensure thread-safe access to internal state. -/// All public methods are `async` and can be called from any concurrency context. +/// Uses `struct` instead of `actor` to prevent inadvertently serializing +/// synchronous `FileManager` operations inside the `TaskGroup`. /// /// ## Disk Size Calculation /// @@ -26,7 +26,7 @@ import Foundation -actor CacheScanner { +struct CacheScanner { private let fileManager = FileManager.default func scanAll(_ categories: [CacheCategory]) async -> [ScanResult] { diff --git a/Sources/Cacheout/Scanner/NodeModulesScanner.swift b/Sources/Cacheout/Scanner/NodeModulesScanner.swift index 3ed4d8c..2dce2ce 100644 --- a/Sources/Cacheout/Scanner/NodeModulesScanner.swift +++ b/Sources/Cacheout/Scanner/NodeModulesScanner.swift @@ -1,6 +1,6 @@ /// # NodeModulesScanner — Recursive node_modules Finder /// -/// An `actor` that recursively searches common developer project directories +/// A `struct` that recursively searches common developer project directories /// for `node_modules` folders. Designed to find abandoned or stale dependencies /// that consume significant disk space. /// @@ -28,7 +28,7 @@ import Foundation -actor NodeModulesScanner { +struct NodeModulesScanner { private let fileManager = FileManager.default /// Common directories where developers keep projects