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 1 commit
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 @@ -52,6 +52,15 @@ private[unsafe] abstract class SchedulerCompanionPlatform { this: Scheduler.type
now.getEpochSecond * 1000000 + now.getLong(ChronoField.MICRO_OF_SECOND)
}

def monotonicNanos() = System.nanoTime()
def monotonicNanos() = {
val back = System.nanoTime()

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

back
}
djspiewak marked this conversation as resolved.
Show resolved Hide resolved
}
}
21 changes: 16 additions & 5 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 @@ -340,7 +346,8 @@ private final class WorkerThread(
// 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)) {
now = System.nanoTime()
djspiewak marked this conversation as resolved.
Show resolved Hide resolved
if ((nextTrigger != MIN_VALUE) && (nextTrigger - now <= 0L)) {
pool.transitionWorkerFromSearching(rnd)
4
} else {
Expand Down Expand Up @@ -378,7 +385,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 @@ -390,7 +397,8 @@ private final class WorkerThread(
} else {
if (parked.get()) {
// we were either awakened spuriously, or we timed out
if (triggerTime - System.nanoTime() <= 0) {
now = System.nanoTime()
if (triggerTime - now <= 0) {
// we timed out
if (parked.getAndSet(false)) {
pool.doneSleeping()
Expand Down Expand Up @@ -530,6 +538,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 @@ -596,7 +607,8 @@ private final class WorkerThread(

case 2 =>
// First try to steal some expired timers:
if (pool.stealTimers(System.nanoTime(), rnd)) {
now = System.nanoTime()
if (pool.stealTimers(now, rnd)) {
// some stolen timer created new work for us
pool.transitionWorkerFromSearching(rnd)
state = 4
Expand Down Expand Up @@ -693,7 +705,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