Skip to content

Commit

Permalink
Override reduceUnordered in Task's Nondeterminism
Browse files Browse the repository at this point in the history
Previously we were overriding gatherUnordered which was not actually
called when Task.gatherUnordered was called with its default argument.
Instead chooseAny was being called, which gave very poor performance:

  val start = System.currentTimeMillis
  Task.gatherUnordered((1 to 1000).map(Task.now)).unsafePerformSync
  println(System.currentTimeMillis - start)

For me, the above code before this change gives 563ms.

After this change gives 2ms.

This also makes the underlying Task code consistent with Future. When we
call Task.gatherUnordered, we're now pretty much calling the equivalent
of Future.gatherUnordered.

We should also figure out why Future's chooseAny is very slow, or if
chooseAny is even useful.
  • Loading branch information
puffnfresh committed Oct 24, 2016
1 parent e89cbe8 commit a44517d
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions concurrent/src/main/scala/scalaz/concurrent/Task.scala
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,16 @@ object Task {
new Task ( F.map(F.chooseAny(h.get, t map (_ get))) { case (a, residuals) =>
a.map((_, residuals.map(new Task(_))))
})
override def gatherUnordered[A](fs: Seq[Task[A]]): Task[List[A]] = {
new Task (F.map(F.gatherUnordered(fs.map(_ get)))(eithers =>
Traverse[List].sequenceU(eithers)
))
val AE = Apply[Throwable \/ ?]
override def reduceUnordered[A, M](fs: Seq[Task[A]])(implicit R: Reducer[A, M]): Task[M] = {
import R.monoid
val RR: Reducer[Throwable \/ A, Throwable \/ M] =
Reducer[Throwable \/ A, Throwable \/ M](
_.map(R.unit),
c => AE.apply2(c, _)(R.cons(_, _)),
m => AE.apply2(m, _)(R.snoc(_, _))
)(Monoid.liftMonoid[Throwable \/ ?, M])
new Task(F.reduceUnordered(fs.map(_.get))(RR))
}
def fail[A](e: Throwable): Task[A] = new Task(Future.now(-\/(e)))
def attempt[A](a: Task[A]): Task[Throwable \/ A] = a.attempt
Expand Down

0 comments on commit a44517d

Please sign in to comment.