From 9246d01f719d7986b8f659ef43f37bd0ac668af6 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 5 Apr 2026 05:13:58 +0000 Subject: [PATCH] Optimize ProcessMemoryScanner concurrency with sliding window In `scanPIDs`, replace static array chunking with a continuous iterator-based sliding window inside `withTaskGroup`. This avoids tail-latency waiting where the system idles waiting for the slowest task in a chunk to finish before starting the next chunk, maximizing throughput when scanning hundreds of PIDs concurrently. Co-authored-by: acebytes <2820910+acebytes@users.noreply.github.com> --- .jules/bolt.md | 3 ++ .../Memory/ProcessMemoryScanner.swift | 38 +++++++++++-------- 2 files changed, 25 insertions(+), 16 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..c7dc04d --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-04-05 - Optimize ProcessMemoryScanner concurrency with sliding window +**Learning:** In Swift structured concurrency, when processing high-volume tasks using `withTaskGroup`, static chunking limits throughput due to tail latency (waiting for the slowest task in a chunk). A sliding window with an iterator maintains maximum concurrent execution limits continuously. +**Action:** Always use an iterator-based sliding window in `withTaskGroup` instead of static chunking for processing large collections to avoid tail latency bottlenecks. diff --git a/Sources/Cacheout/Memory/ProcessMemoryScanner.swift b/Sources/Cacheout/Memory/ProcessMemoryScanner.swift index 3f8e728..b653d4a 100644 --- a/Sources/Cacheout/Memory/ProcessMemoryScanner.swift +++ b/Sources/Cacheout/Memory/ProcessMemoryScanner.swift @@ -97,29 +97,35 @@ 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..