Skip to content

Add parFlatTraverseN and parFlatSequenceN - #4468

Open
yhefamly wants to merge 8 commits into
typelevel:series/3.xfrom
yhefamly:add-parflattraversen
Open

yhefamly wants to merge 8 commits into
typelevel:series/3.xfrom
yhefamly:add-parflattraversen

Conversation

@yhefamly

@yhefamly yhefamly commented Aug 8, 2025

Copy link
Copy Markdown
Contributor

Add equivalents of the Parallel methods that allow flattening the result of a limited parallel traversal. These are helpful in case like big database lookups, where a set of IDs is batched, then each batch is queried and produces a collection of results that needs to be flattened into a larger result.

If #4451 is merged, the implementations in this PR should be aligned, either by delegation and flattening afterwards or by using a customised variant of the new code.

Add equivalents of the Parallel methods that allow flattening the result of a limited parallel traversal. These are helpful in case like big database lookups, where a set of IDs is batched, then each batch is queried and produces a collection of results that needs to be flattened into a larger result.
@djspiewak

Copy link
Copy Markdown
Member

This is excellent but I'd definitely like to push pause until #4451 lands just to make sure we avoid creating more conflicting implementations. I'll try to prioritize getting that over the line.

@djspiewak

Copy link
Copy Markdown
Member

Alright! Now that #4451 is somewhat on sane footing, we can safely revisit this. These functions are going to need similar bespoke treatment (and extensive tests). Unfortunately, we also missed the boat on inclusion in 3.7.0 (mostly because it took me forever to fix up the other PR), so this will have to wait for 3.8.0 due to our forward compatibility model.

@djspiewak djspiewak added this to the v3.8.0 milestone Mar 8, 2026
@djspiewak

Copy link
Copy Markdown
Member

Any interest in fixing this up for the latest on series/3.x?

@yhefamly

yhefamly commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

Any interest in fixing this up for the latest on series/3.x?

Yes, absolutely. Might take until next week though that I get to it.

@djspiewak

Copy link
Copy Markdown
Member

Sounds good! I have very spotty availability myself so no shade whatsoever. I'll try to review and merge as quickly as I can

Yannick Heiber added 3 commits September 10, 2026 16:28
…ersen

# Conflicts:
#	tests/shared/src/test/scala/cats/effect/IOSuite.scala
With the rewrite of parTraverseN's internals for performance, the MiniSemaphore approach is no longer optimal. This extracts the parTraverseN rewrite into a private helper that takes a function that converts the result into its final state, either via `sequence` for the parTraverseN case or via `flatSequence` for `parFlatTraverseN`.
@yhefamly

Copy link
Copy Markdown
Contributor Author

@djspiewak The code is ready for review. One parTraverseN test case timed out on one macOS runner – as it succeeded on others and my local macOS, I simply suspect a busy runner running it too slowly. Let me know if you think further investigation is needed.

@djspiewak djspiewak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fairly minor things.

Comment thread kernel/shared/src/main/scala/cats/effect/kernel/GenConcurrent.scala Outdated
assertCompleteAs(test.attempt.void, ())
}

real("parFlatTraverseN - throw when n < 1") {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have some success tests for this function?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

I added ticked success tests for both new methods, asserting the result and that some parallelism is applied. parTraverseN also has a real test, but that comes at a heavy price in terms of test runtime and both effectively share the implementation, so I kept it simple. Happy to add more / real tests if you think they're needed.

Yannick Heiber added 3 commits September 14, 2026 12:57
Ensure parFlatTraverseN and parFlatSequenceN yield the correct results and run in the expected time, ensuring parallelism.
@yhefamly
yhefamly requested a review from djspiewak September 14, 2026 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants