Skip to content
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

Special case for parEvalMap with max concurrency of 1 #2577

Merged
merged 1 commit into from
Sep 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2014,38 +2014,40 @@ final class Stream[+F[_], +O] private[fs2] (private[fs2] val underlying: Pull[F,
*/
def parEvalMap[F2[x] >: F[x], O2](
maxConcurrent: Int
)(f: O => F2[O2])(implicit F: Concurrent[F2]): Stream[F2, O2] = {
val fstream: F2[Stream[F2, O2]] = for {
chan <- Channel.bounded[F2, F2[Either[Throwable, O2]]](maxConcurrent)
chanReadDone <- F.deferred[Unit]
} yield {
def forkOnElem(o: O): F2[Stream[F2, Unit]] =
for {
value <- F.deferred[Either[Throwable, O2]]
send = chan.send(value.get).as {
Stream.eval(f(o).attempt.flatMap(value.complete(_).void))
)(f: O => F2[O2])(implicit F: Concurrent[F2]): Stream[F2, O2] =
if (maxConcurrent === 1) evalMap(f)
Copy link
Contributor Author

@bastewart bastewart Aug 27, 2021

Choose a reason for hiding this comment

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

There is no direct assertion that maxConcurrent is > 0 in parEvalMap. There is an indirect on though as ultimately parEvalMap calls parJoin which does do this assertion.

else {
val fstream: F2[Stream[F2, O2]] = for {
chan <- Channel.bounded[F2, F2[Either[Throwable, O2]]](maxConcurrent)
chanReadDone <- F.deferred[Unit]
} yield {
def forkOnElem(o: O): F2[Stream[F2, Unit]] =
for {
value <- F.deferred[Either[Throwable, O2]]
send = chan.send(value.get).as {
Stream.eval(f(o).attempt.flatMap(value.complete(_).void))
}
eit <- chanReadDone.get.race(send)
} yield eit match {
case Left(()) => Stream.empty
case Right(stream) => stream
}
eit <- chanReadDone.get.race(send)
} yield eit match {
case Left(()) => Stream.empty
case Right(stream) => stream
}

val background = this
.evalMap(forkOnElem)
.parJoin(maxConcurrent)
.onFinalize(chanReadDone.get.race(chan.close).void)
val background = this
.evalMap(forkOnElem)
.parJoin(maxConcurrent)
.onFinalize(chanReadDone.get.race(chan.close).void)

val foreground =
chan.stream
.evalMap(identity)
.rethrow
.onFinalize(chanReadDone.complete(()).void)
val foreground =
chan.stream
.evalMap(identity)
.rethrow
.onFinalize(chanReadDone.complete(()).void)

foreground.concurrently(background)
foreground.concurrently(background)
}
Stream.eval(fstream).flatten
}
Stream.eval(fstream).flatten
}

/** Like [[Stream#evalMap]], but will evaluate effects in parallel, emitting the results
* downstream. The number of concurrent effects is limited by the `maxConcurrent` parameter.
Expand Down