Skip to content

Commit

Permalink
Minor optimization in fast-path of JobSupport.tryMakeCompleting
Browse files Browse the repository at this point in the history
Check for simple states on completing before looking for a first child
in a potentially long list of job nodes
  • Loading branch information
elizarov committed Sep 20, 2018
1 parent ee87120 commit 73a2583
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions common/kotlinx-coroutines-core-common/src/JobSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -733,11 +733,9 @@ internal open class JobSupport constructor(active: Boolean) : Job, SelectClause0
private fun tryMakeCompleting(state: Any?, proposedUpdate: Any?, mode: Int): Int {
if (state !is Incomplete)
return COMPLETING_ALREADY_COMPLETING
// find first child
val child = firstChild(state)
// FAST PATH -- no children to wait for && simple state (no list) && not failing => can complete immediately
// Failures always have to go through Finishing state to serialize exception handling
if (child == null && (state is Empty || state is JobNode<*>) && proposedUpdate !is CompletedExceptionally) {
if ((state is Empty || state is JobNode<*>) && state !is ChildJob && proposedUpdate !is CompletedExceptionally) {
if (!tryFinalizeSimpleState(state, proposedUpdate, mode)) return COMPLETING_RETRY
return COMPLETING_COMPLETED
}
Expand Down Expand Up @@ -771,6 +769,7 @@ internal open class JobSupport constructor(active: Boolean) : Job, SelectClause0
// process failing notification here -- it cancels all the children _before_ we start to to wait them (sic!!!)
notifyRootCause?.let { notifyFailing(list, it) }
// now wait for children
val child = firstChild(state)
if (child != null && tryWaitForChild(finishing, child, proposedUpdate))
return COMPLETING_WAITING_CHILDREN
// otherwise -- we have not children left (all were already cancelled?)
Expand Down

0 comments on commit 73a2583

Please sign in to comment.