[core] fix Async.foreach and derived methods#1514
Merged
Conversation
The bounded path in Async.foreachIndexed had two bugs: 1. Index bug: `f(idx + idx2, v)` used the group index instead of the item offset, producing wrong indices for all groups after the first (e.g. 0,1,2,3,1,2,3,4,2,3 instead of 0..9). 2. Static batching: items were split into equal-sized groups with sequential execution within each group. Workers that finished fast batches sat idle while slow batches continued, wasting concurrency. Replace with work-stealing via foreachIndexedBounded: spawns min(size, concurrency) worker fibers that pull items dynamically from a shared AtomicInt counter. No idle workers, correct indices, same IOPromise/IOTask machinery as foreachIndexed.
hearnadam
approved these changes
Apr 8, 2026
Collaborator
hearnadam
left a comment
There was a problem hiding this comment.
How did I not notice this?
| Fiber.internal.foreachIndexed(Chunk.from(iterable.grouped(groupSize)))((idx, group) => | ||
| Kyo.foreachIndexed(Chunk.from(group))((idx2, v) => isolate.isolate(state, f(idx + idx2, v))) | ||
| ).map(_.use(r => Kyo.foreach(r.flattenChunk)(isolate.restore))) | ||
| val items = Chunk.from(iterable).toIndexed |
Collaborator
There was a problem hiding this comment.
Suggested change
| val items = Chunk.from(iterable).toIndexed | |
| val items = Chunk.Indexed.from(iterable) |
| val size = items.size | ||
| if size == 0 then Fiber.succeed(Chunk.empty) | ||
| else | ||
| val numWorkers = Math.min(size, concurrency) |
Collaborator
There was a problem hiding this comment.
If this is 1, should we dispatch to Kyo.foreach?
Collaborator
Me neither |
ahoy-jon
approved these changes
Apr 11, 2026
…each-improvements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The bounded concurrency in
Async.foreach*and derived methods had two issues:Index bug:
f(idx + idx2, v)used the group index instead of the item offset, producing wrong indices for all groups after the first (e.g. 0,1,2,3,1,2,3,4,2,3 instead of 0..9). See tests.Static batching: items were split into equal-sized groups with sequential execution within each group. Workers that finished fast batches sat idle while slow batches continued, wasting concurrency.
Solution
Replace implementation with a new
Fiber.internal.foreachIndexedBounded: spawnsmin(size, concurrency)worker fibers that pull items dynamically using a sharedAtomicIntcounter.