Skip to content

Commit dcba7ab

Browse files
committed
Avoid unnecessary allocation in Uncancelable trait (idPoll object moved to MonadCancel object).
1 parent e81d9f0 commit dcba7ab

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

kernel/shared/src/main/scala/cats/effect/kernel/MonadCancel.scala

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package cats.effect.kernel
1818

19-
import cats.{MonadError, Monoid, Semigroup}
19+
import cats.{Id, MonadError, Monoid, Semigroup}
2020
import cats.data.{
2121
EitherT,
2222
IndexedReaderWriterStateT,
@@ -583,7 +583,7 @@ object MonadCancel {
583583
Sync.syncForStateT[F, S](sync)
584584
case cancel =>
585585
new StateTMonadCancel[F, S, E] {
586-
def rootCancelScope = F0.rootCancelScope
586+
def rootCancelScope: CancelScope = F0.rootCancelScope
587587
override implicit protected def F: MonadCancel[F, E] = cancel
588588
}
589589
}
@@ -602,11 +602,16 @@ object MonadCancel {
602602
}
603603
}
604604

605-
trait Uncancelable[F[_], E] { this: MonadCancel[F, E] =>
605+
private val cachedPoll: Poll[Id] = new Poll[Id] {
606+
def apply[A](fa: Id[A]): Id[A] = fa
607+
}
606608

607-
private[this] val IdPoll = new Poll[F] {
608-
def apply[A](fa: F[A]) = fa
609-
}
609+
trait Uncancelable[F[_], E] { this: MonadCancel[F, E] =>
610+
// There used to be a field in this class which wasn't really necessary.
611+
// After it was removed (as a memory optimization - see the uncancelable method),
612+
// the compiler would no longer generate the $init$ method. To get it back
613+
// and thus stay backwards compatible, we apply the following trick:
614+
locally { () } // Do not remove this line.
610615

611616
def rootCancelScope: CancelScope = CancelScope.Uncancelable
612617

@@ -618,7 +623,7 @@ object MonadCancel {
618623
}
619624

620625
def uncancelable[A](body: Poll[F] => F[A]): F[A] =
621-
body(IdPoll)
626+
body(MonadCancel.cachedPoll.asInstanceOf[Poll[F]])
622627
}
623628

624629
private[kernel] trait OptionTMonadCancel[F[_], E] extends MonadCancel[OptionT[F, *], E] {

0 commit comments

Comments
 (0)