last day (17 days later) » 

06:39
0
Q: Kotlin - suspending method/function containing inline class type as variable inside definition or return type

rahatGiven @JvmInline value class StringWrapper(val value: String) import kotlinx.coroutines.delay class DummyServiceWrapper { suspend fun simpleSuspendMethod(){ delay(1000L) } suspend fun fetchStringResult(): Result<String> { delay(1000L) return Result.success("...

What is your question exactly? Do you want to know how suspending functions work internally? It's a pretty long to explain, you can read my answer here: stackoverflow.com/a/73680326/448875
@broot can you please check the question now, I have added one more plain suspending method, and its byte code, the issue is in the case of plain and suspending calls it generates bytecode the way expected, however when I just add a line of code that contains an inline class type then it goes mad.
Sorry, I don't see in your example that inline classes change anything. Bytecode is "mad" for suspend functions. Replace inline classes with any other code and you will see a similar bytecode. In your examples there is no case with delay() and some additional code below it, so I'm not sure to what you compare your cases using inline classes.
I mean, return a simple string from fetchString() instead of StringWrapper and I believe you will get a "mad" code as well.
Nope, if neither the function/method signature nor the definition does not contain any inline class being involved then it would be pretty simple and as expected. @broot
Sorry, I don't really know how do you observe what you say. I just verified, function with code: delay(1000L); return StringWrapper("Testing") and delay(1000L); return "Testing" both produce almost 1:1 the same code. You can check it by yourself. Your above examples don't really contain a base case where you delay and then do anything else in the same method, so you don't have anything to compare your "inline" cases with.
06:39
Are you saying that this -> suspend fun fetchString() { delay(1000L) StringWrapper("Testing") } and suspend fun fetchString() { delay(1000L) "Testing" } are producing the same byte code?
I didn't verify this case (there was return in my example), but my guess is the bytecode differs here. In the second case you don't do anything with the string, this is a noop, so the compiler ignores this string entirely. In the first case creating a StringWrapper requires additional code. So you are comparing entirely different cases here: "delay only" and "delay + further code".
I want you to understand that StingWrapper being an inline class, will be replaced by the String type, except for the constructor call. The issue that I am trying to explain is that when I use it inside a suspending method/function, the continuation object is created again in that function.
I don't know how to describe it to you better. What you observe is a typical bytecode for suspend functions. Suspend functions create continuations. There is optimization where if the suspend function consists of a call to anther suspend function only, there is nothing else, then the compiler can skip the whole state machine thing. So if you put a single delay, bytecode will be simple. But if you put any code after it, no matter if it is an inline class, println or anything else, the bytecode will complicate. It has nothing to do with inline classes.
have you verified the same?
I have added UPDATE 1 check that, there are two suspending method, and their bytecode, if you observe, they differ by far and large. @broot
 
2 hours later…
08:17
For me they actually generate exactly the same code - the complicated one. But again, val value = "Testing" is a noop, this code doesn't do anything, so depending on the config, etc., the compiler may decide to entirely skip this line. val value = StringWrapper("Testing") is different, because it invokes the constructor of StringWrapper, so even if we don't do anything with value, it is not a noop. Please verify by adding e.g. println, set a value of a member property, etc.
 
7 hours later…
15:41
the code sample I provided is a sample code to demonstrate the issue
16:00
I have added one more update to the question, for more clarity on the question or rather I should say the issue, can you please check and let me know if you got an understanding of what I am trying to say.
 
1 hour later…
17:21
You locked yourself into thinking this is caused by inline classes and you don't even try to get out of this assumption. Did you try very basic examples like e.g.:

```kotlin
suspend fun foo() {
delay(1000L)
println()
}
```

It generates a similar complicated bytecode, so it shows clearly this is unrelated to inline classes. I don't think it makes sense to discuss this further until you try the above.
Agree, but can you please try below
class DummyServiceWrapper {


suspend fun foo() {
delay(1000L)
print("hey there")
}

suspend fun bar() {
val p = DummyPrinter()
p.printHello()
}

suspend fun baz() {
val p = DummyPrinter()
p.printHello1()
}

}
import com.example.reply.test.StringWrapper
import kotlinx.coroutines.delay

class DummyPrinter{

suspend fun printHello(){
delay(1000L)
println("Hey there")
}


suspend fun printHello1(): StringWrapper {
delay(1000L)
println("Hey there")
return StringWrapper("hello")
}
}
The generated byte code looks like below
public final class DummyServiceWrapper {
@Nullable
public final Object foo(@NotNull Continuation var1) {
Object $continuation;
label20: {
if (var1 instanceof <undefinedtype>) {
$continuation = (<undefinedtype>)var1;
if ((((<undefinedtype>)$continuation).label & Integer.MIN_VALUE) != 0) {
((<undefinedtype>)$continuation).label -= Integer.MIN_VALUE;
break label20;
}
}

$continuation = new ContinuationImpl(var1) {
// $FF: synthetic field
Object result;
int label;

@Nullable
public final Object invokeSuspend(@NotNull Object $result) {
 
3 hours later…
20:36
This is actually an interesting case. I believe the explanation is here: ((StringWrapper)$result).unbox-impl(). The compiler decides to unbox the StringWrapper, which it probably doesn't have to do, because we don't use the return value of printHello1(). Anyway, no matter if we have to unbox or not, this additional unboxing operation again causes that there is some code after the call to the inner suspend function. And because of this, we need the "mad" code.
We are you so interested in this case? Is this just for curiosity? In 99% of cases suspend functions are compiled into this "mad" code. What we observe here is some kind of a corner case.
I don't know this very well, but apparently. there is an optimization similar to tail call optimization. If the last operation in a suspend function is a call to another suspend function, then the compiler doesn't put a full suspension point there, because we don't need to resume after that point.
And if this is the only call to a suspend function, then we don't at all need to create the "mad" code, because we never need to resume inside the function.
But if for any reason there is any code after the suspension point (call to inner suspend function), then we need the full "mad" implementation.

  last day (17 days later) »