I was examining the bytecode output of Duby this morning, comparing it
to javac output, and noticed that for fib() there's only one small
difference: javac inserts an extra branch in order to have a single
return instruction.
Is this useful? Necessary? Anyone know why javac does this, since it
increases the bytecode size for this simple case?
Duby source:
def fib(a => :fixnum)
if a < 2
a
else
fib(a - 1) + fib(a - 2)
end
end
Java source:
public static int fib(int a) {
if (a < 2) {
return a;
} else {
return fib(a - 1) + fib(a - 2);
}
}
Duby bytecode:
public static int fib(int);
Code:
0: iload_0
1: iconst_2
2: if_icmpge 7
5: iload_0
6: ireturn
7: iload_0
8: iconst_1
9: isub
10: invokestatic #2; //Method fib:(I)I
13: iload_0
14: iconst_2
15: isub
16: invokestatic #2; //Method fib:(I)I
19: iadd
20: ireturn
Java bytecode:
public static int fib(int);
Code:
0: iload_0
1: iconst_2
2: if_icmpge 9
5: iload_0
6: goto 22
9: iload_0
10: iconst_1
11: isub
12: invokestatic #11; //Method fib:(I)I
15: iload_0
16: iconst_2
17: isub
18: invokestatic #11; //Method fib:(I)I
21: iadd
22: ireturn
Other than the branch-to-return, the two compilers output identical
code. So I'm understandably curious if this is a difference I should be
interested in.
- Charlie
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "JVM
Languages" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/jvm-languages?hl=en
-~----------~----~----~----~------~----~------~--~---