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 liveTraces() on JS #3724

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,11 @@ private[effect] final class BatchingMacrotaskExecutor(
var i = 0
while (i < batchSize && !fibers.isEmpty()) {
val fiber = fibers.take()

if (LinkingInfo.developmentMode)
if (fiberBag ne null)
fiberBag -= fiber

try fiber.run()
catch {
case t if NonFatal(t) => reportFailure(t)
case t: Throwable => IOFiber.onFatalFailure(t)
}

i += 1
}

Expand All @@ -99,10 +93,6 @@ private[effect] final class BatchingMacrotaskExecutor(
* batch.
*/
def schedule(fiber: IOFiber[_]): Unit = {
if (LinkingInfo.developmentMode)
if (fiberBag ne null)
fiberBag += fiber

fibers.offer(fiber)

if (needsReschedule) {
Expand All @@ -116,8 +106,12 @@ private[effect] final class BatchingMacrotaskExecutor(

def reportFailure(t: Throwable): Unit = reportFailure0(t)

def liveTraces(): Map[IOFiber[_], Trace] =
fiberBag.iterator.filterNot(_.isDone).map(f => f -> f.captureTrace()).toMap
def liveTraces(): Map[IOFiber[_], Trace] = {
val traces = Map.newBuilder[IOFiber[_], Trace]
fibers.foreach(f => if (!f.isDone) traces += f -> f.captureTrace())
fiberBag.foreach(f => if (!f.isDone) traces += f -> f.captureTrace())
traces.result()
}

@inline private[this] def monitor(runnable: Runnable): Runnable =
if (LinkingInfo.developmentMode)
Expand Down
21 changes: 21 additions & 0 deletions core/js/src/main/scala/cats/effect/unsafe/JSArrayQueue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,25 @@ private final class JSArrayQueue[A] {
}
}

@inline def foreach(f: A => Unit): Unit =
if (empty) ()
else if (startIndex < endIndex) { // consecutive in middle of buffer
var i = startIndex
while (i < endIndex) {
f(buffer(i))
i += 1
}
} else { // split across tail and init of buffer
var i = startIndex
while (i < buffer.length) {
f(buffer(i))
i += 1
}
i = 0
while (i < endIndex) {
f(buffer(i))
i += 1
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package unsafe
import org.scalacheck.Prop.forAll
import org.specs2.ScalaCheck

import scala.collection.mutable.ListBuffer
import scala.collection.mutable.{ListBuffer, Queue}

class JSArrayQueueSpec extends BaseSpec with ScalaCheck {

Expand All @@ -41,6 +41,37 @@ class JSArrayQueueSpec extends BaseSpec with ScalaCheck {
taken.toList must beEqualTo(stuff.flatten)
}
}

"iterate over contents in foreach" in {
forAll { (stuff: List[Option[Int]]) =>
val queue = new JSArrayQueue[Int]
val shadow = new Queue[Int]
Copy link
Contributor

Choose a reason for hiding this comment

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

It's good to see I'm not the only one who tends to call these things "shadows" :-) (Although I don't remember where I've got the idea from.)


def checkContents() = {
val builder = List.newBuilder[Int]
queue.foreach(builder += _)
builder.result() must beEqualTo(shadow.toList)
}

checkContents()

stuff.foreach {
case Some(i) =>
queue.offer(i)
shadow.enqueue(i)
checkContents()
case None =>
if (!shadow.isEmpty) {
val got = queue.take()
val expected = shadow.dequeue()
got must beEqualTo(expected)
checkContents()
}
}

true must beTrue
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this the same as ok?

Copy link
Member Author

Choose a reason for hiding this comment

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

oh lol good call

Suggested change
true must beTrue
ok

}
}
}

}