Skip to content

Why doesn't ParallelFlowable.collect(Collector) return a Single? #7836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jackfirth opened this issue Feb 18, 2025 · 3 comments
Closed

Why doesn't ParallelFlowable.collect(Collector) return a Single? #7836

jackfirth opened this issue Feb 18, 2025 · 3 comments
Labels

Comments

@jackfirth
Copy link

From my understanding, ParallelFlowable.collect(Collector) uses the collector to accumulate values from each parallel rail and then combines each rail's final state into a single output value. It emits that value through a returned Flowable<R>, which will only ever emit one value. Why doesn't this API return a Single<T>? Does the choice to not return a Single imply that if I as a caller immediately convert the returned flowable to a single using singleOrError(), I might hit some problem? Also, the documentation seems off. It claims this:

Each parallel rail receives its own Collector.accumulator() and Collector.combiner().

But why would each rail need its own combiner? The Collector interface is designed for parallel collecting to use just the accumulator in each thread, with the combiner only used to combine final results from multiple threads. Looking at the implementation, I see that this is how ParallelCollector works too: each rail only uses the accumulator, and the final parent flow only uses the combiner.

@akarnokd
Copy link
Member

The parallel flowable came from an experimental library. In there, no other reactive type existed thus the serialized output would become a Flowable. We simply didn't re-evaluate the result type after integration with RxJava.

As for the Collector, we looked at examples and the documentation and it was unclear if it is safe to reuse the same accumulator or combiner returned from it across the rails.

@jackfirth
Copy link
Author

It's definitely safe, since that's exactly how parallel streams work.

@akarnokd
Copy link
Member

Looking at some standard Collectors, it appears the methods return a constant lambda so calling them once or multiple times should have no practical negative consequence.

https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/share/classes/java/util/stream/Collectors.java#L195

@akarnokd akarnokd closed this as not planned Won't fix, can't repro, duplicate, stale Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants