Skip to content

Commit

Permalink
=lib Make use of CompletionStage#handle instead of CompletionStage#wh…
Browse files Browse the repository at this point in the history
…enComplete for better performance.
  • Loading branch information
He-Pin committed Nov 8, 2022
1 parent 4886921 commit a4a2031
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
7 changes: 7 additions & 0 deletions project/MimaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ object MimaFilters extends AutoPlugin {
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.Predef#ArrayCharSequence.isEmpty"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.ArrayCharSequence.isEmpty"),

// KEEP: make use of CompletionStage#handle to get a better performance than CompletionStage#whenComplete.
ProblemFilters.exclude[MissingTypesProblem]("scala.concurrent.impl.FutureConvertersImpl$P"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.concurrent.impl.FutureConvertersImpl#P.andThen"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.concurrent.impl.FutureConvertersImpl#P.apply"),
ProblemFilters.exclude[IncompatibleMethTypeProblem]("scala.concurrent.impl.FutureConvertersImpl#P.andThen"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.concurrent.impl.FutureConvertersImpl#P.accept"),
ProblemFilters.exclude[IncompatibleMethTypeProblem]("scala.concurrent.impl.FutureConvertersImpl#P.andThen"),
)

override val buildSettings = Seq(
Expand Down
4 changes: 2 additions & 2 deletions src/library/scala/concurrent/impl/FutureConvertersImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ private[scala] object FutureConvertersImpl {
override def toString(): String = super[CompletableFuture].toString
}

final class P[T](val wrapped: CompletionStage[T]) extends DefaultPromise[T] with BiConsumer[T, Throwable] {
override def accept(v: T, e: Throwable): Unit = {
final class P[T](val wrapped: CompletionStage[T]) extends DefaultPromise[T] with BiFunction[T, Throwable, Unit] {
override def apply(v: T, e: Throwable): Unit = {
if (e == null) success(v)
else failure(e)
}
Expand Down
10 changes: 7 additions & 3 deletions src/library/scala/jdk/javaapi/FutureConverters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

package scala.jdk.javaapi

import java.util.concurrent.CompletionStage

import java.util.concurrent.{CompletableFuture, CompletionStage}
import scala.concurrent.impl.FutureConvertersImpl.{CF, P}
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Success

/** This object contains methods that convert between Scala [[scala.concurrent.Future]] and Java [[java.util.concurrent.CompletionStage]].
*
Expand Down Expand Up @@ -70,9 +70,13 @@ object FutureConverters {
case cf: CF[T] => cf.wrapped
// in theory not safe (could be `class C extends Future[A] with CompletionStage[B]`):
case f: Future[T @unchecked] => f
case cf: CompletableFuture[T @unchecked] if cf.isDone && !cf.isCompletedExceptionally =>
val p = new P[T](cs)
p.tryComplete(Success(cf.join()))
p.future
case _ =>
val p = new P[T](cs)
cs whenComplete p
cs.handle(p)
p.future
}
}
Expand Down

0 comments on commit a4a2031

Please sign in to comment.