Created
November 29, 2023 10:13
-
-
Save kamilkloch/7edfe8cab15ebf0a75332c40b316ad18 to your computer and use it in GitHub Desktop.
Stream#evalTapChunk benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fs2 | |
import cats.Applicative | |
import cats.effect.{IO, IOApp} | |
import cats.syntax.all._ | |
/** https://github.com/typelevel/fs2/pull/3350 */ | |
object StreamEvalTapChunk extends IOApp.Simple { | |
implicit class StreamOps[F[_], O](s: Stream[F, O]) { | |
def evalTapChunkNew[F2[x] >: F[x] : Applicative, O2](f: O => F2[O2]): Stream[F2, O] = { | |
def tapOutChunk(o: Chunk[O]): Pull[F2, O, Unit] = Pull.eval(o.traverse_(f)) >> Pull.output(o) | |
s.underlying.unconsFlatMap(tapOutChunk).streamNoScope | |
} | |
} | |
def run: IO[Unit] = { | |
val s = Stream(List.fill(100)(1): _*).repeatN(10000).covary[IO] | |
s.evalTapChunkNew(IO.pure).compile.drain.replicateA_(500) >> | |
IO.println("evalTapChunkNew") >> | |
s.evalTapChunkNew(IO.pure).compile.drain.timed.map(_._1.toMillis).flatMap(IO.println).replicateA_(10) >> | |
IO.println("evalTapChunk") >> | |
s.evalTapChunk(IO.pure).compile.drain.timed.map(_._1.toMillis).flatMap(IO.println).replicateA_(10) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
evalTapChunkNew
74
16
15
15
60
29
18
18
22
18
evalTapChunk
164
73
69
63
61
61
64
63
62
63