Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize NonFatal(...) checks #3223

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions core/jvm/src/main/scala/cats/effect/IOApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ trait IOApp {
new ExecutionContext {
def reportFailure(t: Throwable): Unit =
t match {
case NonFatal(t) =>
case t if NonFatal(t) =>
t.printStackTrace()

case t =>
Expand Down Expand Up @@ -457,23 +457,24 @@ trait IOApp {
// if we're unforked, the only way to report cancelation is to throw
throw e

case NonFatal(t) =>
if (isForked) {
t.printStackTrace()
System.exit(1)
case t: Throwable =>
if (NonFatal(t)) {
if (isForked) {
t.printStackTrace()
System.exit(1)
} else {
throw t
}
} else {
throw t
t.printStackTrace()
rt.halt(1)
}

case t: Throwable =>
t.printStackTrace()
rt.halt(1)

case r: Runnable =>
try {
r.run()
} catch {
case NonFatal(t) =>
case t if NonFatal(t) =>
if (isForked) {
t.printStackTrace()
System.exit(1)
Expand Down
2 changes: 1 addition & 1 deletion core/jvm/src/main/scala/cats/effect/IOFiberPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private[effect] abstract class IOFiberPlatform[A] extends AtomicBoolean(false) {
case ex: ClosedByInterruptException => throw ex

// this won't suppress InterruptedException:
case NonFatal(t) => Left(t)
case t if NonFatal(t) => Left(t)
}

// this is why it has to be a semaphore rather than an atomic boolean
Expand Down
16 changes: 8 additions & 8 deletions core/jvm/src/main/scala/cats/effect/unsafe/WorkerThread.scala
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ private final class WorkerThread(

try fiber.run()
catch {
case NonFatal(t) => pool.reportFailure(t)
case t if NonFatal(t) => pool.reportFailure(t)
case t: Throwable => IOFiber.onFatalFailure(t)
}
} else if (element.isInstanceOf[Runnable]) {
Expand All @@ -402,7 +402,7 @@ private final class WorkerThread(
// The dequeued element is a single fiber. Execute it immediately.
try fiber.run()
catch {
case NonFatal(t) => pool.reportFailure(t)
case t if NonFatal(t) => pool.reportFailure(t)
case t: Throwable => IOFiber.onFatalFailure(t)
}
}
Expand All @@ -427,7 +427,7 @@ private final class WorkerThread(
pool.notifyParked(rnd)
try fiber.run()
catch {
case NonFatal(t) => pool.reportFailure(t)
case t if NonFatal(t) => pool.reportFailure(t)
case t: Throwable => IOFiber.onFatalFailure(t)
}

Expand All @@ -444,7 +444,7 @@ private final class WorkerThread(
// The dequeued element is a single fiber. Execute it immediately.
try fiber.run()
catch {
case NonFatal(t) => pool.reportFailure(t)
case t if NonFatal(t) => pool.reportFailure(t)
case t: Throwable => IOFiber.onFatalFailure(t)
}

Expand Down Expand Up @@ -484,7 +484,7 @@ private final class WorkerThread(
// Run the stolen fiber.
try fiber.run()
catch {
case NonFatal(t) => pool.reportFailure(t)
case t if NonFatal(t) => pool.reportFailure(t)
case t: Throwable => IOFiber.onFatalFailure(t)
}
// Transition to executing fibers from the local queue.
Expand Down Expand Up @@ -534,7 +534,7 @@ private final class WorkerThread(
pool.notifyParked(rnd)
try fiber.run()
catch {
case NonFatal(t) => pool.reportFailure(t)
case t if NonFatal(t) => pool.reportFailure(t)
case t: Throwable => IOFiber.onFatalFailure(t)
}

Expand All @@ -554,7 +554,7 @@ private final class WorkerThread(
// The dequeued element is a single fiber. Execute it immediately.
try fiber.run()
catch {
case NonFatal(t) => pool.reportFailure(t)
case t if NonFatal(t) => pool.reportFailure(t)
case t: Throwable => IOFiber.onFatalFailure(t)
}

Expand Down Expand Up @@ -584,7 +584,7 @@ private final class WorkerThread(
// Run the fiber.
try fiber.run()
catch {
case NonFatal(t) => pool.reportFailure(t)
case t if NonFatal(t) => pool.reportFailure(t)
case t: Throwable => IOFiber.onFatalFailure(t)
}
// Continue executing fibers from the local queue.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ abstract class PollingExecutorScheduler(pollEvery: Int)
val task = sleepQueue.poll()
try task.runnable.run()
catch {
case NonFatal(t) => reportFailure(t)
case t if NonFatal(t) => reportFailure(t)
case t: Throwable => IOFiber.onFatalFailure(t)
}
}
Expand All @@ -113,7 +113,7 @@ abstract class PollingExecutorScheduler(pollEvery: Int)
val runnable = executeQueue.poll()
try runnable.run()
catch {
case NonFatal(t) => reportFailure(t)
case t if NonFatal(t) => reportFailure(t)
case t: Throwable => IOFiber.onFatalFailure(t)
}
i += 1
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/cats/effect/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ object IO extends IOCompanionPlatform with IOLowPriorityImplicits {
* IO async_ { cb =>
* exc.execute(new Runnable {
* def run() =
* try cb(Right(body)) catch { case NonFatal(t) => cb(Left(t)) }
* try cb(Right(body)) catch { case t if NonFatal(t) => cb(Left(t)) }
* })
* }
* }}}
Expand Down
24 changes: 12 additions & 12 deletions core/shared/src/main/scala/cats/effect/IOFiber.scala
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private final class IOFiber[A](
val r =
try cur.thunk()
catch {
case NonFatal(t) =>
case t if NonFatal(t) =>
error = t
case t: Throwable =>
onFatalFailure(t)
Expand Down Expand Up @@ -304,7 +304,7 @@ private final class IOFiber[A](
val result =
try f(v)
catch {
case NonFatal(t) =>
case t if NonFatal(t) =>
error = t
case t: Throwable =>
onFatalFailure(t)
Expand Down Expand Up @@ -334,7 +334,7 @@ private final class IOFiber[A](
val result =
try f(delay.thunk())
catch {
case NonFatal(t) =>
case t if NonFatal(t) =>
error = t
case t: Throwable =>
onFatalFailure(t)
Expand Down Expand Up @@ -374,7 +374,7 @@ private final class IOFiber[A](
def next(v: Any): IO[Any] =
try f(v)
catch {
case NonFatal(t) =>
case t if NonFatal(t) =>
failed(t, 0)
case t: Throwable =>
onFatalFailure(t)
Expand All @@ -400,7 +400,7 @@ private final class IOFiber[A](
val result =
try f(delay.thunk())
catch {
case NonFatal(t) =>
case t if NonFatal(t) =>
failed(t, 0)
case t: Throwable =>
onFatalFailure(t)
Expand Down Expand Up @@ -456,7 +456,7 @@ private final class IOFiber[A](
val result =
try delay.thunk()
catch {
case NonFatal(t) =>
case t if NonFatal(t) =>
// We need to augment the exception here because it doesn't
// get forwarded to the `failed` path.
Tracing.augmentThrowable(runtime.enhancedExceptions, t, tracingEvents)
Expand Down Expand Up @@ -950,7 +950,7 @@ private final class IOFiber[A](
try {
scala.concurrent.blocking(cur.thunk())
} catch {
case NonFatal(t) =>
case t if NonFatal(t) =>
error = t
case t: Throwable =>
onFatalFailure(t)
Expand Down Expand Up @@ -1149,7 +1149,7 @@ private final class IOFiber[A](
val transformed =
try f(result)
catch {
case NonFatal(t) =>
case t if NonFatal(t) =>
error = t
case t: Throwable =>
onFatalFailure(t)
Expand All @@ -1168,7 +1168,7 @@ private final class IOFiber[A](

try f(result)
catch {
case NonFatal(t) =>
case t if NonFatal(t) =>
failed(t, depth + 1)
case t: Throwable =>
onFatalFailure(t)
Expand Down Expand Up @@ -1240,7 +1240,7 @@ private final class IOFiber[A](

try f(error)
catch {
case NonFatal(t) =>
case t if NonFatal(t) =>
failed(t, depth + 1)
case t: Throwable =>
onFatalFailure(t)
Expand Down Expand Up @@ -1345,7 +1345,7 @@ private final class IOFiber[A](
val r =
try cur.thunk()
catch {
case NonFatal(t) =>
case t if NonFatal(t) =>
error = t
case t: Throwable =>
onFatalFailure(t)
Expand Down Expand Up @@ -1383,7 +1383,7 @@ private final class IOFiber[A](

try runnable.run()
catch {
case NonFatal(t) =>
case t if NonFatal(t) =>
currentCtx.reportFailure(t)
case t: Throwable =>
onFatalFailure(t)
Expand Down
8 changes: 4 additions & 4 deletions core/shared/src/main/scala/cats/effect/SyncIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ sealed abstract class SyncIO[+A] private () extends Serializable {
val r =
try cur.thunk()
catch {
case NonFatal(t) => error = t
case t if NonFatal(t) => error = t
}

val next =
Expand Down Expand Up @@ -348,7 +348,7 @@ sealed abstract class SyncIO[+A] private () extends Serializable {
val transformed =
try f(result)
catch {
case NonFatal(t) => error = t
case t if NonFatal(t) => error = t
}

if (depth > MaxStackDepth) {
Expand All @@ -365,7 +365,7 @@ sealed abstract class SyncIO[+A] private () extends Serializable {

try f(result)
catch {
case NonFatal(t) => failed(t, depth + 1)
case t if NonFatal(t) => failed(t, depth + 1)
}
}

Expand All @@ -374,7 +374,7 @@ sealed abstract class SyncIO[+A] private () extends Serializable {

try f(t)
catch {
case NonFatal(t) => failed(t, depth + 1)
case t if NonFatal(t) => failed(t, depth + 1)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ final class TestContext private (_seed: Long) extends ExecutionContext { self =>
if (current.tasks.remove(head)) {
// execute task
try head.task.run()
catch { case NonFatal(ex) => reportFailure(ex) }
catch { case ex if NonFatal(ex) => reportFailure(ex) }
true
} else {
tickOne()
Expand Down