-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New special and bench modules + StateIO
- Loading branch information
Luka Jacobowitz
committed
Oct 12, 2018
1 parent
830e1e8
commit a4d95d2
Showing
5 changed files
with
200 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cats.mtl.bench | ||
|
||
import cats.data.StateT | ||
import cats.effect.IO | ||
import org.openjdk.jmh.annotations._ | ||
import java.util.concurrent.TimeUnit | ||
|
||
import cats.mtl.special.StateIO | ||
|
||
@BenchmarkMode(Array(Mode.Throughput)) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
class StateBench { | ||
|
||
def leftFlatMapSpecial(bound: Int): Int = { | ||
def loop(i: Int): StateIO[Int, Int] = | ||
if (i > bound) StateIO.pure(i) | ||
else StateIO.pure[Int, Int](i + 1).flatMap(loop) | ||
|
||
StateIO.pure[Int, Int](0).flatMap(loop).unsafeRunSyncS(0) | ||
} | ||
|
||
def leftFlatMapStateT(bound: Int): Int = { | ||
def loop(i: Int): StateT[IO, Int, Int] = | ||
if (i > bound) StateT.pure[IO, Int, Int](i) | ||
else StateT.pure[IO, Int, Int](i + 1).flatMap(loop) | ||
|
||
StateT.pure[IO, Int, Int](0).flatMap(loop).runS(0).unsafeRunSync() | ||
} | ||
|
||
@Benchmark | ||
def leftAssociatedBindSpecial(): Int = leftFlatMapSpecial(100000) | ||
|
||
@Benchmark | ||
def leftAssociatedBindStateT(): Int = leftFlatMapStateT(100000) | ||
|
||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package cats.mtl.special | ||
|
||
import cats.Monad | ||
import cats.effect.concurrent.Ref | ||
import cats.effect._ | ||
import cats.mtl.MonadState | ||
|
||
/* Performant counterpart to StateT[IO, S, A] */ | ||
case class StateIO[S, A]private (private val value: IO[A]) { | ||
def unsafeRunAsync(s: S)(f: Either[Throwable, (S, A)] => Unit): Unit = | ||
StateIO.refInstance[S].set(s) | ||
.flatMap(_ => value.flatMap(a => StateIO.refInstance[S].get.map(s => (s, a)))) | ||
.unsafeRunAsync(f) | ||
|
||
def unsafeRunSync(s: S): (S, A) = | ||
StateIO.refInstance[S].set(s) | ||
.flatMap(_ => value.flatMap(a => StateIO.refInstance[S].get.map(s => (s, a)))) | ||
.unsafeRunSync() | ||
|
||
def unsafeRunSyncA(s: S): A = | ||
StateIO.refInstance[S].set(s).flatMap(_ => value).unsafeRunSync() | ||
|
||
def unsafeRunSyncS(s: S): S = | ||
StateIO.refInstance[S].set(s) | ||
.flatMap(_ => value.flatMap(_ => StateIO.refInstance[S].get)) | ||
.unsafeRunSync() | ||
|
||
def flatMap[B](f: A => StateIO[S, B]): StateIO[S, B] = | ||
StateIO(value.flatMap(a => f(a).value)) | ||
|
||
def map[B](f: A => B): StateIO[S, B] = | ||
StateIO(value.map(f)) | ||
|
||
def get: StateIO[S, S] = | ||
flatMap(_ => StateIO.get[S]) | ||
} | ||
|
||
|
||
|
||
object StateIO extends StateIOImpl { | ||
|
||
def liftF[S, A](fa: IO[A]): StateIO[S, A] = StateIO(fa) | ||
|
||
def get[S]: StateIO[S, S] = StateIO(refInstance[S].get) | ||
|
||
def modify[S](f: S => S): StateIO[S, Unit] = StateIO(refInstance[S].update(f)) | ||
|
||
def set[S](s: S): StateIO[S, Unit] = StateIO(refInstance[S].set(s)) | ||
|
||
def pure[S, A](a: A): StateIO[S, A] = StateIO(IO.pure(a)) | ||
|
||
def create[S, A](f: S => IO[(S, A)]): StateIO[S, A] = | ||
StateIO(refInstance[S].get.flatMap(s => f(s).flatMap { | ||
case (s, a) => refInstance[S].set(s).map(_ => a) | ||
})) | ||
|
||
implicit def monadStateStateIO[S](implicit ctx: ContextShift[IO]): MonadState[StateIO[S, ?], S] = new MonadState[StateIO[S, ?], S] { | ||
val monad: Monad[StateIO[S, ?]] = monadErrorStateIO | ||
|
||
def get: StateIO[S, S] = StateIO.get[S] | ||
|
||
def modify(f: S => S): StateIO[S, Unit] = StateIO.modify(f) | ||
|
||
def set(s: S): StateIO[S, Unit] = StateIO.set(s) | ||
|
||
def inspect[A](f: S => A): StateIO[S, A] = StateIO.get.map(f) | ||
} | ||
|
||
implicit def monadErrorStateIO[S](implicit ctx: ContextShift[IO]): Concurrent[StateIO[S, ?]] = | ||
new Concurrent[StateIO[S, ?]] { | ||
|
||
type Fiber[A] = cats.effect.Fiber[StateIO[S, ?], A] | ||
|
||
def flatMap[A, B](fa: StateIO[S, A])(f: A => StateIO[S, B]): StateIO[S, B] = | ||
fa.flatMap(f) | ||
|
||
override def suspend[A](thunk: => StateIO[S, A]): StateIO[S, A] = | ||
StateIO.liftF(IO.suspend(thunk.value)) | ||
|
||
def bracketCase[A, B](acquire: StateIO[S, A]) | ||
(use: A => StateIO[S, B]) | ||
(release: (A, ExitCase[Throwable]) => StateIO[S, Unit]): StateIO[S, B] = | ||
StateIO(acquire.value.bracketCase(a => use(a).value)((a, ec) => release(a, ec).value)) | ||
|
||
def async[A](k: (Either[Throwable, A] => Unit) => Unit): StateIO[S, A] = | ||
StateIO.liftF(IO.async(k)) | ||
|
||
def asyncF[A](k: (Either[Throwable, A] => Unit) => StateIO[S, Unit]): StateIO[S, A] = | ||
StateIO.liftF(IO.asyncF(cb => k(cb).value)) | ||
|
||
override def cancelable[A](k: (Either[Throwable, A] => Unit) => CancelToken[StateIO[S, ?]]): StateIO[S, A] = | ||
StateIO.liftF(IO.cancelable[A](cb => k(cb).value)) | ||
|
||
def start[A](fa: StateIO[S, A]): StateIO[S, Fiber[A]] = | ||
StateIO.liftF[S, Fiber[A]](fa.value.start.map(fiberT)) | ||
|
||
|
||
def racePair[A, B](fa: StateIO[S, A], fb: StateIO[S, B]): StateIO[S, Either[(A, Fiber[B]), (Fiber[A], B)]] = | ||
StateIO(IO.racePair(fa.value, fb.value).map { | ||
case Left((a, fib)) => Left((a, fiberT(fib))) | ||
case Right((fib, b)) => Right((fiberT(fib), b)) | ||
}) | ||
|
||
|
||
override def map[A, B](fa: StateIO[S, A])(f: A => B): StateIO[S, B] = fa.map(f) | ||
|
||
def pure[A](x: A): StateIO[S, A] = StateIO.pure(x) | ||
|
||
def raiseError[A](e: Throwable): StateIO[S, A] = StateIO(IO.raiseError(e)) | ||
|
||
def handleErrorWith[A](fa: StateIO[S, A])(f: Throwable => StateIO[S, A]): StateIO[S, A] = | ||
StateIO(fa.value.handleErrorWith(e => f(e).value)) | ||
|
||
def tailRecM[A, B](a: A)(f: A => StateIO[S, Either[A, B]]): StateIO[S, B] = | ||
StateIO(Monad[IO].tailRecM(a)(a => f(a).value)) | ||
|
||
protected def fiberT[A](fiber: cats.effect.Fiber[IO, A]): Fiber[A] = | ||
Fiber(StateIO(fiber.join), StateIO(fiber.cancel)) | ||
} | ||
} | ||
|
||
|
||
|
||
private[special] sealed trait StateIOImpl { | ||
|
||
/* There be dragons */ | ||
private val ref: Ref[IO, Any] = Ref.unsafe[IO, Any](null) | ||
|
||
private[special] def refInstance[S]: Ref[IO, S] = ref.asInstanceOf[Ref[IO, S]] | ||
} |