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:
Denis Zharkov
2017-01-31 18:24:33 +03:00
parent a65d86293b
commit 6dde567be4
4 changed files with 55 additions and 91 deletions
+4 -28
View File
@@ -18,38 +18,14 @@ package kotlin.coroutines.experimental
import kotlin.coroutines.experimental.intrinsics.*
/**
* 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(
internal fun <R, T> (suspend R.() -> T).createCoroutineInternal(
receiver: R,
completion: Continuation<T>
): Continuation<Unit> =
SafeContinuation(
this.asDynamic()(receiver, completion, true),
COROUTINE_SUSPENDED
)
): Continuation<Unit> = this.asDynamic()(receiver, completion, true)
/**
* 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(
internal fun <T> (suspend () -> T).createCoroutineInternal(
completion: Continuation<T>
): Continuation<Unit> =
SafeContinuation(
this.asDynamic()(completion, true),
COROUTINE_SUSPENDED
)
// ------- internal stuff -------
): Continuation<Unit> = this.asDynamic()(completion, true)
@JsName("CoroutineImpl")
internal abstract class CoroutineImpl(private val resultContinuation: Continuation<Any?>) : Continuation<Any?> {
@@ -1,31 +1,5 @@
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
internal header class SafeContinuation<in T> : Continuation<T> {
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 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,
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(
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
* currently running coroutine.
@@ -18,54 +18,32 @@
@file:kotlin.jvm.JvmVersion
package kotlin.coroutines.experimental
import kotlin.coroutines.experimental.intrinsics.COROUTINE_SUSPENDED
import kotlin.coroutines.experimental.jvm.internal.CoroutineImpl
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")
@Suppress("UNCHECKED_CAST")
public fun <R, T> (suspend R.() -> T).createCoroutine(
internal fun <T> (suspend () -> T).createCoroutineInternal(
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,
completion: Continuation<T>
): Continuation<Unit> =
SafeContinuation(
if (this !is CoroutineImpl)
buildContinuationByInvokeCall(completion) {
(this as Function2<R, Continuation<T>, Any?>).invoke(receiver, completion)
}
else
((this as CoroutineImpl).create(receiver, completion) as CoroutineImpl).facade,
COROUTINE_SUSPENDED
)
((this as CoroutineImpl).create(receiver, completion) as CoroutineImpl).facade
/**
* 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(
completion: Continuation<T>,