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

Cache now in WorkerThread in states 4-63 #3690

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
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,16 @@ private[effect] final class WorkStealingThreadPool(
*/
override def reportFailure(cause: Throwable): Unit = reportFailure0(cause)

override def monotonicNanos(): Long = System.nanoTime()
override def monotonicNanos(): Long = {
val back = System.nanoTime()

val thread = Thread.currentThread()
if (thread.isInstanceOf[WorkerThread]) {
thread.asInstanceOf[WorkerThread].now = back
}

back
}

override def nowMillis(): Long = System.currentTimeMillis()

Expand Down
32 changes: 26 additions & 6 deletions core/jvm/src/main/scala/cats/effect/unsafe/WorkerThread.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ private final class WorkerThread(
*/
private[this] var blocking: Boolean = false

/**
* Contains the current monotonic time. Updated by the main worker loop as well as the
* `Scheduler` functions when accessing system time in userspace.
*/
private[unsafe] var now: Long = System.nanoTime()

/**
* Holds a reference to the fiber currently being executed by this worker thread. This field
* is sometimes published by the `head` and `tail` of the [[LocalQueue]] and sometimes by the
Expand Down Expand Up @@ -147,9 +153,13 @@ private final class WorkerThread(
}

def sleep(delay: FiniteDuration, callback: Right[Nothing, Unit] => Unit): Runnable = {
// take the opportunity to update the current time, just in case other timers can benefit
val _now = System.nanoTime()
djspiewak marked this conversation as resolved.
Show resolved Hide resolved
now = _now

// note that blockers aren't owned by the pool, meaning we only end up here if !blocking
sleepers.insert(
now = System.nanoTime(),
now = _now,
delay = delay.toNanos,
callback = callback,
tlr = random
Expand Down Expand Up @@ -335,12 +345,14 @@ private final class WorkerThread(
}
}

now = System.nanoTime()

if (nextState != 4) {
// after being unparked, we re-check sleepers;
// if we find an already expired one, we go
// immediately to state 4 (local queue stuff):
val nextTrigger = sleepers.peekFirstTriggerTime()
if ((nextTrigger != MIN_VALUE) && (nextTrigger - System.nanoTime() <= 0L)) {
if ((nextTrigger != MIN_VALUE) && (nextTrigger - now <= 0L)) {
pool.transitionWorkerFromSearching(rnd)
4
} else {
Expand Down Expand Up @@ -378,7 +390,7 @@ private final class WorkerThread(
parkLoop()
false
djspiewak marked this conversation as resolved.
Show resolved Hide resolved
} else {
val now = System.nanoTime()
now = System.nanoTime()
val nanos = triggerTime - now

if (nanos > 0L) {
Expand All @@ -388,9 +400,12 @@ private final class WorkerThread(
pool.shutdown()
false // we know `done` is `true`
} else {
// we already parked and time passed, so update time again
// it doesn't matter if we timed out or were awakened, the update is free-ish
now = System.nanoTime()
if (parked.get()) {
// we were either awakened spuriously, or we timed out
if (triggerTime - System.nanoTime() <= 0) {
if (triggerTime - now <= 0) {
// we timed out
if (parked.getAndSet(false)) {
pool.doneSleeping()
Expand Down Expand Up @@ -530,6 +545,9 @@ private final class WorkerThread(
}
}

// update the current time
now = System.nanoTime()

// Transition to executing fibers from the local queue.
state = 4

Expand Down Expand Up @@ -595,8 +613,11 @@ private final class WorkerThread(
}

case 2 =>
// update the current time
now = System.nanoTime()

// First try to steal some expired timers:
if (pool.stealTimers(System.nanoTime(), rnd)) {
if (pool.stealTimers(now, rnd)) {
// some stolen timer created new work for us
pool.transitionWorkerFromSearching(rnd)
state = 4
Expand Down Expand Up @@ -693,7 +714,6 @@ private final class WorkerThread(

case _ =>
// Call all of our expired timers:
val now = System.nanoTime()
var cont = true
while (cont) {
val cb = sleepers.pollFirstIfTriggered(now)
Comment on lines 715 to 719
Copy link
Member

Choose a reason for hiding this comment

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

The optimization discussed in #3544 (comment) would become even more meaningful after this change, if now may remain constant for several iterations. So crossing a read-barrier on every iteration is pointless 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I thought about that. It's not horribly complicated to do something like that here, it just introduces a bit more state and a bit more branching. I wanted to start small.

Expand Down