scalaz/scalaz-stream — pull request #245 by stucchio (2014-09-23 00:03:16+00:00)
Title: Faster sink

So this pull req displays two approaches for speeding up `process.to(sink)` for a particular common type of sink. The specific variety of `Sink` is the constant `Sink`, which is just repeated application of a function `O => F2[Unit]`.

The first approach is simply a function `to(f: O => F[Unit])`.

The second is a `ConstantProcess` case class, together with an implicit function to turn it into a constant process. Customized versions of many functions can be created to work with this - I demonstrate a custom version of `to` and `zipWith`.

In the benchmark below, this speeds things up from 21 seconds to about 7.

def benchmark {
val p = emitRange(1,1024*1024)

var i = 0
def f(t: Int): Task[Unit] = { Task { i = (i + t) % 127 } }
val fp = Process.constant(f _)

p.to(fp).run.run
val startTimeMap = System.currentTimeMillis
p.to(fp).run.run
println("End time: " + (System.currentTimeMillis - startTimeMap))
}