Introduces ExecutionContext.opportunistic as a private[scala] instance as an escape-hatch for getting the original 2.13.0 batching global EC

This commit is contained in:
Viktor Klang 2020-10-29 22:11:27 +01:00 committed by Lukas Rytz
parent 7e9fd558d2
commit 94390f0da4
2 changed files with 23 additions and 1 deletions

View File

@ -498,7 +498,10 @@ val mimaFilterSettings = Seq {
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.immutable.RedBlackTree#Tree.this"),
ProblemFilters.exclude[ReversedAbstractMethodProblem]("scala.collection.immutable.RedBlackTree#Tree.black"),
ProblemFilters.exclude[ReversedAbstractMethodProblem]("scala.collection.immutable.RedBlackTree#Tree.red"),
ProblemFilters.exclude[MissingClassProblem]("scala.collection.immutable.TreeMap$TreeMapBuilder")
ProblemFilters.exclude[MissingClassProblem]("scala.collection.immutable.TreeMap$TreeMapBuilder"),
// Escape hatch for getting Scala 2.13.0 behavior of the global ExecutionContext
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.concurrent.ExecutionContext.opportunistic"),
),
}

View File

@ -166,6 +166,25 @@ object ExecutionContext {
override final def reportFailure(t: Throwable): Unit = defaultReporter(t)
}
/**
* This `ExecutionContext` is ideally suited to execute short-lived tasks on the `global` `ExecutionContext` as it
* attempts to locally batch the execution of nested tasks.
*
* WARNING: long-running and/or blocking tasks should be demarcated within `scala.concurrent.blocking`-blocks,
* to ensure that any pending tasks in the current batch can be executed by another thread on `global`.
*/
private[scala] lazy val opportunistic: ExecutionContextExecutor = new ExecutionContextExecutor with BatchingExecutor {
final override def submitForExecution(runnable: Runnable): Unit = global.execute(runnable)
final override def execute(runnable: Runnable): Unit =
if ((!runnable.isInstanceOf[impl.Promise.Transformation[_,_]] || runnable.asInstanceOf[impl.Promise.Transformation[_,_]].benefitsFromBatching) && runnable.isInstanceOf[Batchable])
submitAsyncBatched(runnable)
else
submitForExecution(runnable)
override final def reportFailure(t: Throwable): Unit = global.reportFailure(t)
}
object Implicits {
/**
* The implicit global `ExecutionContext`. Import `global` when you want to provide the global