diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..f48f396 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-04-01 - Concurrency actor vs struct issue +**Learning:** `actor` isolates state to a single execution context. Using `withTaskGroup` inside an `actor` limits the concurrency execution as all child tasks inherit the actor's context and run sequentially instead of in parallel. We can optimize CPU/I/O intensive tasks by making `actor` components `struct` when they are stateless and only rely on thread-safe dependencies. Also, batching operations without releasing the task group limits parallelism. By using a sliding window for task queuing, we can maintain concurrency limits and ensure maximum resource utilization. +**Action:** Use `struct` for parallel scanning when state isolation is unnecessary. When using `withTaskGroup` for high-volume tasks, use a sliding window instead of static chunking to maximize concurrent execution. diff --git a/Sources/Cacheout/Memory/ProcessMemoryScanner.swift b/Sources/Cacheout/Memory/ProcessMemoryScanner.swift index 3f8e728..911deb2 100644 --- a/Sources/Cacheout/Memory/ProcessMemoryScanner.swift +++ b/Sources/Cacheout/Memory/ProcessMemoryScanner.swift @@ -97,30 +97,38 @@ actor ProcessMemoryScanner { /// /// Returns the collected entries and the count of EPERM failures. private func scanPIDs(_ pids: [pid_t]) async -> (entries: [ProcessEntryDTO], epermCount: Int) { - // Chunk PIDs to cap concurrency at maxConcurrency. - let chunks = stride(from: 0, to: pids.count, by: maxConcurrency).map { - Array(pids[$0.. [ScanResult] { diff --git a/Sources/Cacheout/Scanner/NodeModulesScanner.swift b/Sources/Cacheout/Scanner/NodeModulesScanner.swift index 3ed4d8c..ea00d41 100644 --- a/Sources/Cacheout/Scanner/NodeModulesScanner.swift +++ b/Sources/Cacheout/Scanner/NodeModulesScanner.swift @@ -28,7 +28,7 @@ import Foundation -actor NodeModulesScanner { +struct NodeModulesScanner { private let fileManager = FileManager.default /// Common directories where developers keep projects