Skip to content

Instantly share code, notes, and snippets.

@Deamon5550
Created March 27, 2017 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Deamon5550/617b2d3ac2aa1b3f66ed64bb9ee2829c to your computer and use it in GitHub Desktop.
Save Deamon5550/617b2d3ac2aa1b3f66ed64bb9ee2829c to your computer and use it in GitHub Desktop.
Kotlin's lack of optimizations of gotos
fun main(args: Array<String>) {
val x = 10
val y = 9
if(x == 6) {
if(y == 6) {
println("a")
} else {
println("b")
}
} else {
println("c")
}
}
/*
The goto at the end of the inner if block (instruction 34 in hello.kt.javap)
points directly at another goto (insn 47) which is the end of the outer if block.
In the equivalent compiled java you can see that the goto at the
end of the inner if (insn 26) targets directly the end of the outer else (insn 48).
I'm also not sure the reason for putting the constant strings into
local slot 3 before passing them to the println method, and I'd be
very interested to know why that is.
*/
public static void main(java.lang.String[]);
Code:
0: bipush 9
2: istore_1
3: bipush 10
5: istore_2
6: iload_1
7: bipush 6
9: if_icmpne 40
12: iload_2
13: bipush 6
15: if_icmpne 29
18: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
21: ldc #3 // String a
23: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
26: goto 48
29: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
32: ldc #5 // String b
34: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
37: goto 48
40: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
43: ldc #6 // String c
45: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
48: return
}
public static final void main(java.lang.String[]);
Code:
0: aload_0
1: ldc #50 // String args
3: invokestatic #29 // Method kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull:(Ljava/lang/Object;Ljava/lang/String;)V
6: bipush 10
8: istore_1
9: bipush 9
11: istore_2
12: iload_1
13: bipush 6
15: if_icmpne 50
18: iload_2
19: bipush 6
21: if_icmpne 37
24: ldc #51 // String a
26: astore_3
27: getstatic #57 // Field java/lang/System.out:Ljava/io/PrintStream;
30: aload_3
31: invokevirtual #63 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
34: goto 47
37: ldc #64 // String b
39: astore_3
40: getstatic #57 // Field java/lang/System.out:Ljava/io/PrintStream;
43: aload_3
44: invokevirtual #63 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
47: goto 60
50: ldc #66 // String c
52: astore_3
53: getstatic #57 // Field java/lang/System.out:Ljava/io/PrintStream;
56: aload_3
57: invokevirtual #63 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
60: return
}
@dzharkov
Copy link

Hello!
Thank you for request.
I've filed an issue to our tracker.

Putting constant to a local variable happens because println is an inline function and the 3rd variable is its inline parameter, and it can be used many times within inlined body.
But here it's, of course, redundant, so here is another issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment