Optimize startCoroutine impls, move them to common stdlib
Do not wrap initial continuation for startCoroutine in SafeContinuation This changes leaves only internal declarations and intrinsics as platform dependent parts of the coroutine library, the rest parts (public API) is implemented through them in common module
This commit is contained in:
@@ -18,38 +18,14 @@ package kotlin.coroutines.experimental
|
|||||||
|
|
||||||
import kotlin.coroutines.experimental.intrinsics.*
|
import kotlin.coroutines.experimental.intrinsics.*
|
||||||
|
|
||||||
/**
|
internal fun <R, T> (suspend R.() -> T).createCoroutineInternal(
|
||||||
* Creates coroutine with receiver type [R] and result type [T].
|
|
||||||
* This function creates a new, fresh instance of suspendable computation every time it is invoked.
|
|
||||||
* To start executing the created coroutine, invoke `resume(Unit)` on the returned [Continuation] instance.
|
|
||||||
* The [completion] continuation is invoked when coroutine completes with result of exception.
|
|
||||||
*/
|
|
||||||
@SinceKotlin("1.1")
|
|
||||||
public fun <R, T> (suspend R.() -> T).createCoroutine(
|
|
||||||
receiver: R,
|
receiver: R,
|
||||||
completion: Continuation<T>
|
completion: Continuation<T>
|
||||||
): Continuation<Unit> =
|
): Continuation<Unit> = this.asDynamic()(receiver, completion, true)
|
||||||
SafeContinuation(
|
|
||||||
this.asDynamic()(receiver, completion, true),
|
|
||||||
COROUTINE_SUSPENDED
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
internal fun <T> (suspend () -> T).createCoroutineInternal(
|
||||||
* Creates coroutine without receiver and with result type [T].
|
|
||||||
* This function creates a new, fresh instance of suspendable computation every time it is invoked.
|
|
||||||
* To start executing the created coroutine, invoke `resume(Unit)` on the returned [Continuation] instance.
|
|
||||||
* The [completion] continuation is invoked when coroutine completes with result of exception.
|
|
||||||
*/
|
|
||||||
@SinceKotlin("1.1")
|
|
||||||
public fun <T> (suspend () -> T).createCoroutine(
|
|
||||||
completion: Continuation<T>
|
completion: Continuation<T>
|
||||||
): Continuation<Unit> =
|
): Continuation<Unit> = this.asDynamic()(completion, true)
|
||||||
SafeContinuation(
|
|
||||||
this.asDynamic()(completion, true),
|
|
||||||
COROUTINE_SUSPENDED
|
|
||||||
)
|
|
||||||
|
|
||||||
// ------- internal stuff -------
|
|
||||||
|
|
||||||
@JsName("CoroutineImpl")
|
@JsName("CoroutineImpl")
|
||||||
internal abstract class CoroutineImpl(private val resultContinuation: Continuation<Any?>) : Continuation<Any?> {
|
internal abstract class CoroutineImpl(private val resultContinuation: Continuation<Any?>) : Continuation<Any?> {
|
||||||
|
|||||||
@@ -1,31 +1,5 @@
|
|||||||
package kotlin.coroutines.experimental
|
package kotlin.coroutines.experimental
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates coroutine with receiver type [R] and result type [T].
|
|
||||||
* This function creates a new, fresh instance of suspendable computation every time it is invoked.
|
|
||||||
* To start executing the created coroutine, invoke `resume(Unit)` on the returned [Continuation] instance.
|
|
||||||
* The [completion] continuation is invoked when coroutine completes with result of exception.
|
|
||||||
* An optional [dispatcher] may be specified to customise dispatch of continuations between suspension points inside the coroutine.
|
|
||||||
*/
|
|
||||||
@SinceKotlin("1.1")
|
|
||||||
public header fun <R, T> (suspend R.() -> T).createCoroutine(
|
|
||||||
receiver: R,
|
|
||||||
completion: Continuation<T>
|
|
||||||
): Continuation<Unit>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates coroutine without receiver and with result type [T].
|
|
||||||
* This function creates a new, fresh instance of suspendable computation every time it is invoked.
|
|
||||||
* To start executing the created coroutine, invoke `resume(Unit)` on the returned [Continuation] instance.
|
|
||||||
* The [completion] continuation is invoked when coroutine completes with result of exception.
|
|
||||||
* An optional [dispatcher] may be specified to customise dispatch of continuations between suspension points inside the coroutine.
|
|
||||||
*/
|
|
||||||
@SinceKotlin("1.1")
|
|
||||||
public header fun <T> (suspend () -> T).createCoroutine(
|
|
||||||
completion: Continuation<T>
|
|
||||||
): Continuation<Unit>
|
|
||||||
|
|
||||||
@PublishedApi
|
@PublishedApi
|
||||||
internal header class SafeContinuation<in T> : Continuation<T> {
|
internal header class SafeContinuation<in T> : Continuation<T> {
|
||||||
internal constructor(delegate: Continuation<T>, initialResult: Any?)
|
internal constructor(delegate: Continuation<T>, initialResult: Any?)
|
||||||
@@ -40,3 +14,14 @@ internal header class SafeContinuation<in T> : Continuation<T> {
|
|||||||
override fun resume(value: T): Unit
|
override fun resume(value: T): Unit
|
||||||
override fun resumeWithException(exception: Throwable): Unit
|
override fun resumeWithException(exception: Throwable): Unit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SinceKotlin("1.1")
|
||||||
|
internal header fun <T> (suspend () -> T).createCoroutineInternal(
|
||||||
|
completion: Continuation<T>
|
||||||
|
): Continuation<Unit>
|
||||||
|
|
||||||
|
@SinceKotlin("1.1")
|
||||||
|
internal header fun <R, T> (suspend R.() -> T).createCoroutineInternal(
|
||||||
|
receiver: R,
|
||||||
|
completion: Continuation<T>
|
||||||
|
): Continuation<Unit>
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public fun <R, T> (suspend R.() -> T).startCoroutine(
|
|||||||
receiver: R,
|
receiver: R,
|
||||||
completion: Continuation<T>
|
completion: Continuation<T>
|
||||||
) {
|
) {
|
||||||
createCoroutine(receiver, completion).resume(Unit)
|
createCoroutineInternal(receiver, completion).resume(Unit)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -44,9 +44,34 @@ public fun <R, T> (suspend R.() -> T).startCoroutine(
|
|||||||
public fun <T> (suspend () -> T).startCoroutine(
|
public fun <T> (suspend () -> T).startCoroutine(
|
||||||
completion: Continuation<T>
|
completion: Continuation<T>
|
||||||
) {
|
) {
|
||||||
createCoroutine(completion).resume(Unit)
|
createCoroutineInternal(completion).resume(Unit)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates coroutine with receiver type [R] and result type [T].
|
||||||
|
* This function creates a new, fresh instance of suspendable computation every time it is invoked.
|
||||||
|
* To start executing the created coroutine, invoke `resume(Unit)` on the returned [Continuation] instance.
|
||||||
|
* The [completion] continuation is invoked when coroutine completes with result or exception.
|
||||||
|
*/
|
||||||
|
@SinceKotlin("1.1")
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
public fun <R, T> (suspend R.() -> T).createCoroutine(
|
||||||
|
receiver: R,
|
||||||
|
completion: Continuation<T>
|
||||||
|
): Continuation<Unit> = SafeContinuation(createCoroutineInternal(receiver, completion), COROUTINE_SUSPENDED)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates coroutine without receiver and with result type [T].
|
||||||
|
* This function creates a new, fresh instance of suspendable computation every time it is invoked.
|
||||||
|
* To start executing the created coroutine, invoke `resume(Unit)` on the returned [Continuation] instance.
|
||||||
|
* The [completion] continuation is invoked when coroutine completes with result or exception.
|
||||||
|
*/
|
||||||
|
@SinceKotlin("1.1")
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
public fun <T> (suspend () -> T).createCoroutine(
|
||||||
|
completion: Continuation<T>
|
||||||
|
): Continuation<Unit> = SafeContinuation(createCoroutineInternal(completion), COROUTINE_SUSPENDED)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtains the current continuation instance inside suspend functions and suspends
|
* Obtains the current continuation instance inside suspend functions and suspends
|
||||||
* currently running coroutine.
|
* currently running coroutine.
|
||||||
|
|||||||
@@ -18,54 +18,32 @@
|
|||||||
@file:kotlin.jvm.JvmVersion
|
@file:kotlin.jvm.JvmVersion
|
||||||
package kotlin.coroutines.experimental
|
package kotlin.coroutines.experimental
|
||||||
|
|
||||||
import kotlin.coroutines.experimental.intrinsics.COROUTINE_SUSPENDED
|
|
||||||
import kotlin.coroutines.experimental.jvm.internal.CoroutineImpl
|
import kotlin.coroutines.experimental.jvm.internal.CoroutineImpl
|
||||||
import kotlin.coroutines.experimental.jvm.internal.interceptContinuationIfNeeded
|
import kotlin.coroutines.experimental.jvm.internal.interceptContinuationIfNeeded
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates coroutine with receiver type [R] and result type [T].
|
|
||||||
* This function creates a new, fresh instance of suspendable computation every time it is invoked.
|
|
||||||
* To start executing the created coroutine, invoke `resume(Unit)` on the returned [Continuation] instance.
|
|
||||||
* The [completion] continuation is invoked when coroutine completes with result or exception.
|
|
||||||
*/
|
|
||||||
@SinceKotlin("1.1")
|
@SinceKotlin("1.1")
|
||||||
@Suppress("UNCHECKED_CAST")
|
internal fun <T> (suspend () -> T).createCoroutineInternal(
|
||||||
public fun <R, T> (suspend R.() -> T).createCoroutine(
|
completion: Continuation<T>
|
||||||
|
): Continuation<Unit> =
|
||||||
|
if (this !is CoroutineImpl)
|
||||||
|
buildContinuationByInvokeCall(completion) {
|
||||||
|
(this as Function1<Continuation<T>, Any?>).invoke(completion)
|
||||||
|
}
|
||||||
|
else
|
||||||
|
(this.create(completion) as CoroutineImpl).facade
|
||||||
|
|
||||||
|
@SinceKotlin("1.1")
|
||||||
|
internal fun <R, T> (suspend R.() -> T).createCoroutineInternal(
|
||||||
receiver: R,
|
receiver: R,
|
||||||
completion: Continuation<T>
|
completion: Continuation<T>
|
||||||
): Continuation<Unit> =
|
): Continuation<Unit> =
|
||||||
SafeContinuation(
|
|
||||||
if (this !is CoroutineImpl)
|
if (this !is CoroutineImpl)
|
||||||
buildContinuationByInvokeCall(completion) {
|
buildContinuationByInvokeCall(completion) {
|
||||||
(this as Function2<R, Continuation<T>, Any?>).invoke(receiver, completion)
|
(this as Function2<R, Continuation<T>, Any?>).invoke(receiver, completion)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
((this as CoroutineImpl).create(receiver, completion) as CoroutineImpl).facade,
|
((this as CoroutineImpl).create(receiver, completion) as CoroutineImpl).facade
|
||||||
COROUTINE_SUSPENDED
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates coroutine without receiver and with result type [T].
|
|
||||||
* This function creates a new, fresh instance of suspendable computation every time it is invoked.
|
|
||||||
* To start executing the created coroutine, invoke `resume(Unit)` on the returned [Continuation] instance.
|
|
||||||
* The [completion] continuation is invoked when coroutine completes with result or exception.
|
|
||||||
*/
|
|
||||||
@SinceKotlin("1.1")
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
public fun <T> (suspend () -> T).createCoroutine(
|
|
||||||
completion: Continuation<T>
|
|
||||||
): Continuation<Unit> =
|
|
||||||
SafeContinuation(
|
|
||||||
if (this !is CoroutineImpl)
|
|
||||||
buildContinuationByInvokeCall(completion) {
|
|
||||||
(this as Function1<Continuation<T>, Any?>).invoke(completion)
|
|
||||||
}
|
|
||||||
else
|
|
||||||
((this as CoroutineImpl).create(completion) as CoroutineImpl).facade,
|
|
||||||
COROUTINE_SUSPENDED
|
|
||||||
)
|
|
||||||
|
|
||||||
// INTERNAL DECLARATIONS
|
|
||||||
|
|
||||||
private inline fun <T> buildContinuationByInvokeCall(
|
private inline fun <T> buildContinuationByInvokeCall(
|
||||||
completion: Continuation<T>,
|
completion: Continuation<T>,
|
||||||
|
|||||||
Reference in New Issue
Block a user