From feceb1954d4c7cfc70c34c47cd4c3f4434fc0e42 Mon Sep 17 00:00:00 2001 From: Kamil Kloch Date: Mon, 11 Sep 2023 16:31:21 +0200 Subject: [PATCH 1/3] Extract parasitic EC outside `unsafeRunAsync`, inline `unsafeRunAndForget`. --- .../scala/cats/effect/std/Dispatcher.scala | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/std/shared/src/main/scala/cats/effect/std/Dispatcher.scala b/std/shared/src/main/scala/cats/effect/std/Dispatcher.scala index 4d08927e33..d9b61211cd 100644 --- a/std/shared/src/main/scala/cats/effect/std/Dispatcher.scala +++ b/std/shared/src/main/scala/cats/effect/std/Dispatcher.scala @@ -17,16 +17,16 @@ package cats.effect.std import cats.effect.kernel.{Async, Outcome, Resource} +import cats.effect.std.Dispatcher.parasiticEC import cats.syntax.all._ +import java.util.concurrent.ThreadLocalRandom +import java.util.concurrent.atomic.{AtomicBoolean, AtomicReference} import scala.annotation.tailrec import scala.collection.mutable import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.{Failure, Success} -import java.util.concurrent.ThreadLocalRandom -import java.util.concurrent.atomic.{AtomicBoolean, AtomicReference} - /** * A fiber-based supervisor utility for evaluating effects across an impure boundary. This is * useful when working with reactive interfaces that produce potentially many values (as opposed @@ -68,28 +68,28 @@ trait Dispatcher[F[_]] extends DispatcherPlatform[F] { /** * Submits an effect to be executed with fire-and-forget semantics. */ - def unsafeRunAndForget[A](fa: F[A]): Unit = { - unsafeRunAsync(fa) { - case Left(t) => t.printStackTrace() - case Right(_) => () - } - } + def unsafeRunAndForget[A](fa: F[A]): Unit = + unsafeToFutureCancelable(fa) + ._1 + .onComplete { + case Failure(ex) => ex.printStackTrace() + case _ => () + }(parasiticEC) // package-private because it's just an internal utility which supports specific implementations // anyone who needs this type of thing should use unsafeToFuture and then onComplete - private[std] def unsafeRunAsync[A](fa: F[A])(cb: Either[Throwable, A] => Unit): Unit = { - // this is safe because the only invocation will be cb - implicit val parasitic: ExecutionContext = new ExecutionContext { - def execute(runnable: Runnable) = runnable.run() - def reportFailure(t: Throwable) = t.printStackTrace() - } - - unsafeToFuture(fa).onComplete(t => cb(t.toEither)) - } + private[std] def unsafeRunAsync[A](fa: F[A])(cb: Either[Throwable, A] => Unit): Unit = + unsafeToFutureCancelable(fa)._1.onComplete(t => cb(t.toEither))(parasiticEC) } object Dispatcher { + private val parasiticEC: ExecutionContext = new ExecutionContext { + def execute(runnable: Runnable) = runnable.run() + + def reportFailure(t: Throwable) = t.printStackTrace() + } + private[this] val Cpus: Int = Runtime.getRuntime().availableProcessors() private[this] val Noop: () => Unit = () => () From 102f9fe0b7c06402458f632d07475c2a1a10e6c6 Mon Sep 17 00:00:00 2001 From: Kamil Kloch Date: Mon, 11 Sep 2023 16:41:57 +0200 Subject: [PATCH 2/3] Cosmetics. --- std/shared/src/main/scala/cats/effect/std/Dispatcher.scala | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/std/shared/src/main/scala/cats/effect/std/Dispatcher.scala b/std/shared/src/main/scala/cats/effect/std/Dispatcher.scala index d9b61211cd..3937f9b1bc 100644 --- a/std/shared/src/main/scala/cats/effect/std/Dispatcher.scala +++ b/std/shared/src/main/scala/cats/effect/std/Dispatcher.scala @@ -20,13 +20,14 @@ import cats.effect.kernel.{Async, Outcome, Resource} import cats.effect.std.Dispatcher.parasiticEC import cats.syntax.all._ -import java.util.concurrent.ThreadLocalRandom -import java.util.concurrent.atomic.{AtomicBoolean, AtomicReference} import scala.annotation.tailrec import scala.collection.mutable import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.{Failure, Success} +import java.util.concurrent.ThreadLocalRandom +import java.util.concurrent.atomic.{AtomicBoolean, AtomicReference} + /** * A fiber-based supervisor utility for evaluating effects across an impure boundary. This is * useful when working with reactive interfaces that produce potentially many values (as opposed From 279fc23dcc480e70ea0be1b2bdf550e422189ca2 Mon Sep 17 00:00:00 2001 From: Kamil Kloch Date: Mon, 11 Sep 2023 17:00:41 +0200 Subject: [PATCH 3/3] Cosmetics. --- .../src/main/scala/cats/effect/std/Dispatcher.scala | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/std/shared/src/main/scala/cats/effect/std/Dispatcher.scala b/std/shared/src/main/scala/cats/effect/std/Dispatcher.scala index 3937f9b1bc..943aa74889 100644 --- a/std/shared/src/main/scala/cats/effect/std/Dispatcher.scala +++ b/std/shared/src/main/scala/cats/effect/std/Dispatcher.scala @@ -70,17 +70,15 @@ trait Dispatcher[F[_]] extends DispatcherPlatform[F] { * Submits an effect to be executed with fire-and-forget semantics. */ def unsafeRunAndForget[A](fa: F[A]): Unit = - unsafeToFutureCancelable(fa) - ._1 - .onComplete { - case Failure(ex) => ex.printStackTrace() - case _ => () - }(parasiticEC) + unsafeToFuture(fa).onComplete { + case Failure(ex) => ex.printStackTrace() + case _ => () + }(parasiticEC) // package-private because it's just an internal utility which supports specific implementations // anyone who needs this type of thing should use unsafeToFuture and then onComplete private[std] def unsafeRunAsync[A](fa: F[A])(cb: Either[Throwable, A] => Unit): Unit = - unsafeToFutureCancelable(fa)._1.onComplete(t => cb(t.toEither))(parasiticEC) + unsafeToFuture(fa).onComplete(t => cb(t.toEither))(parasiticEC) } object Dispatcher {