Skip to content

Commit

Permalink
Optimize ZQuery.collectAllPar For Very Small Collections (#349)
Browse files Browse the repository at this point in the history
* optimize

* format
  • Loading branch information
adamgfraser authored Jul 28, 2022
1 parent d2812dd commit d707caa
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion zio-query/shared/src/main/scala/zio/query/ZQuery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,15 @@ object ZQuery {
)(
f: A => ZQuery[R, E, B]
)(implicit bf: BuildFrom[Collection[A], B, Collection[B]], trace: Trace): ZQuery[R, E, Collection[B]] =
ZQuery(ZIO.foreachPar(Chunk.fromIterable(as))(f(_).step).map(Result.collectAllPar(_).map(bf.fromSpecific(as))))
ZQuery.suspend {
val size = as.size
if (size == 0)
ZQuery.succeed(bf.newBuilder(as).result())
else if (size == 1)
f(as.head).map(bf.newBuilder(as) += _).map(_.result())
else
ZQuery(ZIO.foreachPar(Chunk.fromIterable(as))(f(_).step).map(Result.collectAllPar(_).map(bf.fromSpecific(as))))
}

/**
* Performs a query for each element in a Set, collecting the results
Expand Down

0 comments on commit d707caa

Please sign in to comment.