Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter return Unit from tail-optimized suspend funs #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

poseidon-mysugr
Copy link

No description provided.

@poseidon-mysugr poseidon-mysugr changed the base branch from myMaster to master November 17, 2021 14:12
@poseidon-mysugr poseidon-mysugr force-pushed the feature/tailOptimizedSuspendFunsReturnUnit branch from a22b3e6 to 4759c3f Compare November 17, 2021 14:21
Copy link

@ChristopherKoellner ChristopherKoellner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also attach the bytecode to this pull request?
Others than that LGTM!

@poseidon-mysugr
Copy link
Author

This function ignores some uncoverable instructions and branchs of suspend funs:

    suspend fun example(b: Boolean) {
        if (b)
            suspendingFunction()
        else
            suspendingFunction()
    }

produces the following bytecode:

   L0
    ILOAD 1
    IFEQ L1
   L2
    ALOAD 0
    ALOAD 2
    INVOKEVIRTUAL suspendingFunction (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
>   DUP
>   INVOKESTATIC kotlin/coroutines/intrinsics/IntrinsicsKt.getCOROUTINE_SUSPENDED ()Ljava/lang/Object;
>   IF_ACMPNE L3
>   ARETURN
>  L3
>   POP
    GOTO L4
   L1
    ALOAD 0
    ALOAD 2
    INVOKEVIRTUAL suspendingFunction (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
>   DUP
>   INVOKESTATIC kotlin/coroutines/intrinsics/IntrinsicsKt.getCOROUTINE_SUSPENDED ()Ljava/lang/Object;
>   IF_ACMPNE L5
>   ARETURN
>  L5
>   POP
   L4
>   GETSTATIC kotlin/Unit.INSTANCE : Lkotlin/Unit;
>   ARETURN

Here, the first two blocks marked with > need to be ignored, as they are only executed if the coroutine function call (suspendingFunction) actually does suspend, in which case the COROUTINE_SUSPENDED marker is passed on. The last block at the bottom in turn cannot be covered if the suspendingFunction never suspends.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants