Skip to content

Commit b846ae5

Browse files
viktorklangdwijnand
authored andcommitted
Optimizing the implementation of DefaultPromise.submitWithValue (scala#7445)
Optimizes the submitWithValue code and aligns it with the style of the rest of the implementation.
1 parent 1f76b24 commit b846ae5

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

src/library/scala/concurrent/impl/Promise.scala

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -285,30 +285,23 @@ private[concurrent] final object Promise {
285285
}
286286

287287
// IMPORTANT: Noop should never be passed in here, neither as left OR as right
288-
@tailrec private[this] final def concatCallbacks(left: Callbacks[T], right: Callbacks[T]): Callbacks[T] = {
289-
if (left.isInstanceOf[Transformation[T,_]]) new ManyCallbacks[T](left, right)
288+
@tailrec private[this] final def concatCallbacks(left: Callbacks[T], right: Callbacks[T]): Callbacks[T] =
289+
if (left.isInstanceOf[Transformation[T,_]]) new ManyCallbacks[T](left.asInstanceOf[Transformation[T,_]], right)
290290
else /*if (left.isInstanceOf[ManyCallbacks[T]) */ { // This should only happen when linking
291291
val m = left.asInstanceOf[ManyCallbacks[T]]
292-
concatCallbacks(m.last, new ManyCallbacks(m.first, right))
292+
concatCallbacks(m.rest, new ManyCallbacks(m.first, right))
293293
}
294-
}
295294

296-
// IMPORTANT: Noop should not be passed in here
295+
// IMPORTANT: Noop should not be passed in here, `callbacks` cannot be null
297296
@tailrec
298-
private[this] final def submitWithValue(callbacks: Callbacks[T],
299-
resolved: Try[T],
300-
deferred: List[Callbacks[T]] = Nil): Unit = {
301-
callbacks match {
302-
case m: ManyCallbacks[T] =>
303-
submitWithValue(m.first, resolved, m.last :: deferred)
304-
case t: Transformation[T, _] =>
305-
t.submitWithValue(resolved)
306-
deferred match {
307-
case head :: tail => submitWithValue(head, resolved, tail)
308-
case Nil => //ignore
309-
}
297+
private[this] final def submitWithValue(callbacks: Callbacks[T], resolved: Try[T]): Unit =
298+
if(callbacks.isInstanceOf[ManyCallbacks[T]]) {
299+
val m: ManyCallbacks[T] = callbacks.asInstanceOf[ManyCallbacks[T]]
300+
m.first.submitWithValue(resolved)
301+
submitWithValue(m.rest, resolved)
302+
} else {
303+
callbacks.asInstanceOf[Transformation[T, _]].submitWithValue(resolved)
310304
}
311-
}
312305

313306
/** Link this promise to the root of another promise.
314307
*/
@@ -352,7 +345,7 @@ private[concurrent] final object Promise {
352345
sealed trait Callbacks[-T] {
353346
}
354347

355-
final class ManyCallbacks[-T](final val first: Callbacks[T], final val last: Callbacks[T]) extends Callbacks[T] {
348+
final class ManyCallbacks[-T](final val first: Transformation[T,_], final val rest: Callbacks[T]) extends Callbacks[T] {
356349
// NOTE: This does grow the stack for *linked* callbacks which are transported across the links,
357350
// these should normally be rather flat.
358351
override final def toString: String = "ManyCallbacks"

0 commit comments

Comments
 (0)