Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
19 changes: 14 additions & 5 deletions core/jvm-native/src/main/scala/zio/internal/ZScheduler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import scala.collection.mutable
* applications. Inspired by "Making the Tokio Scheduler 10X Faster" by Carl
* Lerche. [[https://tokio.rs/blog/2019-10-scheduler]]
*/
private final class ZScheduler(autoBlocking: Boolean) extends Executor {
private final class ZScheduler(autoBlocking: Boolean) extends Executor { parent =>

import Trace.{empty => emptyTrace}
import ZScheduler.{poolSize, workerOrNull}
Expand Down Expand Up @@ -266,14 +266,23 @@ private final class ZScheduler(autoBlocking: Boolean) extends Executor {
private[this] def makeWorker(): ZScheduler.Worker =
new ZScheduler.Worker {
self =>
override val submittedLocations = makeLocations()
override val submittedLocations: ZScheduler.Locations = makeLocations()

final override def run(): Unit = {
// Store parent mutable object references in stack memory to avoid fetching it from the heap every time
val globalQueue = parent.globalQueue
val workers = parent.workers
val state = parent.state
val cache = parent.cache
val idle = parent.idle
val poolSize = ZScheduler.poolSize

override def run(): Unit = {
var currentBlocking = false
var currentOpCount = 0L
val random = ThreadLocalRandom.current
var runnable = null.asInstanceOf[Runnable]
var searching = false

while (!isInterrupted) {
currentBlocking = blocking
val currentNextRunnable = nextRunnable
Expand Down Expand Up @@ -321,7 +330,7 @@ private final class ZScheduler(autoBlocking: Boolean) extends Executor {
currentBlocking = blocking
if (currentBlocking) {
val runnables = localQueue.pollUpTo(256)
if (runnables.nonEmpty) {
if (!runnables.isEmpty) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this because of the interface dispatch? I think we could override it even though it's marked deprecatedOverriding

Copy link
Contributor Author

@kyri-petrou kyri-petrou May 20, 2025

Choose a reason for hiding this comment

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

Is this because of the interface dispatch

Yeap

I think we could override it

I'm a bit worried that it'll make things a bit worse for the rest of the std library collections. My knowledge on the matter is not the best but I think that the more overrides a method has, it makes interface dispatches for that method slower. I could be wrong on that though, I vaguely remember reading that somewhere.

globalQueue.offerAll(runnables, random)
}
}
Expand Down Expand Up @@ -388,7 +397,7 @@ private final class ZScheduler(autoBlocking: Boolean) extends Executor {

// NOTE: Synchronized block in case the supervisor attempts to mark the worker as blocking at the same time
// as an external call
def markAsBlocking(): Unit = synchronized {
final def markAsBlocking(): Unit = synchronized {
if (blocking) ()
else {
blocking = true
Expand Down