Skip to content

Commit 6634ed7

Browse files
committed
Minor optimization in fast-path of JobSupport.tryMakeCompleting
Check for simple states on completing before looking for a first child in a potentially long list of job nodes
1 parent 7e23875 commit 6634ed7

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

common/kotlinx-coroutines-core-common/src/JobSupport.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,11 +733,9 @@ internal open class JobSupport constructor(active: Boolean) : Job, SelectClause0
733733
private fun tryMakeCompleting(state: Any?, proposedUpdate: Any?, mode: Int): Int {
734734
if (state !is Incomplete)
735735
return COMPLETING_ALREADY_COMPLETING
736-
// find first child
737-
val child = firstChild(state)
738736
// FAST PATH -- no children to wait for && simple state (no list) && not failing => can complete immediately
739737
// Failures always have to go through Finishing state to serialize exception handling
740-
if (child == null && (state is Empty || state is JobNode<*>) && proposedUpdate !is CompletedExceptionally) {
738+
if ((state is Empty || state is JobNode<*>) && state !is ChildJob && proposedUpdate !is CompletedExceptionally) {
741739
if (!tryFinalizeSimpleState(state, proposedUpdate, mode)) return COMPLETING_RETRY
742740
return COMPLETING_COMPLETED
743741
}
@@ -771,6 +769,7 @@ internal open class JobSupport constructor(active: Boolean) : Job, SelectClause0
771769
// process failing notification here -- it cancels all the children _before_ we start to to wait them (sic!!!)
772770
notifyRootCause?.let { notifyFailing(list, it) }
773771
// now wait for children
772+
val child = firstChild(state)
774773
if (child != null && tryWaitForChild(finishing, child, proposedUpdate))
775774
return COMPLETING_WAITING_CHILDREN
776775
// otherwise -- we have not children left (all were already cancelled?)

0 commit comments

Comments
 (0)