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

Fiber locals #1393

Merged
merged 24 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions core/shared/src/main/scala/cats/effect/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ sealed abstract class IO[+A] private () extends IOPlatform[A] {

val fiber = new IOFiber[A](
0,
Map(),
oc =>
oc.fold(
(),
Expand Down Expand Up @@ -665,6 +666,11 @@ object IO extends IOCompanionPlatform with IOLowPriorityImplicits {
def tag = 20
}

// Not part of the run loop. Only used in the implementation of IO#to.
private[effect] final case class UnmaskTo[F[_], +A](ioa: IO[A], poll: Poll[F]) extends IO[A] {
def tag = -1
}

private[effect] final case class Blocking[+A](hint: Sync.Type, thunk: () => A) extends IO[A] {
def tag = 21
}
Expand All @@ -677,6 +683,14 @@ object IO extends IOCompanionPlatform with IOLowPriorityImplicits {
def tag = 23
}

private[effect] final case class GetLocal[A](index: Int) extends IO[Option[A]] {
RaasAhsan marked this conversation as resolved.
Show resolved Hide resolved
def tag = 24
}

private[effect] final case class SetLocal[A](index: Int, value: A) extends IO[Unit] {
def tag = 25
}

// INTERNAL, only created by the runloop itself as the terminal state of several operations
private[effect] case object EndFiber extends IO[Nothing] {
def tag = -1
Expand Down
29 changes: 28 additions & 1 deletion core/shared/src/main/scala/cats/effect/IOFiber.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import scala.concurrent.duration._
import scala.util.control.NonFatal

import java.util.concurrent.RejectedExecutionException
import java.util.concurrent.atomic.{AtomicBoolean, AtomicReference}
import java.util.concurrent.atomic.{AtomicBoolean, AtomicInteger, AtomicReference}
import scala.util.control.NoStackTrace

/*
Expand Down Expand Up @@ -61,6 +61,7 @@ import scala.util.control.NoStackTrace
*/
private final class IOFiber[A](
initMask: Int,
initLocalState: scala.collection.immutable.Map[Int, Any],
cb: OutcomeIO[A] => Unit,
startIO: IO[A],
startEC: ExecutionContext,
Expand Down Expand Up @@ -101,6 +102,8 @@ private final class IOFiber[A](
/* true when semantically blocking (ensures that we only unblock *once*) */
private[this] val suspended: AtomicBoolean = new AtomicBoolean(true)

private[this] var localState: scala.collection.immutable.Map[Int, Any] = initLocalState
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two options here for holding local state:

  1. Immutable map/array: Spawning fibers just passes along the current local state reference (constant-time). Manipulating local state means inserting into the map and reassigning the variable (constant-time, worst case linear time IIRC?). This is the current approach
  2. Mutable hashmap/array: Spawning fibers has to perform a shallow copy of the local state (linear time). Manipulating local state is constant-time.


@volatile
private[this] var outcome: OutcomeIO[A] = _

Expand Down Expand Up @@ -595,6 +598,7 @@ private final class IOFiber[A](
val ec = currentCtx
val fiber = new IOFiber[Any](
initMask2,
localState,
null,
cur.ioa,
ec,
Expand All @@ -621,13 +625,15 @@ private final class IOFiber[A](
val ec = currentCtx
val fiberA = new IOFiber[Any](
initMask2,
localState,
null,
cur.ioa,
ec,
runtime
)
val fiberB = new IOFiber[Any](
initMask2,
localState,
null,
cur.iob,
ec,
Expand Down Expand Up @@ -712,13 +718,15 @@ private final class IOFiber[A](
val ec = currentCtx
val fiberA = new IOFiber[Any](
initMask2,
localState,
null,
cur.ioa,
ec,
runtime
)
val fiberB = new IOFiber[Any](
initMask2,
localState,
null,
cur.iob,
ec,
Expand Down Expand Up @@ -798,13 +806,15 @@ private final class IOFiber[A](
val ec = currentCtx
val fiberA = new IOFiber[Any](
initMask2,
localState,
null,
cur.ioa,
ec,
runtime
)
val fiberB = new IOFiber[Any](
initMask2,
localState,
null,
cur.iob,
ec,
Expand Down Expand Up @@ -873,6 +883,18 @@ private final class IOFiber[A](
}.guarantee(IO.defer(finalizer.get()))

runLoop(next, nextIteration)

case 24 =>
val cur = cur0.asInstanceOf[GetLocal[Any]]

val value = localState.get(cur.index)
runLoop(succeeded(value, 0), nextIteration)

case 25 =>
val cur = cur0.asInstanceOf[SetLocal[Any]]

localState = localState + (cur.index -> cur.value)
runLoop(succeeded((), 0), nextIteration)
}
}
}
Expand Down Expand Up @@ -1320,9 +1342,14 @@ private final class IOFiber[A](
}

private object IOFiber {
private val localIndex = new AtomicInteger(0)

/* prefetch */
private val OutcomeCanceled = Outcome.Canceled()
private[effect] val RightUnit = Right(())

private[effect] def nextLocalIndex(): Int =
localIndex.getAndIncrement()
}

private[effect] case object AsyncPropagateCancelation extends NoStackTrace
51 changes: 51 additions & 0 deletions core/shared/src/main/scala/cats/effect/local/Local.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2020 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats.effect.local
RaasAhsan marked this conversation as resolved.
Show resolved Hide resolved

import cats.effect.{IO, IOFiber}

final class Local[A] private (index: Int, default: A) {
RaasAhsan marked this conversation as resolved.
Show resolved Hide resolved
RaasAhsan marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extend Ref[IO, A] here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a review comment about it up here #1393 (comment) . I need to recall my thoughts on this but I was more inclined not to


def get: IO[A] =
IO.GetLocal[A](index).map(_.getOrElse(default))

def set(value: A): IO[Unit] =
IO.SetLocal[A](index, value)

def update(f: A => A): IO[Unit] =
get.flatMap(a => set(f(a)))

def getAndSet(value: A): IO[A] =
get <* set(value)

def clear: IO[Unit] =
IO.SetLocal[A](index, default)

}

object Local {

def apply[A](default: A): IO[Local[A]] =
of(default)

def of[A](default: A): IO[Local[A]] =
IO {
val index = IOFiber.nextLocalIndex()
new Local(index, default)
}

}
64 changes: 64 additions & 0 deletions core/shared/src/main/scala/cats/effect/local/LocalRef.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2020 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats.effect.local

import cats.effect.IO
import cats.effect.kernel.Ref

final class LocalRef[A] private (local: Local[Ref[IO, A]]) extends Ref[IO, A] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not crazy about the name, but I can't think of a better one. This is basically just a newtype around Local[Ref[IO, A]] right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep


override def get: IO[A] =
local.get.flatMap(_.get)

override def set(a: A): IO[Unit] =
local.get.flatMap(_.set(a))

override def access: IO[(A, A => IO[Boolean])] =
local.get.flatMap(_.access)

override def tryUpdate(f: A => A): IO[Boolean] =
local.get.flatMap(_.tryUpdate(f))

override def tryModify[B](f: A => (A, B)): IO[Option[B]] =
local.get.flatMap(_.tryModify(f))

override def update(f: A => A): IO[Unit] =
local.get.flatMap(_.update(f))

override def modify[B](f: A => (A, B)): IO[B] =
local.get.flatMap(_.modify(f))

override def tryModifyState[B](state: cats.data.State[A, B]): IO[Option[B]] =
local.get.flatMap(_.tryModifyState(state))

override def modifyState[B](state: cats.data.State[A, B]): IO[B] =
local.get.flatMap(_.modifyState(state))

}

object LocalRef {

def apply[A](default: A): IO[LocalRef[A]] =
of(default)

def of[A](default: A): IO[LocalRef[A]] =
for {
ref <- Ref.of[IO, A](default)
local <- Local.of[Ref[IO, A]](ref)
} yield new LocalRef(local)

}
87 changes: 87 additions & 0 deletions core/shared/src/test/scala/cats/effect/local/LocalRefSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2020 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats
package effect
package local

class LocalRefSpec extends BaseSpec {

"LocalRef" should {
"return a default value" in ticked { implicit ticker =>
val io = LocalRef(0).flatMap(_.get)

io must completeAs(0)
}

"set and get a value" in ticked { implicit ticker =>
val io = for {
local <- LocalRef(0)
_ <- local.set(10)
value <- local.get
} yield value

io must completeAs(10)
}

"preserve locals across async boundaries" in ticked { implicit ticker =>
val io = for {
local <- LocalRef(0)
_ <- local.set(10)
_ <- IO.cede
value <- local.get
} yield value

io must completeAs(10)
}

"children fibers can read locals" in ticked { implicit ticker =>
val io = for {
local <- LocalRef(0)
_ <- local.set(10)
f <- local.get.start
value <- f.joinAndEmbedNever
} yield value

io must completeAs(10)
}

"child local manipulation is visible to parents" in ticked { implicit ticker =>
val io = for {
local <- LocalRef(0)
f <- local.set(20).start
_ <- f.join
value <- local.get
} yield value

io must completeAs(20)
}

"parent local manipulation is visible to children" in ticked { implicit ticker =>
val io = for {
local <- LocalRef(0)
d1 <- Deferred[IO, Unit]
f <- (d1.get *> local.get).start
_ <- local.set(10)
_ <- d1.complete(())
value <- f.joinAndEmbedNever
} yield value

io must completeAs(10)
}
}

}