From ea15d0e8d94586d720decb48cc0b6343704ee7ea Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 8 Apr 2026 05:20:32 +0000 Subject: [PATCH] Optimize ProcessMemoryScanner concurrency with sliding window approach Replaces static task chunking in `scanPIDs` with a continuous sliding window iterator inside `withTaskGroup`. This eliminates tail-latency wait times between chunks, keeping the worker pool fully saturated at `maxConcurrency` and improving overall scanning throughput. 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..9717895 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## YYYY-MM-DD - Sliding Window Concurrency +**Learning:** In Swift structured concurrency, when processing high-volume tasks using `withTaskGroup`, utilize a sliding window approach with an iterator instead of static chunking. Static chunking limits throughput due to tail latency (waiting for the slowest task in a chunk), whereas a sliding window maintains maximum concurrent execution limits continuously. +**Action:** Use an iterator inside `withTaskGroup` to add tasks up to the concurrency limit, then add new tasks as previous ones complete. diff --git a/Sources/Cacheout/Memory/ProcessMemoryScanner.swift b/Sources/Cacheout/Memory/ProcessMemoryScanner.swift index 3f8e728..b987f3d 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..