8000 Handle Differences In Ancestor Values When Joining Fiber Refs by adamgfraser · Pull Request #8334 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Handle Differences In Ancestor Values When Joining Fiber Refs #8334

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

Merged
merged 4 commits into from
Sep 15, 2023
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
11 changes: 11 additions & 0 deletions core-tests/shared/src/test/scala/zio/FiberRefSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,17 @@ object FiberRefSpec extends ZIOBaseSpec {
}
value <- promise.await
} yield assertTrue(value)
},
test("child fiber inherits subsequent changes made by parent when joining sibling") {
for {
fiberRef <- FiberRef.make(false)
promise <- Promise.make[Nothing, Fiber.Runtime[Any, Any]]
child1 <- (promise.await.flatMap(_.join) *> fiberRef.get).fork
_ <- fiberRef.set(true)
child2 <- ZIO.unit.fork
_ <- promise.succeed(child2)
value <- child1.join
} yield assertTrue(value)
}
) @@ TestAspect.fromLayer(Runtime.enableCurrentFiber) @@ TestAspect.sequential
}
Expand Down
70 changes: 35 additions & 35 deletions core/shared/src/main/scala/zio/FiberRefs.scala
8000
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import scala.annotation.tailrec
* example between an asynchronous producer and consumer.
*/
final class FiberRefs private (
private[zio] val fiberRefLocals: Map[FiberRef[_], (::[(FiberId.Runtime, Any)], Int)]
private[zio] val fiberRefLocals: Map[FiberRef[_], (::[(FiberId.Runtime, Any, Int)], Int)]
) { self =>

/**
Expand All @@ -47,11 +47,11 @@ final class FiberRefs private (
* individual fiber refs that make up the collection.
*/
def forkAs(childId: FiberId.Runtime): FiberRefs = {
val childFiberRefLocals: Map[FiberRef[_], (::[(FiberId.Runtime, Any)], Int)] =
val childFiberRefLocals: Map[FiberRef[_], (::[(FiberId.Runtime, Any, Int)], Int)] =
fiberRefLocals.transform { case (fiberRef, (stack, depth)) =>
val oldValue = stack.head._2.asInstanceOf[fiberRef.Value]
val newValue = fiberRef.patch(fiberRef.fork)(oldValue)
if (oldValue == newValue) (stack, depth) else (::(childId -> newValue, stack), depth + 1)
if (oldValue == newValue) (stack, depth) else (::((childId, newValue, 0), stack), depth + 1)
}

FiberRefs(childFiberRefLocals)
Expand Down Expand Up @@ -93,48 +93,47 @@ final class FiberRefs private (
.get(ref)
.fold {
if (childValue == ref.initial) parentFiberRefs
else parentFiberRefs.updated(ref, (::((fiberId, ref.join(ref.initial, childValue)), Nil), 1))
else parentFiberRefs.updated(ref, (::((fiberId, ref.join(ref.initial, childValue), 0), Nil), 1))
} { case (parentStack, parentDepth) =>
@tailrec
def findAncestor(
parentStack: List[(FiberId.Runtime, Any)],
parentStack: List[(FiberId.Runtime, Any, Int)],
parenthDepth: Int,
childStack: List[(FiberId.Runtime, Any)],
childDepth: Int,
childModified: Boolean = false
): (Any, Boolean) =
childStack: List[(FiberId.Runtime, Any, Int)],
childDepth: Int
): Any =
(parentStack, childStack) match {
case ((parentFiberId, _) :: parentAncestors, (childFiberId, childValue) :: childAncestors) =>
case (
(parentFiberId, parentValue, parentVersion) :: parentAncestors,
(childFiberId, childValue, childVersion) :: childAncestors
) =>
if (parentFiberId == childFiberId)
(childValue, childModified)
if (childVersion > parentVersion) parentValue else childValue
else if (childDepth > parentDepth)
findAncestor(parentStack, parentDepth, childAncestors, childDepth - 1, true)
findAncestor(parentStack, parentDepth, childAncestors, childDepth - 1)
else if (childDepth < parentDepth)
findAncestor(parentAncestors, parentDepth - 1, childStack, childDepth, childModified)
findAncestor(parentAncestors, parentDepth - 1, childStack, childDepth)
else
findAncestor(parentAncestors, parentDepth - 1, childAncestors, childDepth - 1, true)
findAncestor(parentAncestors, parentDepth - 1, childAncestors, childDepth - 1)
case _ =>
(ref.initial, true)
(ref.initial)
}

val (ancestor, wasModified) = findAncestor(parentStack, parentDepth, childStack, childDepth)
val ancestor = findAncestor(parentStack, parentDepth, childStack, childDepth)

if (!wasModified) parentFiberRefs
val patch = ref.diff(ancestor, childValue)

val oldValue = parentStack.head._2
val newValue = ref.join(oldValue, ref.patch(patch)(oldValue))

if (oldValue == newValue) parentFiberRefs
else {
val patch = ref.diff(ancestor, childValue)

val oldValue = parentStack.head._2
val newValue = ref.join(oldValue, ref.patch(patch)(oldValue))

if (oldValue == newValue) parentFiberRefs
else {
val (newStack, newDepth) = parentStack match {
case (parentFiberId, _) :: tail =>
if (parentFiberId == fiberId) (::((parentFiberId, newValue), tail), parentDepth)
else (::((fiberId, newValue), parentStack), parentDepth + 1)
}
parentFiberRefs.updated(ref, (newStack, newDepth))
val (newStack, newDepth) = parentStack match {
case (parentFiberId, _, parentVersion) :: tail =>
if (parentFiberId == fiberId) (::((parentFiberId, newValue, parentVersion + 1), tail), parentDepth)
else (::((fiberId, newValue, 0), parentStack), parentDepth + 1)
}
parentFiberRefs.updated(ref, (newStack, newDepth))
}
}
}
Expand All @@ -153,10 +152,11 @@ final class FiberRefs private (
def updatedAs[A](fiberId: FiberId.Runtime)(fiberRef: FiberRef[A], value: A): FiberRefs = {
val (oldStack, oldDepth) = fiberRefLocals.get(fiberRef).getOrElse((List.empty, 0))
val (newStack, newDepth) =
if (oldStack.isEmpty) (::((fiberId, value.asInstanceOf[Any]), Nil), 1)
else if (oldStack.head._1 == fiberId) (::((fiberId, value.asInstanceOf[Any]), oldStack.tail), oldDepth)
if (oldStack.isEmpty) (::((fiberId, value.asInstanceOf[Any], 0), Nil), 1)
else if (oldStack.head._1 == fiberId)
(::((fiberId, value.asInstanceOf[Any], oldStack.head._3 + 1), oldStack.tail), oldDepth)
else if (oldStack.head._2 == value) (::(oldStack.head, oldStack.tail), oldDepth)
else (::((fiberId, value), oldStack), oldDepth + 1)
else (::((fiberId, value, 0), oldStack), oldDepth + 1)

FiberRefs(fiberRefLocals.updated(fiberRef, (newStack, newDepth)))
}
Expand All @@ -170,7 +170,7 @@ object FiberRefs {
val empty: FiberRefs =
FiberRefs(Map.empty)

private[zio] def apply(fiberRefLocals: Map[FiberRef[_], (::[(FiberId.Runtime, Any)], Int)]): FiberRefs =
private[zio] def apply(fiberRefLocals: Map[FiberRef[_], (::[(FiberId.Runtime, Any, Int)], Int)]): FiberRefs =
new FiberRefs(fiberRefLocals)

/**
Expand Down Expand Up @@ -234,7 +234,7 @@ object FiberRefs {
*/
def diff(oldValue: FiberRefs, newValue: FiberRefs): Patch = {
val (removed, patch) = newValue.fiberRefLocals.foldLeft[(FiberRefs, Patch)](oldValue -> empty) {
case ((fiberRefs, patch), (fiberRef, ((_, newValue) :: _, _))) =>
case ((fiberRefs, patch), (fiberRef, ((_, newValue, _) :: _, _))) =>
fiberRefs.get(fiberRef) match {
case Some(oldValue) =>
if (oldValue == newValue)
Expand Down
0