JS: improve performance of suspend function calling suspen function when no suspension ever occur.
This commit is contained in:
@@ -177,11 +177,10 @@ class CoroutineFunctionTransformer(private val program: JsProgram, private val f
|
|||||||
val instanceName = functionWithBody.scope.declareFreshName("instance")
|
val instanceName = functionWithBody.scope.declareFreshName("instance")
|
||||||
functionWithBody.body.statements += JsAstUtils.newVar(instanceName, JsNameRef(context.metadata.facadeName, instantiation))
|
functionWithBody.body.statements += JsAstUtils.newVar(instanceName, JsNameRef(context.metadata.facadeName, instantiation))
|
||||||
|
|
||||||
val invokeResume = JsInvocation(JsNameRef(context.metadata.resumeName, instanceName.makeRef()), JsLiteral.NULL).makeStmt()
|
val invokeResume = JsReturn(JsInvocation(JsNameRef(context.metadata.doResumeName, instanceName.makeRef()), JsLiteral.NULL))
|
||||||
val returnResult = JsReturn(JsNameRef(context.metadata.resultName, instanceName.makeRef()))
|
|
||||||
|
|
||||||
functionWithBody.body.statements += JsIf(
|
functionWithBody.body.statements += JsIf(
|
||||||
suspendedName.makeRef(), JsReturn(instanceName.makeRef()), JsBlock(invokeResume, returnResult))
|
suspendedName.makeRef(), JsReturn(instanceName.makeRef()), invokeResume)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateCoroutineBody(
|
private fun generateCoroutineBody(
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ import kotlin.coroutines.intrinsics.*
|
|||||||
public fun <R, T> (suspend R.() -> T).createCoroutine(
|
public fun <R, T> (suspend R.() -> T).createCoroutine(
|
||||||
receiver: R,
|
receiver: R,
|
||||||
completion: Continuation<T>
|
completion: Continuation<T>
|
||||||
): Continuation<Unit> = this.asDynamic()(receiver, completion, true).facade
|
): Continuation<Unit> = this.asDynamic()(receiver, completion, true)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts coroutine with receiver type [R] and result type [T].
|
* Starts coroutine with receiver type [R] and result type [T].
|
||||||
@@ -40,7 +40,8 @@ public fun <R, T> (suspend R.() -> T).startCoroutine(
|
|||||||
receiver: R,
|
receiver: R,
|
||||||
completion: Continuation<T>
|
completion: Continuation<T>
|
||||||
) {
|
) {
|
||||||
this.asDynamic()(receiver, completion)
|
val coroutine: Continuation<Any?> = this.asDynamic()(receiver, completion, true)
|
||||||
|
coroutine.resume(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -52,7 +53,7 @@ public fun <R, T> (suspend R.() -> T).startCoroutine(
|
|||||||
@SinceKotlin("1.1")
|
@SinceKotlin("1.1")
|
||||||
public fun <T> (suspend () -> T).createCoroutine(
|
public fun <T> (suspend () -> T).createCoroutine(
|
||||||
completion: Continuation<T>
|
completion: Continuation<T>
|
||||||
): Continuation<Unit> = this.asDynamic()(completion, true).facade
|
): Continuation<Unit> = this.asDynamic()(completion, true)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts coroutine without receiver and with result type [T].
|
* Starts coroutine without receiver and with result type [T].
|
||||||
@@ -63,7 +64,8 @@ public fun <T> (suspend () -> T).createCoroutine(
|
|||||||
public fun <T> (suspend () -> T).startCoroutine(
|
public fun <T> (suspend () -> T).startCoroutine(
|
||||||
completion: Continuation<T>
|
completion: Continuation<T>
|
||||||
) {
|
) {
|
||||||
this.asDynamic()(completion)
|
val coroutine: Continuation<Any?> = this.asDynamic()(completion, true)
|
||||||
|
coroutine.resume(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -97,22 +99,16 @@ internal abstract class CoroutineImpl(private val resultContinuation: Continuati
|
|||||||
|
|
||||||
override fun resume(data: Any?) {
|
override fun resume(data: Any?) {
|
||||||
result = data
|
result = data
|
||||||
try {
|
doResumeWrapper()
|
||||||
result = doResume()
|
|
||||||
if (result != SUSPENDED_MARKER) {
|
|
||||||
val data = result
|
|
||||||
result = SUSPENDED_MARKER
|
|
||||||
resultContinuation.resume(data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (e: Throwable) {
|
|
||||||
resultContinuation.resumeWithException(e)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun resumeWithException(exception: Throwable) {
|
override fun resumeWithException(exception: Throwable) {
|
||||||
state = exceptionState
|
state = exceptionState
|
||||||
this.exception = exception
|
this.exception = exception
|
||||||
|
doResumeWrapper()
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun doResumeWrapper() {
|
||||||
try {
|
try {
|
||||||
result = doResume()
|
result = doResume()
|
||||||
if (result != SUSPENDED_MARKER) {
|
if (result != SUSPENDED_MARKER) {
|
||||||
|
|||||||
Reference in New Issue
Block a user