From ad9fe53ee2fb0b1e8695942129d6fe726f614a26 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Thu, 17 Aug 2017 17:08:03 +0700 Subject: [PATCH] Avoid local var entry for continuation parameter in suspend function The primary reason is getting rid of redundant stack spilling, but also it's not very sensible to have such entry since the parameter is synthetic --- .../kotlin/codegen/FunctionCodegen.java | 27 +++++++++++++++++-- .../coroutines/doNotReassignContinuation.kt | 3 +-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java index 4d60198825c..78df49652d8 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java @@ -595,8 +595,31 @@ public class FunctionCodegen { @NotNull KotlinTypeMapper typeMapper, int shiftForDestructuringVariables ) { - generateLocalVariablesForParameters(mv, jvmMethodSignature, thisType, methodBegin, methodEnd, - functionDescriptor.getValueParameters(), + if (functionDescriptor.isSuspend()) { + FunctionDescriptor unwrapped = CoroutineCodegenUtilKt.unwrapInitialDescriptorForSuspendFunction( + functionDescriptor + ); + + if (unwrapped != functionDescriptor) { + generateLocalVariableTable( + mv, + new JvmMethodSignature( + jvmMethodSignature.getAsmMethod(), + jvmMethodSignature.getValueParameters().subList( + 0, + jvmMethodSignature.getValueParameters().size() - 1 + ) + ), + unwrapped, + thisType, methodBegin, methodEnd, ownerKind, typeMapper, shiftForDestructuringVariables + ); + return; + } + } + + generateLocalVariablesForParameters(mv, + jvmMethodSignature, + thisType, methodBegin, methodEnd, functionDescriptor.getValueParameters(), AsmUtil.isStaticMethod(ownerKind, functionDescriptor), typeMapper, shiftForDestructuringVariables ); } diff --git a/compiler/testData/codegen/bytecodeText/coroutines/doNotReassignContinuation.kt b/compiler/testData/codegen/bytecodeText/coroutines/doNotReassignContinuation.kt index 6185ede2818..7fbb856a110 100644 --- a/compiler/testData/codegen/bytecodeText/coroutines/doNotReassignContinuation.kt +++ b/compiler/testData/codegen/bytecodeText/coroutines/doNotReassignContinuation.kt @@ -14,5 +14,4 @@ suspend fun suspendThere(param: Int, param2: String, param3: Long): String { return a + b } -/* 2 stores happen because the continuation parameter is visible in debug and should be spilled */ -// 2 ASTORE 4 +// 0 ASTORE 4